class MetaHari::Spyglass::Base

Attributes

iteration[R]
uri[R]

Public Class Methods

new(uri, iteration = 0) click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 13
def initialize(uri, iteration = 0)
  @uri       = uri
  @iteration = iteration
end
suitable?(uri) click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 9
def self.suitable?(uri)
  fail StandardError.new, "not implemented for '#{uri.host}'"
end

Public Instance Methods

spy() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 18
def spy
  spy_list.map { |method| send method }
    .inject(MetaHari::Product.new 'uri' => uri) { |a, e| a.apply e }
end

Protected Instance Methods

document() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 60
def document
  @document ||= Nokogiri::HTML fetch_data
end
fetch_data() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 48
def fetch_data
  return @_data if @_data
  case res = fetch_response
  when Net::HTTPSuccess
    @_data = res.body
  when Net::HTTPRedirection
    notification = RedirectNotification.new res['location'], iteration + 1
    fail notification, 'Redirection'
  else res.error!
  end
end
fetch_request() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 37
def fetch_request
  path = uri.path.empty? ? '/' : uri.path
  Net::HTTP::Get.new path, 'User-Agent' => user_agent
end
fetch_response() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 42
def fetch_response
  Net::HTTP.start uri.host, uri.port do |http|
    http.request fetch_request
  end
end
spy_json_ld() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 64
def spy_json_ld
  json_ld = MetaHari::Helpers::JsonLd.new(document)
  json_ld.data
end
spy_list() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 25
def spy_list
  %i(spy_json_ld spy_microdata spy_open_graph)
end
spy_microdata() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 69
def spy_microdata
  microdata = MetaHari::Helpers::Microdata.new(document, uri.to_s)
  microdata.data
end
spy_open_graph() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 74
def spy_open_graph
  open_graph = MetaHari::Helpers::OpenGraph.new(document)
  open_graph.data
end
user_agent() click to toggle source
# File lib/meta_hari/spyglass/base.rb, line 29
def user_agent
  [
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5)',
    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125',
    'Safari/537.36'
  ].join(' ')
end