Class: InstacartApi::Item

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/instacart_api/models/item.rb

Overview

A single product returned by the Items query.

Instacart item ids look like items_<retailerLocationId>-<productId> (e.g. items_3113-22373938); that full id is what the cart mutation expects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Item

Returns a new instance of Item.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/instacart_api/models/item.rb', line 15

def initialize(data)
  @id = data["id"]
  @name = data["name"]
  @size = data["size"]
  @product_id = data["productId"]
  @brand_name = data["brandName"]

  pricing = data.dig("price", "viewSection") || {}
  @price = pricing["priceValueString"]&.to_f
  @full_price = pricing["fullPriceString"]

  availability = data["availability"] || {}
  @available = availability["available"]
  @stock_level = availability["stockLevel"]

  @quantity_type =
    (data["quantityAttributes"] || {})["quantityType"] || "each"
end

Instance Attribute Details

#brand_nameObject (readonly)

Returns the value of attribute brand_name.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def brand_name
  @brand_name
end

#full_priceObject (readonly)

Returns the value of attribute full_price.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def full_price
  @full_price
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def name
  @name
end

#priceObject (readonly)

Returns the value of attribute price.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def price
  @price
end

#product_idObject (readonly)

Returns the value of attribute product_id.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def product_id
  @product_id
end

#quantity_typeObject (readonly)

Returns the value of attribute quantity_type.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def quantity_type
  @quantity_type
end

#sizeObject (readonly)

Returns the value of attribute size.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def size
  @size
end

#stock_levelObject (readonly)

Returns the value of attribute stock_level.



12
13
14
# File 'lib/instacart_api/models/item.rb', line 12

def stock_level
  @stock_level
end

Instance Method Details

#<=>(other) ⇒ Object

Compare by price so a list of items can be sorted cheapest-first.



39
40
41
# File 'lib/instacart_api/models/item.rb', line 39

def <=>(other)
  price <=> other.price
end

#available?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/instacart_api/models/item.rb', line 34

def available?
  @available == true
end