Class: ShopzillaAPI

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

Constant Summary collapse

@@default_api_call_timeout =
25

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShopzillaAPI

Returns a new instance of ShopzillaAPI.



12
13
14
# File 'lib/api_helpers/shopzilla_api.rb', line 12

def initialize
  @logger = Logger.new(STDERR)
end

Instance Attribute Details

#api_call_timeoutObject

Returns the value of attribute api_call_timeout.



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

def api_call_timeout
  @api_call_timeout
end

Class Method Details

.default_api_call_timeout=(obj) ⇒ Object



7
8
9
# File 'lib/api_helpers/shopzilla_api.rb', line 7

def self.default_api_call_timeout=(obj)
  @@default_api_call_timeout = obj
end

Instance Method Details

#find_offers_by_product_id(product_id) ⇒ Object

Find all offers for a product given the (shopzilla) product_id



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/api_helpers/shopzilla_api.rb', line 17

def find_offers_by_product_id(product_id)
  result = make_offer_service_request(product_id)
  offers = {}
  unless result.nil?
    merchant_offers = result / 'Products/Product/Offers/Offer'
    merchant_offers.each do |offer|
      merchant_id = offer.attributes['merchantId']
      merchant_name = safe_inner_text(offer.at('merchantName'))
      merchant_rating_elem = offer.at('MerchantRating')
      merchant_rating = normalize_merchant_rating(merchant_rating_elem.attributes['value'].to_f) unless merchant_rating_elem.nil?
      price = get_price_from_node(offer.at('price'))
      shipping = get_price_from_node(offer.at('shipAmount'))
      url = safe_inner_text(offer.at('url'))
      condition = safe_inner_text(offer.at('condition'))
      stock = safe_inner_text(offer.at('stock'))
      if is_likely_new_condition?(condition) && is_likely_in_stock?(stock)
        offers[merchant_id] = { :merchant_code => merchant_id,
                                :merchant_name => safe_unescape_html(merchant_name),
                                :merchant_logo_url => "http://img.bizrate.com/merchant/#{merchant_id}.gif",
                                :cpc => nil,
                                :price => price,
                                :shipping => shipping,
                                :offer_url => safe_unescape_html(url),
                                :offer_tier => 1,
                                :merchant_rating => merchant_rating,
                                :num_merchant_reviews => nil }
      end
    end
  end
  offers.values
end

#logo_url(merchant_id) ⇒ Object



107
108
109
# File 'lib/api_helpers/shopzilla_api.rb', line 107

def logo_url(merchant_id)
  "http://img.bizrate.com/merchant/#{merchant_id}.gif"
end

#merchant_source_detail(merchant_id) ⇒ Object

This method makes a separate API call to get merchant detail info. It returns a hash that is aligned with the merchant_source model



55
56
57
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
# File 'lib/api_helpers/shopzilla_api.rb', line 55

def merchant_source_detail(merchant_id)
  result = make_merchant_service_request(merchant_id)
  return nil if result.nil?
  merchant_element = result / ("Merchants/Merchant[@id=#{merchant_id}]")
  merchant_source = {}
  merchant_source[:source] = 'shopzilla'
  merchant_source[:code] = merchant_id.to_s
  merchant_source[:name] = safe_inner_text(merchant_element.at('name'))
  logo_url = logo_url(merchant_id)
  if verify_logo_url(logo_url)
    merchant_source[:logo_url] = logo_url
  end

  # rating will not exist if unrated (although "unrated" will -- doh!)
  rated = false
  begin
    rating_elem = merchant_element.at('/Rating/Overall')
    unless rating_elem.nil?
      rated = true
      merchant_source[:merchant_rating] = normalize_merchant_rating(rating_elem.attributes['value'].to_f)
    end
  rescue
    merchant_source[:merchant_rating] = 0
  end
  # There are about 38 ways this could fail, so rescue any baddies
  begin
    # URL provided is an entity escaped url to shopzilla/bizrate for example
    # http://www.bizrate.com/rd?t=http%3A%2F%2Fwww.pcnation.com%2Fasp%2Findex.asp%3Faffid%3D308&mid=31427&cat_id=&prod_id=350513557&oid=&pos=1&b_id=18&rf=af1&af_id=3973&af_creative_id=6&af_assettype_id=10&af_placement_id=1
    # http://www.bizrate.com/rd?t=http%3A%2F%2Fad.doubleclick.net%2Fclk%3B23623113%3B12119329%3Bs%3Fhttp%3A%2F%2Fwww.staples.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fhome%3FstoreId%3D10001%26langId%3D-1%26cm_mmc%3Donline_bizrate-_-search-_-staples_brand-_-staples.com&mid=370&cat_id=&prod_id=&oid=&pos=1&b_id=18&rf=af1&af_id=3973&af_creative_id=6&af_assettype_id=10&af_placement_id=1
    # http://www.bizrate.com/rd?t=http%3A%2F%2Fwww.tigerdirect.com%2Findex.asp%3FSRCCODE%3DBIZRATE&mid=23939&cat_id=&prod_id=&oid=&pos=1&b_id=18&rf=af1&af_id=3973&af_creative_id=6&af_assettype_id=10&af_placement_id=1
    # http://www.bizrate.com/rd?t=http%3A%2F%2Fad.doubleclick.net%2Fclk%3BNEW_1%3B6928611%3Ba%3Fhttp%3A%2F%2Fwww.officedepot.com&mid=814&cat_id=&prod_id=&oid=&pos=1&b_id=18&rf=af1&af_id=3973&af_creative_id=6&af_assettype_id=10&af_placement_id=1
    redir_url = CGI::unescape(merchant_element.at('url').inner_text)
    # the query string will contain a value at the "t" parameter
    t_param_value = redir_url.match(/(\?|&)t=(.+)/)[2]
    if t_param_value.index('doubleclick').nil?
      homepage = t_param_value.match(/https?:\/\/(.+?)(\/|&|\?|$)/)[1]
    else
      homepage = t_param_value.match(/.+https?:\/\/(.+?)(\/|&|\?|$)/)[1]
    end
    merchant_source[:homepage] = "http://#{homepage}/"
  rescue
    merchant_source[:homepage] = nil
  end
  # now, we just need the number of reviews
  if rated
    num_merchant_reviews = safe_inner_text(merchant_element.at('Details/surveyCount'))
    num_merchant_reviews = (num_merchant_reviews.nil? || num_merchant_reviews.empty?) ? 0 : num_merchant_reviews.to_i
    merchant_source[:num_merchant_reviews] = num_merchant_reviews
  end
  merchant_source
end

#normalize_merchant_rating(merchant_rating) ⇒ Object



49
50
51
# File 'lib/api_helpers/shopzilla_api.rb', line 49

def normalize_merchant_rating(merchant_rating)
  merchant_rating.nil? ? nil : (merchant_rating * 10.0).round
end

#verified_logo_url(merchant_id) ⇒ Object



111
112
113
114
# File 'lib/api_helpers/shopzilla_api.rb', line 111

def verified_logo_url(merchant_id)
  logo_url = logo_url(merchant_id)
  verify_logo_url(logo_url) ? logo_url : nil
end