Class: CatalogAPI::Item

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Item

Returns a new instance of Item.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/catalogapi/item.rb', line 13

def initialize(opts)
  @brand = opts[:brand]
  @catalog_item_id = opts[:catalog_item_id]
  @catalog_price = opts[:catalog_price]
  @categories = opts[:categories].to_h[:integer]
  @currency = opts[:currency] || 'USD'
  @description = opts[:description]
  @has_options = opts[:has_options]
  @image_150 = opts[:image_150]
  @image_300 = opts[:image_300]
  @image_75 = opts[:image_75]
  @model = opts[:model]
  @name = opts[:name]
  @options = opts[:options]
  @original_points = opts[:original_points]
  @original_price = opts[:original_price]
  @points = opts[:points]
  @rank = opts[:rank]
  @retail_price = opts[:retail_price]
  @shipping_estimate = opts[:shipping_estimate]
  @socket_id = opts[:socket_id]
  @tags = opts[:tags].to_h[:string]
  @quantity = opts[:quantity] || 1
  @option_id = opts[:option_id]
end

Instance Attribute Details

#brandObject (readonly)

Returns the value of attribute brand.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def brand
  @brand
end

#catalog_item_idObject (readonly)

Returns the value of attribute catalog_item_id.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def catalog_item_id
  @catalog_item_id
end

#catalog_priceObject (readonly)

Returns the value of attribute catalog_price.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def catalog_price
  @catalog_price
end

#categoriesObject (readonly)

Returns the value of attribute categories.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def categories
  @categories
end

#currencyObject (readonly)

Returns the value of attribute currency.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def currency
  @currency
end

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/catalogapi/item.rb', line 5

def description
  @description
end

#has_optionsObject (readonly)

Returns the value of attribute has_options.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def has_options
  @has_options
end

#image_150Object (readonly)

Returns the value of attribute image_150.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def image_150
  @image_150
end

#image_300Object (readonly)

Returns the value of attribute image_300.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def image_300
  @image_300
end

#image_75Object (readonly)

Returns the value of attribute image_75.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def image_75
  @image_75
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def name
  @name
end

#option_idObject (readonly)

Returns the value of attribute option_id.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def option_id
  @option_id
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def options
  @options
end

#original_pointsObject (readonly)

Returns the value of attribute original_points.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def original_points
  @original_points
end

#original_priceObject (readonly)

Returns the value of attribute original_price.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def original_price
  @original_price
end

#pointsObject (readonly)

Returns the value of attribute points.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def points
  @points
end

#quantityObject (readonly)

Returns the value of attribute quantity.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def quantity
  @quantity
end

#rankObject (readonly)

Returns the value of attribute rank.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def rank
  @rank
end

#retail_priceObject (readonly)

Returns the value of attribute retail_price.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def retail_price
  @retail_price
end

#shipping_estimateObject (readonly)

Returns the value of attribute shipping_estimate.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def shipping_estimate
  @shipping_estimate
end

#socket_idObject (readonly)

Returns the value of attribute socket_id.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def socket_id
  @socket_id
end

#tagsObject (readonly)

Returns the value of attribute tags.



7
8
9
# File 'lib/catalogapi/item.rb', line 7

def tags
  @tags
end

Instance Method Details

#order_paramsObject



39
40
41
42
43
44
45
46
47
# File 'lib/catalogapi/item.rb', line 39

def order_params
  {
    catalog_item_id: catalog_item_id,
    quantity: quantity,
    currency: currency,
    catalog_price: catalog_price.to_f,
    option_id: option_id
  }.reject { |_k, v| v.nil? }.to_h
end

#orderable?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/catalogapi/item.rb', line 49

def orderable?
  !catalog_item_id.nil? && !catalog_price.nil?
end

#viewObject

Raises:



53
54
55
56
57
58
59
60
61
62
# File 'lib/catalogapi/item.rb', line 53

def view
  raise CatalogAPI::Error, 'No Socket ID' if socket_id.nil?
  raise CatalogAPI::Error, 'No Catalog Item ID' if catalog_item_id.nil?

  request = CatalogAPI.request.new(:view_item)
                      .get(socket_id: socket_id, catalog_item_id: catalog_item_id)
  fields = request.json.dig(:view_item_response, :view_item_result, :item)
  request.data = self.class.new(fields.merge(socket_id: socket_id))
  request
end