Class: Onebox::Engine::AmazonOnebox

Inherits:
Object
  • Object
show all
Includes:
Onebox::Engine, HTML, LayoutSupport
Defined in:
lib/onebox/engine/amazon_onebox.rb

Constant Summary

Constants included from Onebox::Engine

DEFAULT

Instance Attribute Summary

Attributes included from Onebox::Engine

#errors, #options, #timeout, #uri

Instance Method Summary collapse

Methods included from LayoutSupport

#layout, max_text, #to_html

Methods included from Onebox::Engine

all_iframe_origins, engines, included, #initialize, origins_to_regexes, #placeholder_html, #to_html

Instance Method Details

#http_paramsObject



43
44
45
46
47
# File 'lib/onebox/engine/amazon_onebox.rb', line 43

def http_params
  if @options && @options[:user_agent]
    { 'User-Agent' => @options[:user_agent] }
  end
end

#tldObject



39
40
41
# File 'lib/onebox/engine/amazon_onebox.rb', line 39

def tld
  @tld ||= @@matcher.match(@url)["tld"]
end

#urlObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/onebox/engine/amazon_onebox.rb', line 16

def url
  @raw ||= nil

  # If possible, fetch the cached HTML body immediately so we can
  # try to grab the canonical URL from that document,
  # rather than guess at the best URL structure to use
  if !@raw && has_cached_body
    @raw = Onebox::Helpers.fetch_html_doc(@url, http_params, body_cacher)
  end

  if @raw
    canonical_link = @raw.at('//link[@rel="canonical"]/@href')
    return canonical_link.to_s if canonical_link
  end

  if match && match[:id]
    id = Addressable::URI.encode_component(match[:id], Addressable::URI::CharacterClasses::PATH)
    return "https://www.amazon.#{tld}/dp/#{id}"
  end

  @url
end