Class: Amazon::ProductAdvertising

Inherits:
Object
  • Object
show all
Defined in:
lib/api_helpers/amazon.rb

Instance Method Summary collapse

Instance Method Details

#accessories_url(asin) ⇒ Object



40
41
42
43
# File 'lib/api_helpers/amazon.rb', line 40

def accessories_url(asin)
  accessories_url = "http://www.amazon.com/dp/accessories/#{asin}/#accessories"
  "http://www.amazon.com/gp/redirect.html?ie=UTF8&tag=#{associate_tag}&linkCode=ur2&camp=1789&creative=9325&location=#{CGI::escape(accessories_url)}"
end

#associate_tagObject



24
25
26
# File 'lib/api_helpers/amazon.rb', line 24

def associate_tag
  AMAZON_ASSOCIATE_TAG
end

#at_a_glance_url(seller_id) ⇒ Object



28
29
30
# File 'lib/api_helpers/amazon.rb', line 28

def at_a_glance_url(seller_id)
  "http://www.amazon.com/gp/help/seller/at-a-glance.html?seller=#{seller_id}"
end

#find_offers_by_asin(asin, featured_merchants_only = false) ⇒ Object



45
46
47
48
# File 'lib/api_helpers/amazon.rb', line 45

def find_offers_by_asin(asin, featured_merchants_only=false)
  # find_offers_by_asin_via_api(asin, featured_merchants_only)
  scrape_offer_listing_page_to_hash(asin, featured_merchants_only)
end

#find_product_by_asin(asin) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/api_helpers/amazon.rb', line 58

def find_product_by_asin(asin)
  request = {'Operation' => 'ItemLookup',
             'ResponseGroup' => 'Medium',
             'ItemId' => asin.strip,
             'IdType' => 'ASIN'}
  res = make_amazon_api_request request

  item = res / 'Items' / 'Item'
  asin = (item / 'ASIN').inner_html
  item_attributes = item / 'ItemAttributes'
  name = (item_attributes / 'Title').inner_html
  list_price = (item_attributes / 'ListPrice' / 'Amount').inner_html
  if list_price.nil? || list_price.empty?
    list_price = 0
  else
    list_price = (list_price.to_f / 100.0)
  end
  model = (item_attributes / 'Model').inner_html
  mpn = (item_attributes / 'MPN').inner_html
  upc = (item_attributes / 'UPC').inner_html
  manufacturer = (item_attributes / 'Manufacturer').inner_html
  
  features = (item_attributes / 'Feature').collect{|x| x.inner_html } # specifications
  
  editorial_reviews = (item / 'EditorialReviews').inject({}) {|ha, x| ha[(x / 'EditorialReview' / 'Source').inner_html] = (x / 'EditorialReview' / 'Content').inner_html; ha }
  
  begin
    small_image = {:url => (item.at('SmallImage') / 'URL').inner_html,
                   :width => (item.at('SmallImage') / 'Width').inner_html,
                   :height => (item.at('SmallImage') / 'Height').inner_html}
  rescue
    small_image = nil
  end
  begin
    medium_image = {:url => (item.at('MediumImage') / 'URL').inner_html,
                    :width => (item.at('MediumImage') / 'Width').inner_html,
                    :height => (item.at('MediumImage') / 'Height').inner_html}
  rescue
    medium_image = nil
  end
  begin
    large_image = {:url => (item.at('LargeImage') / 'URL').inner_html,
                   :width => (item.at('LargeImage') / 'Width').inner_html,
                   :height => (item.at('LargeImage') / 'Height').inner_html}
  rescue
    large_image = nil
  end

  product = {:asin => asin,
             :name => name,
             :list_price => list_price,
             :model => model,
             :mpn => mpn,
             :upc => upc,
             :manufacturer => manufacturer,
             :small_image => small_image,
             :medium_image => medium_image,
             :large_image => large_image,
             :features => features,
             :editorial_reviews => editorial_reviews}
  product
end

#find_product_review_info_by_asin_raw(asin) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/api_helpers/amazon.rb', line 50

def find_product_review_info_by_asin_raw(asin)
  request = {'Operation' => 'ItemLookup',
             'ResponseGroup' => 'Reviews',
             'ItemId' => asin.strip,
             'IdType' => 'ASIN'}
  make_amazon_api_request_raw request
end

#item_search(search_terms) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/api_helpers/amazon.rb', line 121

def item_search(search_terms)
  request = {'Operation' => 'ItemSearch',
             'Keywords' => search_terms,
             'SearchIndex' => 'All',
             'ResponseGroup' => 'Images,ItemAttributes'}
  res = make_amazon_api_request request
  products = []
  items = (res / 'Items' / 'Item')
  items.each do |item|
    begin
      small_image = item.at('SmallImage')
      if !small_image.nil?
        small_image_url = (small_image / 'URL').inner_html
      else
        small_image_url = ''
      end
      products << {
        :asin => (item / 'ASIN').inner_html,
        :name => (item / 'ItemAttributes' / 'Title').inner_html,
        :small_image_url => small_image_url
      }
    rescue
    end
  end
  products
end

#offer_listing_url(asin) ⇒ Object



36
37
38
# File 'lib/api_helpers/amazon.rb', line 36

def offer_listing_url(asin)
  "http://www.amazon.com/gp/offer-listing/#{asin}?condition=new"
end

#offer_url(asin, merchant_type, merchant_id) ⇒ Object



32
33
34
# File 'lib/api_helpers/amazon.rb', line 32

def offer_url(asin, merchant_type, merchant_id)
  "http://www.amazon.com/exec/obidos/ASIN/#{asin}/?#{merchant_type == 'seller' ? 'seller' : 'm'}=#{merchant_id}&tag=#{associate_tag}"
end

#seller_lookup(seller_id) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/api_helpers/amazon.rb', line 148

def seller_lookup(seller_id)
  request = { 'Operation' => 'SellerLookup',
              'SellerId' => seller_id }
  res = make_amazon_api_request request

  element = res.at('/SellerLookupResponse/Sellers/Seller/SellerName')
  if element.nil?
    element = res.at('/SellerLookupResponse/Sellers/Seller/Nickname')
  end
  if !element.nil?
    merchant_name = element.inner_text
  end
  begin
    details = scrape_at_a_glance_page(seller_id)
    logo_url = details[:logo_url]
    merchant_name = details[:merchant_name] if merchant_name.nil? || merchant_name.empty?
    homepage = details[:homepage]
  rescue
  end

  if merchant_name.nil? || merchant_name.empty?
    merchant_name = "Amazon merchant (#{seller_id})"
  end

  element = res.at('/SellerLookupResponse/Sellers/Seller/GlancePage')
  glance_page_url = element.inner_text unless element.nil?

  element = res.at('/SellerLookupResponse/Sellers/Seller/AverageFeedbackRating')
  average_feedback_rating = element.nil? ? 0.0 : element.inner_text.to_f

  element = res.at('/SellerLookupResponse/Sellers/Seller/TotalFeedback')
  total_feedback = element.nil? ? 0 : element.inner_text.to_i

  { :seller_id => seller_id,
    :merchant_name => merchant_name,
    :glance_page_url => glance_page_url,
    :average_feedback_rating => average_feedback_rating,
    :total_feedback => total_feedback,
    :logo_url => logo_url,
    :homepage => homepage }
end