Class: Gilt::Product
- Inherits:
-
Object
- Object
- Gilt::Product
- Defined in:
- lib/gilt/product.rb
Class Method Summary collapse
- .categories(apikey, affid = nil) ⇒ Object
- .client(apikey, affid = nil) ⇒ Object
- .create(product_id, apikey, affid = nil) ⇒ Object
- .defer(future) ⇒ Object
- .detail(product_id, apikey, affid = nil) ⇒ Object
Instance Method Summary collapse
- #brand ⇒ Object
- #care_instructions ⇒ Object
- #categories ⇒ Object
- #colors ⇒ Object
- #content ⇒ Object
- #description ⇒ Object
- #fit_notes ⇒ Object
- #for_sale? ⇒ Boolean
- #format_price ⇒ Object
- #id ⇒ Object
- #images ⇒ Object
-
#initialize(product_body) ⇒ Product
constructor
A new instance of Product.
- #inventory_status ⇒ Object
- #material ⇒ Object
- #max_price ⇒ Object
- #min_price ⇒ Object
- #name ⇒ Object
- #origin ⇒ Object
- #price_range ⇒ Object
- #product ⇒ Object
- #reserved? ⇒ Boolean
- #select_sku(attributes) ⇒ Object
- #sizes ⇒ Object
- #skus ⇒ Object
- #skus_of_color(color) ⇒ Object
- #skus_of_size(size) ⇒ Object
- #skus_with_attribute(attribute, value) ⇒ Object
- #sold_out? ⇒ Boolean
- #url ⇒ Object
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
#brand ⇒ Object
45 46 47 |
# File 'lib/gilt/product.rb', line 45 def brand @product["brand"] end |
#care_instructions ⇒ Object
68 69 70 |
# File 'lib/gilt/product.rb', line 68 def care_instructions fetch_content :care_instructions end |
#categories ⇒ Object
53 54 55 |
# File 'lib/gilt/product.rb', line 53 def categories @product["categories"] || [] end |
#colors ⇒ Object
107 108 109 |
# File 'lib/gilt/product.rb', line 107 def colors skus.map {|sku| sku.attributes[:color] }.uniq end |
#content ⇒ Object
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 |
#description ⇒ Object
56 57 58 |
# File 'lib/gilt/product.rb', line 56 def description fetch_content :description end |
#fit_notes ⇒ Object
60 61 62 |
# File 'lib/gilt/product.rb', line 60 def fit_notes fetch_content :fit_notes end |
#for_sale? ⇒ Boolean
144 145 146 |
# File 'lib/gilt/product.rb', line 144 def for_sale? inventory_status == Gilt::Sku::FOR_SALE end |
#format_price ⇒ Object
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 |
#id ⇒ Object
41 42 43 |
# File 'lib/gilt/product.rb', line 41 def id @product["id"].to_i end |
#images ⇒ Object
103 104 105 |
# File 'lib/gilt/product.rb', line 103 def images @product["image_urls"] end |
#inventory_status ⇒ Object
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 |
#material ⇒ Object
64 65 66 |
# File 'lib/gilt/product.rb', line 64 def material fetch_content :material end |
#max_price ⇒ Object
89 90 91 |
# File 'lib/gilt/product.rb', line 89 def max_price sorted_price.first end |
#min_price ⇒ Object
85 86 87 |
# File 'lib/gilt/product.rb', line 85 def min_price sorted_price.last end |
#name ⇒ Object
33 34 35 |
# File 'lib/gilt/product.rb', line 33 def name @product["name"] end |
#origin ⇒ Object
72 73 74 |
# File 'lib/gilt/product.rb', line 72 def origin fetch_content :origin end |
#price_range ⇒ Object
93 94 95 |
# File 'lib/gilt/product.rb', line 93 def price_range [min_price, max_price] end |
#product ⇒ Object
37 38 39 |
# File 'lib/gilt/product.rb', line 37 def product URI(@product["product"]) end |
#reserved? ⇒ 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 |
#sizes ⇒ Object
111 112 113 |
# File 'lib/gilt/product.rb', line 111 def sizes skus.map {|sku| sku.attributes[:size] }.uniq end |
#skus ⇒ Object
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
148 149 150 |
# File 'lib/gilt/product.rb', line 148 def sold_out? inventory_status == Gilt::Sku::SOLD_OUT end |
#url ⇒ Object
49 50 51 |
# File 'lib/gilt/product.rb', line 49 def url URI(@product["url"]) end |