Class: VersacommerceAPI::Product

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metafieldable

#add_metafield, #metafields

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.



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

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

Class Method Details

.categoriesObject



82
83
84
# File 'lib/versacommerce_api/resources/product.rb', line 82

def self.categories
  get(:categories)
end

Instance Method Details

#amount_available(amount) ⇒ Object



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

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

#availableObject



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

def available
  amount_available(0) && active
end

#carriersObject



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

def carriers
  associated_resource "carrier"
end


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

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

#is_variantObject



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

def is_variant
  !product_id.nil?
end

#price_rangeObject

compute the price range



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

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



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

def product_images
  associated_resource "product_image"
end

#propertiesObject



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

def properties
   associated_resource "property"
end

#tagsObject



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

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

#tax_for_country(country = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/versacommerce_api/resources/product.rb', line 70

def tax_for_country country = nil
  begin
    if country.present?
      get(:tax_for_country, country: country)
    else
      get(:tax_for_country) # Retrieve tax for default shipping country
    end
  rescue ActiveResource::ResourceNotFound
    return nil
  end
end

#variantsObject



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

def variants
  associated_resource "variant"
end