class Urifetch::Strategy::Base

Attributes

match_data[R]
response[R]
route_data[R]
uri[R]

Public Class Methods

new(uri,match_data,route_data={}) click to toggle source
# File lib/urifetch/strategy/base.rb, line 9
def initialize(uri,match_data,route_data={})
  @uri = uri
  @match_data = match_data
  @route_data = route_data
  @response = Response.new(['0',''],route_data[:strategy_key],{})
end

Public Instance Methods

execute!() click to toggle source
# File lib/urifetch/strategy/base.rb, line 16
def execute!
  perform_request
  process_request if response.status == ["200","OK"]
  response
end
perform_request() click to toggle source
# File lib/urifetch/strategy/base.rb, line 22
def perform_request
  begin
    timeout(30) { @request  = open(@uri.to_s,'rb') }
    set_status  @request.status
  rescue OpenURI::HTTPError => error
    set_status error.message.split(" ",2)
  rescue SocketError => error
    set_status ["400","Bad Request"]
  rescue Errno::ENOENT => error
    set_status ["404","File not Found"]
  rescue Errno::ECONNREFUSED => error
    set_status ["401","Unauthorized"]
  rescue Errno::EADDRINUSE
    set_status ["401","Unauthorized"]
  rescue RuntimeError => error
    set_status ["400","Bad Request"]
  rescue Exception => e
    set_status ["500","Server Error",e]
  rescue TimeOutError
    set_status ["408","Request Timeout"]
  else
    set_status ["200","OK"]
  end
end
process_request() click to toggle source
# File lib/urifetch/strategy/base.rb, line 47
def process_request
          
  # Start by setting the URI
  set :url, uri.to_s
  
  doc = Nokogiri::HTML.parse(@request)
          
  # Open Auth data
  if og = OpenGraph.parse(doc)
    set :url,         og.url, override: true
    set :title,       og.title
    set :image,       og.image
    set :description, og.description
  end
          
  # Custom CSS data
  unless set? :title
    t = doc.css('title').first
    set :title, t.nil? ? match_data[0] : t.content.strip
  end
  
  favicon = doc.css('link[rel="shortcut icon"], link[rel="icon shortcut"], link[rel="shortcut"], link[rel="icon"]').first
  favicon = favicon.nil? ? nil : favicon['href'].strip
  if favicon
    if favicon.match(/^https?:\/\//i).nil?
      favicon = uri.scheme + "://" + uri.host.sub(/\/$/,"") + "/" + favicon.sub(/^\//,"")
    end
    set :favicon, favicon
  end
  
end

Private Instance Methods

get(key) click to toggle source
# File lib/urifetch/strategy/base.rb, line 85
def get(key)
  response.data[key.to_s]
end
set(key,value,args={}) click to toggle source
# File lib/urifetch/strategy/base.rb, line 89
def set(key,value,args={})
  response.data[key.to_s] = value if (args[:override] == true) or response.data[key.to_s].nil? unless value.nil?
end
set?(key) click to toggle source
# File lib/urifetch/strategy/base.rb, line 93
def set?(key)
  !response.data[key.to_s].nil?
end
set_status(status_array) click to toggle source
# File lib/urifetch/strategy/base.rb, line 81
def set_status(status_array)
  response.status = status_array
end