Class: Gilt::Product

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product_body) ⇒ Product

Returns a new instance of Product.



29
30
31
# File 'lib/gilt/product.rb', line 29

def initialize(product_body)
  @product = product_body
end

Class Method Details

.categories(apikey, affid = nil) ⇒ Object



24
25
26
27
# File 'lib/gilt/product.rb', line 24

def self.categories(apikey, affid=nil)
  response = client(apikey, affid).categories.perform
  response.parse["categories"] || []
end

.client(apikey, affid = nil) ⇒ Object



11
12
13
# File 'lib/gilt/product.rb', line 11

def self.client(apikey, affid=nil)
  Gilt::Client::Products.new(apikey, affid)
end

.create(product_id, apikey, affid = nil) ⇒ Object



19
20
21
22
# File 'lib/gilt/product.rb', line 19

def self.create(product_id, apikey, affid=nil)
  response = detail(product_id, apikey, affid).perform
  self.new response.parse
end

.defer(future) ⇒ Object



6
7
8
9
# File 'lib/gilt/product.rb', line 6

def self.defer(future)
  require 'weary/deferred'
  Weary::Deferred.new future, self, lambda {|product, response| product.new(response.parse) }
end

.detail(product_id, apikey, affid = nil) ⇒ Object



15
16
17
# File 'lib/gilt/product.rb', line 15

def self.detail(product_id, apikey, affid=nil)
  client(apikey, affid).detail(:product_id => product_id)
end

Instance Method Details

#brandObject



45
46
47
# File 'lib/gilt/product.rb', line 45

def brand
  @product["brand"]
end

#care_instructionsObject



68
69
70
# File 'lib/gilt/product.rb', line 68

def care_instructions
  fetch_content :care_instructions
end

#categoriesObject



53
54
55
# File 'lib/gilt/product.rb', line 53

def categories
  @product["categories"] || []
end

#colorsObject



107
108
109
# File 'lib/gilt/product.rb', line 107

def colors
  skus.map {|sku| sku.attributes[:color] }.uniq
end

#contentObject



76
77
78
79
# File 'lib/gilt/product.rb', line 76

def content
  keys = [:description, :fit_notes, :material, :care_instructions, :origin]
  Hash[keys.map {|content| [content, self.send(content) ]}]
end

#descriptionObject



56
57
58
# File 'lib/gilt/product.rb', line 56

def description
  fetch_content :description
end

#fit_notesObject



60
61
62
# File 'lib/gilt/product.rb', line 60

def fit_notes
  fetch_content :fit_notes
end

#for_sale?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/gilt/product.rb', line 144

def for_sale?
  inventory_status == Gilt::Sku::FOR_SALE
end

#format_priceObject



97
98
99
100
101
# File 'lib/gilt/product.rb', line 97

def format_price
  range = price_range
  return range.first.format if range.first == range.last
  price_range.map(&:format).join(" - ")
end

#idObject



41
42
43
# File 'lib/gilt/product.rb', line 41

def id
  @product["id"].to_i
end

#imagesObject



103
104
105
# File 'lib/gilt/product.rb', line 103

def images
  @product["image_urls"]
end

#inventory_statusObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/gilt/product.rb', line 133

def inventory_status
  sku_inventory = skus.map(&:inventory_status).uniq
  if sku_inventory.include? Gilt::Sku::FOR_SALE
    Gilt::Sku::FOR_SALE
  elsif sku_inventory.all? {|status| status == Gilt::Sku::RESERVED}
    Gilt::Sku::RESERVED
  else
    Gilt::Sku::SOLD_OUT
  end
end

#materialObject



64
65
66
# File 'lib/gilt/product.rb', line 64

def material
  fetch_content :material
end

#max_priceObject



89
90
91
# File 'lib/gilt/product.rb', line 89

def max_price
  sorted_price.first
end

#min_priceObject



85
86
87
# File 'lib/gilt/product.rb', line 85

def min_price
  sorted_price.last
end

#nameObject



33
34
35
# File 'lib/gilt/product.rb', line 33

def name
  @product["name"]
end

#originObject



72
73
74
# File 'lib/gilt/product.rb', line 72

def origin
  fetch_content :origin
end

#price_rangeObject



93
94
95
# File 'lib/gilt/product.rb', line 93

def price_range
  [min_price, max_price]
end

#productObject



37
38
39
# File 'lib/gilt/product.rb', line 37

def product
  URI(@product["product"])
end

#reserved?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/gilt/product.rb', line 152

def reserved?
  inventory_status == Gilt::Sku::RESERVED
end

#select_sku(attributes) ⇒ Object



127
128
129
130
131
# File 'lib/gilt/product.rb', line 127

def select_sku(attributes)
  attribute_map = attributes.map {|k, v| skus_with_attribute(k, v).map(&:id) }
  ids = attribute_map.reduce(:&)
  skus.find {|sku| sku.id == ids.first } if ids.size > 0
end

#sizesObject



111
112
113
# File 'lib/gilt/product.rb', line 111

def sizes
  skus.map {|sku| sku.attributes[:size] }.uniq
end

#skusObject



81
82
83
# File 'lib/gilt/product.rb', line 81

def skus
  @product["skus"].map {|sku| Sku.new(sku) }
end

#skus_of_color(color) ⇒ Object



119
120
121
# File 'lib/gilt/product.rb', line 119

def skus_of_color(color)
  skus_with_attribute :color, color
end

#skus_of_size(size) ⇒ Object



115
116
117
# File 'lib/gilt/product.rb', line 115

def skus_of_size(size)
  skus_with_attribute :size, size
end

#skus_with_attribute(attribute, value) ⇒ Object



123
124
125
# File 'lib/gilt/product.rb', line 123

def skus_with_attribute(attribute, value)
  skus.select {|sku| !!sku.attributes[attribute.to_sym].match(value) }
end

#sold_out?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/gilt/product.rb', line 148

def sold_out?
  inventory_status == Gilt::Sku::SOLD_OUT
end

#urlObject



49
50
51
# File 'lib/gilt/product.rb', line 49

def url
  URI(@product["url"])
end