Module: TipsanityMerchantExtractor::LinkShare

Included in:
Rakuten, TigerDirect
Defined in:
lib/tipsanity_merchant_extractor/linkshare.rb

Instance Method Summary collapse

Instance Method Details

#call_to_linkshare(merchant_url, token, mid) {|product| ... } ⇒ Object

Yields:

  • (product)


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

def call_to_linkshare(merchant_url, token, mid)
  product_name = extract_linkshare(merchant_url)
  linkshare_base_url = "http://productsearch.linksynergy.com/productsearch?"
  query_hash = {token: token, keyword: product_name, mid: mid, exact: product_name.split(" ").pop(2).join(" ")}
  query = URI.encode_www_form query_hash
  request_url = [linkshare_base_url, query].join
  response_xml = Nokogiri::XML open(URI.escape(request_url))
  unless response_xml.xpath("//Errors").empty?
    product = nil
  else
    product = response_xml.xpath("//result")
  end
  yield product
end

#extract_linkshare(merchant_url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tipsanity_merchant_extractor/linkshare.rb', line 18

def extract_linkshare(merchant_url)
  capitalized_product_name = nil
  if is_merchant_linkshare_rakuten?(merchant_url){}
    path = URI(URI.unescape merchant_url).path
    element_array = path.split("/")
    product_name = element_array[element_array.index("p")+1] if element_array.include?("p")
    product_name = element_array[element_array.index("prod")+1] if element_array.include?("prod")
    capitalized_product_name = product_name.gsub(/\d/,'').split('-').delete_if{|x| x==""}.collect{|x| x.capitalize}[0..6].join(" ")
    return capitalized_product_name
  end
end

#provide_buy_url(token, mid, merchant_url) ⇒ Object



29
30
31
32
# File 'lib/tipsanity_merchant_extractor/linkshare.rb', line 29

def provide_buy_url(token, mid, merchant_url)
  buy_url = "http://getdeeplink.linksynergy.com/createcustomlink.shtml?token=#{token}&mid=#{mid}&murl=#{merchant_url}"
  Nokogiri::HTML(open(buy_url)).search("p").text
end