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
#availability, #condition, #currency, #last_updated, #offer_id, #price, #retailer, #shipping, #url
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?, #limited_stock?, #new_condition?, #out_of_stock?, #refurbished_condition?, #used_condition?
Constructor Details
#initialize(data) ⇒ OfferWithHistory
Returns a new instance of OfferWithHistory.
142 143 144 145 |
# File 'lib/shopsavvy_data_api/models.rb', line 142 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.
140 141 142 |
# File 'lib/shopsavvy_data_api/models.rb', line 140 def price_history @price_history end |
Instance Method Details
#average_price ⇒ Object
163 164 165 166 167 168 |
# File 'lib/shopsavvy_data_api/models.rb', line 163 def average_price return nil if price_history.empty? prices = price_history.map(&:price) prices.sum.to_f / prices.length end |
#max_price ⇒ Object
157 158 159 160 161 |
# File 'lib/shopsavvy_data_api/models.rb', line 157 def max_price return nil if price_history.empty? price_history.map(&:price).max end |
#min_price ⇒ Object
151 152 153 154 155 |
# File 'lib/shopsavvy_data_api/models.rb', line 151 def min_price return nil if price_history.empty? price_history.map(&:price).min end |
#to_h ⇒ Object
147 148 149 |
# File 'lib/shopsavvy_data_api/models.rb', line 147 def to_h super.merge(price_history: price_history.map(&:to_h)) end |