Class: ShopsavvyDataApi::OfferWithHistory
- Defined in:
- lib/shopsavvy_data_api/models.rb
Overview
Offer with historical price data
Instance Attribute Summary collapse
-
#price_history ⇒ Object
readonly
Returns the value of attribute price_history.
Attributes inherited from Offer
#URL, #availability, #condition, #currency, #history, #id, #price, #retailer, #seller, #timestamp
Instance Method Summary collapse
- #average_price ⇒ Object
-
#initialize(data) ⇒ OfferWithHistory
constructor
A new instance of OfferWithHistory.
- #max_price ⇒ Object
- #min_price ⇒ Object
- #to_h ⇒ Object
Methods inherited from Offer
#in_stock?, #last_updated, #limited_stock?, #new_condition?, #offer_id, #out_of_stock?, #refurbished_condition?, #url, #used_condition?
Constructor Details
#initialize(data) ⇒ OfferWithHistory
Returns a new instance of OfferWithHistory.
221 222 223 224 |
# File 'lib/shopsavvy_data_api/models.rb', line 221 def initialize(data) super(data) @price_history = (data["price_history"] || []).map { |entry| PriceHistoryEntry.new(entry) } end |
Instance Attribute Details
#price_history ⇒ Object (readonly)
Returns the value of attribute price_history.
219 220 221 |
# File 'lib/shopsavvy_data_api/models.rb', line 219 def price_history @price_history end |
Instance Method Details
#average_price ⇒ Object
242 243 244 245 246 247 |
# File 'lib/shopsavvy_data_api/models.rb', line 242 def average_price return nil if price_history.empty? prices = price_history.map(&:price) prices.sum.to_f / prices.length end |
#max_price ⇒ Object
236 237 238 239 240 |
# File 'lib/shopsavvy_data_api/models.rb', line 236 def max_price return nil if price_history.empty? price_history.map(&:price).max end |
#min_price ⇒ Object
230 231 232 233 234 |
# File 'lib/shopsavvy_data_api/models.rb', line 230 def min_price return nil if price_history.empty? price_history.map(&:price).min end |
#to_h ⇒ Object
226 227 228 |
# File 'lib/shopsavvy_data_api/models.rb', line 226 def to_h super.merge(price_history: price_history.map(&:to_h)) end |