Class: ShopsavvyDataApi::OfferWithHistory

Inherits:
Offer
  • Object
show all
Defined in:
lib/shopsavvy_data_api/models.rb

Overview

Offer with historical price data

Instance Attribute Summary collapse

Attributes inherited from Offer

#availability, #condition, #currency, #last_updated, #offer_id, #price, #retailer, #shipping, #url

Instance Method Summary collapse

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_historyObject (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_priceObject



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_priceObject



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_priceObject



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_hObject



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