Class: EpinionsSource

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

Constant Summary

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?, #fetch_offers, #hash, inherited, #keyname, keyname, keyname=, merchant_rating_sources, method_missing, #nullify_offer_url, offer_sources, #product_code_valid?, #product_page_link, source, sources, #to_s, #url_for_merchant_source_page_alt

Constructor Details

#initializeEpinionsSource

Returns a new instance of EpinionsSource.



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

def initialize
  super(:name => 'Epinions',
        :homepage => 'http://www.epinions.com/',
        :cpc => 0,
        :offer_enabled => false,
        :offer_ttl_seconds => 0,
        :use_for_merchant_ratings => true,
        :offer_affiliate => false,
        :supports_lifetime_ratings => false,
        :batch_fetch_delay => 1,
        :product_code_regexp => /^\d{4,10}$/,
        :product_code_examples => ['44393573', '37469715'])
end

Instance Method Details

#code_from_merchant_source_page_url(merchant_source_page_url) ⇒ Object



22
23
24
# File 'lib/sources/epinions_source.rb', line 22

def code_from_merchant_source_page_url(merchant_source_page_url)
  merchant_source_page_url.match(/epinions\.com\/([^\/\?#]*)/)[1]
end

#fetch_merchant_source(merchant_source_page_url) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/sources/epinions_source.rb', line 26

def fetch_merchant_source(merchant_source_page_url)
  merchant_source_page_url.gsub!(/\/display_~.*$/, '')
  delay_fetch
  doc = Hpricot(open(merchant_source_page_url))

  merchant_source = OpenStruct.new
  merchant_source.source = self

  # merchant name
  element = doc.at('h1[@class = "title"]')
  unless element.nil?
    name = element.inner_text.strip
    merchant_source.name = name
  end

  # merchant logo
  element = doc.at('img[@name = "product_image"]')
  unless element.nil?
    logo_url = element.attributes['src']
    logo_url.gsub!(/-resized\d+/, '')
    merchant_source.logo_url = logo_url
  end

  # merchant code
  merchant_source.code = code_from_merchant_source_page_url(merchant_source_page_url)

  # merchant rating
  element = doc.at('span[text() *= "Overall store rating:"]/../img')
  element = doc.at('span[text() *= "Overall service rating:"]/../img') if element.nil?
  unless element.nil?
    merchant_rating = element.attributes['alt'].match(/Store Rating: ((\d|,)*\.?\d)/)[1]
    merchant_source.merchant_rating = merchant_rating.to_f * 20.0 unless merchant_rating.nil?
  end

  # Num Merchant Reviews
  element = doc.at('span[@class = "sgr"]')
  unless element.nil?
    num_merchant_reviews = element.inner_text.match(/Reviewed by (\d+) customer/)[1]
    merchant_source.num_merchant_reviews = num_merchant_reviews.delete(',').to_i unless num_merchant_reviews.nil? || num_merchant_reviews.empty?
  end

  # Homepage
  element = doc.at('span[text() = "Web Site"]../../td[2]/span/a')
  unless element.nil?
    homepage = element.inner_text.strip.downcase
    merchant_source.homepage = homepage
  end

  merchant_source
end

#format_rating(merchant_source) ⇒ Object



77
78
79
# File 'lib/sources/epinions_source.rb', line 77

def format_rating(merchant_source)
  '%01.1f/5.0' % (merchant_source.get_merchant_rating.to_f / 20.0)
end

#url_for_merchant_source_page(merchant_source_code) ⇒ Object



18
19
20
# File 'lib/sources/epinions_source.rb', line 18

def url_for_merchant_source_page(merchant_source_code)
  "http://www.epinions.com/#{merchant_source_code}"
end