Class: ShopzillaSource

Inherits:
Source
  • Object
show all
Defined in:
lib/sources/shopzilla_source.rb

Constant Summary collapse

SHOPZILLA_SEARCH_PAGE =
'http://www.bizrate.com/ratings_guide/guide.html'
SHOPZILLA_SEARCH_ACTION =
'http://www.bizrate.com/merchant/results.xpml'

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

Instance Method Summary collapse

Methods inherited from Source

affiliate_sources, #eql?, #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

#initializeShopzillaSource

Returns a new instance of ShopzillaSource.



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

def initialize
  super(:name => 'Shopzilla',
        :homepage => 'http://www.shopzilla.com/',
        :cpc => 39,
        :offer_enabled => false,
        :offer_ttl_seconds => 1800,
        :use_for_merchant_ratings => true,
        :offer_affiliate => false,
        :supports_lifetime_ratings => false,
        :batch_fetch_delay => 1,
        :product_code_regexp => /^\d{7,11}$/,
        :product_code_examples => ['1028968032', '852926140'],
        :product_page_link_erb => "http://www.shopzilla.com/-/<%= product_code %>/shop")
end

Instance Method Details

#add_merchant_source_from_store(merchant_sources, store) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sources/shopzilla_source.rb', line 81

def add_merchant_source_from_store(merchant_sources, store)
  name = store.text
  merchant_code = CGI.parse(URI.parse(store['href']).query)['mid']
  logo_url = api.verified_logo_url(merchant_code)
  existing_merchant_source = MerchantSource.find_by_source_and_code(self, merchant_code)
  if existing_merchant_source.nil?
    merchant_sources << OpenStruct.new({:source => self, :name => name, :code => merchant_code, :logo_url => logo_url})
  else
    merchant_sources << existing_merchant_source
  end
end

#apiObject



101
102
103
# File 'lib/sources/shopzilla_source.rb', line 101

def api
  @api ||= ShopzillaAPI.new
end

#code_from_merchant_source_page_url(merchant_source_page_url) ⇒ Object



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

def code_from_merchant_source_page_url(merchant_source_page_url)
  merchant_source_page_url.match /6[A-Z](\-\-.*)?_\-_mid\-\-(\d+)/i
  $2
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
45
46
# File 'lib/sources/shopzilla_source.rb', line 29

def fetch_merchant_source(merchant_source_page_url)
  delay_fetch
  merchant_source = OpenStruct.new
  merchant_source.source = self

  merchant_code = code_from_merchant_source_page_url(merchant_source_page_url)
  merchant_source_detail = api.merchant_source_detail(merchant_code)

  unless merchant_source_detail.nil?
    merchant_source.code = merchant_source_detail[:code]
    merchant_source.name = merchant_source_detail[:name]
    merchant_source.logo_url = merchant_source_detail[:logo_url]
    merchant_source.merchant_rating = merchant_source_detail[:merchant_rating]
    merchant_source.homepage = merchant_source_detail[:homepage]
    merchant_source.num_merchant_reviews = merchant_source_detail[:num_merchant_reviews]
  end
  merchant_source
end

#fetch_offers(product_code) ⇒ Object



105
106
107
# File 'lib/sources/shopzilla_source.rb', line 105

def fetch_offers(product_code)
  api.find_offers_by_product_id(product_code)
end

#format_rating(merchant_source) ⇒ Object



93
94
95
# File 'lib/sources/shopzilla_source.rb', line 93

def format_rating(merchant_source)
  '%01.1f/10' % (merchant_source.get_merchant_rating.to_f / 10.0)
end

#nullify_offer_url(offer_url) ⇒ Object



97
98
99
# File 'lib/sources/shopzilla_source.rb', line 97

def nullify_offer_url(offer_url)
  offer_url.gsub(/af_id=3973/, 'af_id=3233')
end

#search_for_merchant_source(search_text) ⇒ Object



50
51
52
53
54
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
# File 'lib/sources/shopzilla_source.rb', line 50

def search_for_merchant_source(search_text)
  merchant_sources = []

  agent = WWW::Mechanize.new
  agent.html_parser = Nokogiri::HTML
  agent.user_agent_alias = 'Windows IE 7'
  agent.follow_meta_refresh = true

  search_page = agent.get(SHOPZILLA_SEARCH_PAGE)
  if form = search_page.form_with(:action => /superfind/)
    # Must switch the action given in the form, because BizRate does exactly this in JavaScript
    form.action = SHOPZILLA_SEARCH_ACTION
    form['SEARCH_GO'] = 'Find it!'
    form.keyword = search_text
    if result = form.submit
      if single_store = result.at('table[id="merchant_overview"]')
        if store = single_store.at('div[class="certified"] strong a')
          add_merchant_source_from_store(merchant_sources, store)
        end
      elsif stores_rated_list = result.at('div[class="storesRatedList"]')
        if stores = stores_rated_list.search('th a')
          stores.each do |store|
            add_merchant_source_from_store(merchant_sources, store)
          end
        end
      end
    end
  end
  merchant_sources
end

#url_for_merchant_source_page(merchant_source_code) ⇒ Object



20
21
22
# File 'lib/sources/shopzilla_source.rb', line 20

def url_for_merchant_source_page(merchant_source_code)
  "http://www.shopzilla.com/6E_-_mid--#{merchant_source_code}"
end