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

#URL, #availability, #condition, #currency, #history, #id, #price, #retailer, #seller, #timestamp

Instance Method Summary collapse

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



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_priceObject



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_priceObject



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_hObject



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