Class: Scalablepress::Product

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Product

Returns a new instance of Product.



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

def initialize(params)
  @name = params["name"]
  @product_id = params["id"]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/scalablepress/resources/product.rb', line 3

def name
  @name
end

#product_idObject (readonly)

Returns the value of attribute product_id.



4
5
6
# File 'lib/scalablepress/resources/product.rb', line 4

def product_id
  @product_id
end

Class Method Details

.allObject



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

def self.all
  Category.all.map {|c| c.products }.flatten
end

Instance Method Details

#colorsObject



16
17
18
19
# File 'lib/scalablepress/resources/product.rb', line 16

def colors
  @colors = product_info["colors"]
  @colors.map {|c| c["name"] }
end

#images_for_color(color) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/scalablepress/resources/product.rb', line 21

def images_for_color(color)
  matched_color = product_info["colors"].select {|c| c["name"] == color }.first
  raise "#{color} is invalid color selection for #{colors.join(", ")}" unless matched_color
  images = matched_color["images"]
  raise "Found no images for #{color}!" unless images.any?
  images.map {|x| x["url"] }
end

#product_infoObject



11
12
13
14
# File 'lib/scalablepress/resources/product.rb', line 11

def product_info
  return @product_info if @product_info
  @product_info = API.get("products/#{@product_id}")
end