Class: AmazonSource

Inherits:
Source show all
Defined in:
lib/sources/amazon_source.rb

Constant Summary collapse

'amazon'

Constants inherited from Source

Source::SIMPLE_SOURCES_YAML_FILE

Instance Attribute Summary

Attributes inherited from Source

#batch_fetch_delay, #cpc, #for_product_info, #for_review_aggregates, #homepage, #mappable, #name, #offer_affiliate, #offer_enabled, #offer_ttl_seconds, #product_code_examples, #product_code_regexp, #product_page_link_erb, #search_token_separator, #search_url, #supports_lifetime_ratings, #use_for_merchant_ratings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Source

affiliate_sources, #code_from_merchant_source_page_url, #eql?, #format_rating, #hash, inherited, #keyname, keyname, keyname=, merchant_rating_sources, method_missing, offer_sources, #product_code_valid?, #product_page_link, source, sources, #to_s, #url_for_merchant_source_page_alt

Constructor Details

#initializeAmazonSource

Returns a new instance of AmazonSource.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sources/amazon_source.rb', line 6

def initialize
  super(:name => 'Amazon',
        :homepage => 'http://www.amazon.com/',
        :cpc => 12,
        :offer_enabled => true,
        :offer_ttl_seconds => 3600,
        :use_for_merchant_ratings => true,
        :offer_affiliate => true,
        :supports_lifetime_ratings => false,
        :batch_fetch_delay => 2,
        :product_code_regexp => /^[a-zA-Z0-9]{10}$/,
        :product_code_examples => ['B000HEC7BO', 'B002YP45EQ'],
        :product_page_link_erb => "http://www.amazon.com/gp/product/<%= product_code %>")
end

Class Method Details

.nullify_offer_url(offer_url) ⇒ Object



67
68
69
# File 'lib/sources/amazon_source.rb', line 67

def self.nullify_offer_url(offer_url)
  offer_url.gsub(/#{AMAZON_ASSOCIATE_TAG}/, AMAZON_ASSOCIATE_TAG_ALT)
end

Instance Method Details

#affiliate_wrap_deal_url(deal_url, nullify = false) ⇒ Object



79
80
81
# File 'lib/sources/amazon_source.rb', line 79

def affiliate_wrap_deal_url(deal_url, nullify=false)
  nullify ? nullify_offer_url(deal_url) : deal_url
end

#apiObject



21
22
23
# File 'lib/sources/amazon_source.rb', line 21

def api
  @api ||= Amazon::ProductAdvertising.new
end

#fetch_best_offer(product_code, min_num_offers_to_qualify = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sources/amazon_source.rb', line 46

def fetch_best_offer(product_code, min_num_offers_to_qualify=nil)
  delay_fetch
  offers = fetch_offers(product_code)
  if !min_num_offers_to_qualify.nil? && offers.length < min_num_offers_to_qualify
    return nil
  end
  offers.inject(nil) do |best_offer, offer|
    unless offer.price.nil? || offer.shipping.nil?
      if best_offer.nil? || (offer.price + offer.shipping) < (best_offer.price + best_offer.shipping)
        best_offer = offer
      end
    end
    best_offer
  end
end

#fetch_merchant_source(merchant_source_page_url) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sources/amazon_source.rb', line 29

def fetch_merchant_source(merchant_source_page_url)
  amazon_merchant_code = merchant_source_page_url
  if merchant_source_page_url.match /(\?|&)seller=(A.+?)(&.*|$)/
    amazon_merchant_code = $2
  end
  delay_fetch
  properties = api.seller_lookup(amazon_merchant_code)
  
  { :source => self,
    :code => properties[:seller_id],
    :name => properties[:merchant_name],
    :merchant_rating => properties[:average_feedback_rating] * 20.0,
    :num_merchant_reviews => properties[:total_feedback],
    :logo_url => properties[:logo_url],
    :homepage => properties[:homepage] }
end

#fetch_offers(product_code) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sources/amazon_source.rb', line 83

def fetch_offers(product_code)
  begin
    api.find_offers_by_asin(product_code).values
  rescue Amazon::AsinNotFoundError => ex
    raise Source::ProductNotFoundError.new(ex.message << " w/ #{product_code}", keyname, product_code)
  rescue Amazon::AsinFatalError => ex
    raise Source::ProductFatalError.new(ex.message << " w/ #{product_code}", keyname, product_code)
  rescue => ex
    raise Source::GeneralError.new(ex.message << " w/ #{product_code}", keyname)
  end
end

#fetch_street_price(product_code) ⇒ Object



62
63
64
65
# File 'lib/sources/amazon_source.rb', line 62

def fetch_street_price(product_code)
  best_offer = fetch_best_offer(product_code, 3)
  best_offer.nil? ? nil : best_offer.total_price
end

#nullify_offer_url(offer_url) ⇒ Object



71
72
73
# File 'lib/sources/amazon_source.rb', line 71

def nullify_offer_url(offer_url)
  AmazonSource.nullify_offer_url(offer_url)
end

#offer_affiliate_for_merchant?(merchant) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/sources/amazon_source.rb', line 75

def offer_affiliate_for_merchant?(merchant)
  !merchant.nil? && merchant.permalink == AMAZON_MERCHANT_PERMALINK
end

#url_for_merchant_source_page(merchant_source_code) ⇒ Object



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

def url_for_merchant_source_page(merchant_source_code)
  api.at_a_glance_url(merchant_source_code)
end