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.



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

def initialize(product_body)
  @product = product_body
end

Class Method Details

.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



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

def brand
  @product["brand"]
end

#care_instructionsObject



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

def care_instructions
  fetch_content :care_instructions
end

#colorsObject



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

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

#contentObject



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

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

#descriptionObject



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

def description
  fetch_content :description
end

#fit_notesObject



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

def fit_notes
  fetch_content :fit_notes
end

#for_sale?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/gilt/product.rb', line 136

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

#format_priceObject



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

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

#idObject



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

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

#imagesObject



95
96
97
# File 'lib/gilt/product.rb', line 95

def images
  @product["image_urls"]
end

#inventory_statusObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/gilt/product.rb', line 125

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



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

def material
  fetch_content :material
end

#max_priceObject



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

def max_price
  sorted_price.first
end

#min_priceObject



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

def min_price
  sorted_price.last
end

#nameObject



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

def name
  @product["name"]
end

#originObject



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

def origin
  fetch_content :origin
end

#price_rangeObject



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

def price_range
  [min_price, max_price]
end

#productObject



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

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

#reserved?Boolean

Returns:

  • (Boolean)


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

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

#select_sku(attributes) ⇒ Object



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

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



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

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

#skusObject



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

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

#skus_of_color(color) ⇒ Object



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

def skus_of_color(color)
  skus_with_attribute :color, color
end

#skus_of_size(size) ⇒ Object



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

def skus_of_size(size)
  skus_with_attribute :size, size
end

#skus_with_attribute(attribute, value) ⇒ Object



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

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

#sold_out?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/gilt/product.rb', line 140

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

#urlObject



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

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