Module: AmazonBundler

Defined in:
lib/amazon_bundler.rb,
lib/amazon_bundler/version.rb

Overview

Creates a one-click url to add a bundle of Amazon products to cart

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.asin(product) ⇒ Object



29
30
31
# File 'lib/amazon_bundler.rb', line 29

def asin(product)
  "ASIN.#{product[:index]}=#{product[:asin]}"
end

.base_urlObject



10
11
12
# File 'lib/amazon_bundler.rb', line 10

def base_url
  "https://www.amazon.com/gp/aws/cart/add.html?"
end

.convert_product_to_url(product) ⇒ Object



25
26
27
# File 'lib/amazon_bundler.rb', line 25

def convert_product_to_url(product)
  [asin(product), quantity(product), offer(product)].join("&")
end

.convert_products_array_to_url(product_array) ⇒ Object



20
21
22
23
# File 'lib/amazon_bundler.rb', line 20

def convert_products_array_to_url(product_array)
  product_array = products_with_index(product_array)
  product_array.map { |product| convert_product_to_url(product) }.join("&")
end


42
43
44
45
46
47
# File 'lib/amazon_bundler.rb', line 42

def create_bundle_link(product_array: [], affiliate_id: nil)
  ErrorChecking.product_array_valid?(product_array)
  affiliate_info = affiliate_id ? "AssociateTag=#{affiliate_id}&" : ""
  products_url = convert_products_array_to_url(product_array)
  "#{base_url}#{affiliate_info}#{products_url}"
end

.offer(product) ⇒ Object



37
38
39
40
# File 'lib/amazon_bundler.rb', line 37

def offer(product)
  offer_id = product[:offer_id].to_s
  offer_id.empty? ? "" : "OfferListingId.#{product[:index]}=#{offer_id}"
end

.products_with_index(product_array) ⇒ Object



14
15
16
17
18
# File 'lib/amazon_bundler.rb', line 14

def products_with_index(product_array)
  product_array.each_with_index do |product, index|
    product[:index] = index + 1
  end
end

.quantity(product) ⇒ Object



33
34
35
# File 'lib/amazon_bundler.rb', line 33

def quantity(product)
  "Quantity.#{product[:index]}=#{product[:quantity]}"
end