Class: PricePulse::Tracker
- Inherits:
-
Object
- Object
- PricePulse::Tracker
- Defined in:
- lib/price_pulse/tracker.rb
Instance Method Summary collapse
- #compare_prices(item_name) ⇒ Object
-
#initialize(items) ⇒ Tracker
constructor
A new instance of Tracker.
Constructor Details
#initialize(items) ⇒ Tracker
Returns a new instance of Tracker.
12 13 14 |
# File 'lib/price_pulse/tracker.rb', line 12 def initialize(items) @items = items end |
Instance Method Details
#compare_prices(item_name) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/price_pulse/tracker.rb', line 16 def compare_prices(item_name) target_price = @items[item_name] return "Item not found." unless target_price full_prices = get_prices(item_name) # 1. Filter out non-numerical data (like the page title) numerical_prices = full_prices.reject { |key, value| key == "Source (Title)" || value.is_a?(String) } # 2. Find the best price and shop if numerical_prices.empty? best_price = target_price best_shop = "N/A (No online prices found)" else best_price = numerical_prices.values.min best_shop = numerical_prices.key(best_price) end report = ["--- Tracking: #{item_name} (Target: $#{target_price}) ---"] # List all prices, including the live title or errors full_prices.each { |shop, price| report << " - #{shop} price: #{price.is_a?(String) ? price : "$#{price}"}" } if best_price < target_price report << " => **DEAL!** Below target price of $#{target_price}" else report << " => Price is currently $#{best_price}, waiting for a better deal." end report << "Best Current Price Found: #{best_price == target_price ? "Target Price" : "$#{best_price}"} at #{best_shop}" report.join("\n") end |