Module: ResellerRatingsAPI

Defined in:
lib/api_helpers/reseller_ratings_api.rb

Class Method Summary collapse

Class Method Details

.alt_code_from_merchant_source_page_url(merchant_source_page_url) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/api_helpers/reseller_ratings_api.rb', line 6

def self.alt_code_from_merchant_source_page_url(merchant_source_page_url)
  alt_code = nil
  if res = merchant_source_page_url.match(/resellerratings\.com\/store\/([^\/\?#]*)/)
    alt_code = res[1]
  end
  alt_code
end

.fetch_merchant_source(merchant_source_page_url) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/api_helpers/reseller_ratings_api.rb', line 63

def self.fetch_merchant_source(merchant_source_page_url)
  doc_and_final_uri = open_doc(merchant_source_page_url)
  if !doc_and_final_uri.nil?
    convert_merchant_page_to_merchant_source(doc_and_final_uri)
  else
    nil
  end
end

.fetch_merchant_source_by_alt_merchant_code(alt_merchant_code) ⇒ Object



72
73
74
75
# File 'lib/api_helpers/reseller_ratings_api.rb', line 72

def self.fetch_merchant_source_by_alt_merchant_code(alt_merchant_code)
  merchant_source_page_url = "http://www.resellerratings.com/store/#{alt_merchant_code}"
  fetch_merchant_source(merchant_source_page_url)
end

.fetch_suggestions(search_text, limit) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/api_helpers/reseller_ratings_api.rb', line 14

def self.fetch_suggestions(search_text, limit)
  rr_search_url = "http://www.resellerratings.com/reseller_list.pl?keyword_search=#{URI.escape(search_text)}"
  doc_and_final_uri = open_doc(rr_search_url)
  result = []
  unless doc_and_final_uri.nil? || doc_and_final_uri[:final_uri].nil? || doc_and_final_uri[:final_uri].empty?
    final_uri = URI.safe_parse(doc_and_final_uri[:final_uri])
    if final_uri.path == '/reseller_list.pl'
      # got the search results page with more than one result
      result = convert_search_results_page_to_merchant_array(doc_and_final_uri[:doc], limit)
    elsif final_uri.path.match(/^\/store\/(.+)$/)
      # got merchant page back
      result << { :merchant_page_url => final_uri, :merchant_code => $1, :merchant_name => $1.gsub('_', ' ') }
    else
      # don't know where we ended up
    end
  end
  result
end

.merchant_page_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/api_helpers/reseller_ratings_api.rb', line 77

def self.merchant_page_url?(url)
  !url.nil? && url.match(/\/store\/.+$/) != nil
end

.search_for_merchant_source(search_text, limit = 15) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/api_helpers/reseller_ratings_api.rb', line 33

def self.search_for_merchant_source(search_text, limit=15)
  merchant_sources = []
  rr_search_url = "http://www.resellerratings.com/reseller_list.pl?keyword_search=#{URI.escape(search_text)}"
  doc_and_final_uri = open_doc(rr_search_url)
  unless doc_and_final_uri.nil?
    if merchant_page_url?(doc_and_final_uri[:final_uri])
      merchant_sources << convert_merchant_page_to_merchant_source(doc_and_final_uri)
    else
      doc_and_final_uri[:doc].search('tr/td/font/a[text() = "Read Reviews"]/../../..').each do |result_row|
        element = result_row.at('td//a')
        name = element.inner_text.strip
        alt_merchant_code = element.attributes['href'].match(/\/store\/(.+)$/)[1]
        existing_merchant_source = MerchantSource.find_by_source_and_alt_code(Source.reseller_ratings_source, alt_merchant_code)
        if existing_merchant_source.nil?
          merchant_sources << OpenStruct.new({:source => Source.reseller_ratings_source, :name => name, :alt_code => alt_merchant_code})
        else
          merchant_sources << existing_merchant_source
        end
        break if merchant_sources.length >= limit
      end
    end
  end
  merchant_sources
end

.search_for_merchant_source_best_match(search_text) ⇒ Object



58
59
60
61
# File 'lib/api_helpers/reseller_ratings_api.rb', line 58

def self.search_for_merchant_source_best_match(search_text)
  rr_search_url = "http://www.resellerratings.com/reseller_list.pl?keyword_search=#{URI.escape(search_text)}"
  fetch_merchant_source(rr_search_url)
end