Class: VersacommerceAPI::Product

Inherits:
Base show all
Includes:
Associatable
Defined in:
lib/versacommerce_api/resources/product.rb

Instance Method Summary collapse

Methods included from Associatable

#associated_resource

Methods inherited from Base

activate_session, all, clear_session, headers, root!

Methods included from Countable

#count

Methods inherited from ActiveResource::Base

build, delete, headers

Constructor Details

#initialize(attributes, persisted = false) ⇒ Product

Returns a new instance of Product.



7
8
9
10
11
12
13
14
15
# File 'lib/versacommerce_api/resources/product.rb', line 7

def initialize(attributes, persisted = false)
  super

  if respond_to?(:shipping_types) && self.shipping_types.present?
    self.shipping_types.map! do |shipping_type|
      Carrier.new(shipping_type.attributes)
    end
  end
end

Instance Method Details

#amount_available(amount) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/versacommerce_api/resources/product.rb', line 32

def amount_available(amount)
  if considers_stock
    stock >= amount
  else
    true
  end
end

#availableObject



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

def available
  amount_available(0) && active
end

#carriersObject



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

def carriers
  associated_resource "carrier"
end


65
66
67
# File 'lib/versacommerce_api/resources/product.rb', line 65

def featured_image
  ProductImage.new(:src => featured_image_url)
end

#is_variantObject



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

def is_variant
  !product_id.nil?
end

#price_rangeObject

compute the price range



18
19
20
21
22
23
24
25
26
# File 'lib/versacommerce_api/resources/product.rb', line 18

def price_range
  prices = variants.collect(&:price)
  format =  "%0.2f"
  if prices.min != prices.max
    "#{format % prices.min} - #{format % prices.max}"
  else
    format % prices.min
  end
end

#product_imagesObject



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

def product_images
  associated_resource "product_image"
end

#propertiesObject



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

def properties
   associated_resource "property"
end

#tagsObject



60
61
62
63
# File 'lib/versacommerce_api/resources/product.rb', line 60

def tags
  return [] if self.respond_to?("tag_list") && self.send("tag_list").blank?
  tag_list.split(",").map(&:strip)
end

#variantsObject



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

def variants
  associated_resource "variant"
end