Class: Tesco::Groceries::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/tesco.rb

Overview

Represents an individual grocery item, #healthier_alternative, #cheaper_alternative and #base_product are populated as detailess Products. Requesting any information from these will retrieve full information from the API.

Direct Known Subclasses

BasketItem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_productObject (readonly)

Returns the value of attribute base_product.



179
180
181
# File 'lib/tesco.rb', line 179

def base_product
  @base_product
end

#cheaper_alternativeObject (readonly)

Returns the value of attribute cheaper_alternative.



179
180
181
# File 'lib/tesco.rb', line 179

def cheaper_alternative
  @cheaper_alternative
end

#healthier_alternativeObject (readonly)

Returns the value of attribute healthier_alternative.



179
180
181
# File 'lib/tesco.rb', line 179

def healthier_alternative
  @healthier_alternative
end

#image_urlObject (readonly)

Returns the value of attribute image_url.



178
179
180
# File 'lib/tesco.rb', line 178

def image_url
  @image_url
end

#max_quantityObject (readonly)

Returns the value of attribute max_quantity.



178
179
180
# File 'lib/tesco.rb', line 178

def max_quantity
  @max_quantity
end

#nameObject (readonly)

Returns the value of attribute name.



178
179
180
# File 'lib/tesco.rb', line 178

def name
  @name
end

#offerObject (readonly)

Returns the value of attribute offer.



178
179
180
# File 'lib/tesco.rb', line 178

def offer
  @offer
end

#product_idObject (readonly)

Returns the value of attribute product_id.



180
181
182
# File 'lib/tesco.rb', line 180

def product_id
  @product_id
end

Class Method Details

.new(api, product_id, more = nil) ⇒ Object

Don’t use this yourself!

The unusual initialization here is so that there is only ever one instance of each product. This means that using #Products as keys in a hash will always work, and (as they’re identical) it’ll also save memory.

Raises:

  • (ArgumentError)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/tesco.rb', line 187

def self.new(api,product_id,more = nil) # :nodoc:
  raise ArgumentError, "Not a product id" if not product_id =~ /^\d+$/
  @@instances ||= {}

  # Make sure we only ever have on instance of each product
  # If we have an instance then we should just return that
  if @@instances[product_id]
    # If we've been passed more then set it as it'll be more up-to-date
    @@instances[product_id].instance_variable_set(:@more,more) if !more.nil?
    return @@instances[product_id] 
  end

  # We don't have an instance of this product yet, go ahead and make one
  new_product = self.allocate
  new_product.instance_variable_set(:@product_id,product_id)
  new_product.instance_variable_set(:@api,api)
  new_product.instance_variable_set(:@more,more)
  new_product.details if !more.nil?
  @@instances[product_id] = new_product
end

Instance Method Details

#detailsObject

Will refresh the details of the product.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/tesco.rb', line 213

def details
  # If we had some free 'more' data from the instanciation we should use it!
  if @more
    more = @more
    remove_instance_variable(:@more)
  end

  # If we have no data then we should get it from the ProductId
  if more.nil?
    # TODO: check to see if there are more than one
    more = @api.api_request('productsearch',:searchtext => @product_id)['Products'][0]
  end

  @healthier_alternative = Product.new(@api,more['HealthierAlthernativeProductId']) rescue nil
  @cheaper_alternative = Product.new(@api,more['CheaperAlthernativeProductId']) rescue nil
  @base_product = Product.new(@api,more['BaseProductId']) rescue nil
  @image_url = more['ImagePath']
  # ProducyType?
  # OfferValidity?
  @name = more['Name']
  @max_quantity = more['MaximumPurchaseQuantity']
  @barcode = Barcode.new(more['EANBarcode'])
  @offer = Offer.new(more['OfferLabelImagePath'],more['OfferPromotion'],more['OfferValidity']) rescue nil
end

#inspectObject



208
209
210
# File 'lib/tesco.rb', line 208

def inspect
  name
end