Class: Urifetch::Strategy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/urifetch/strategy/base.rb

Direct Known Subclasses

Image

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, match_data, route_data = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
# 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

Instance Attribute Details

#match_dataObject (readonly)

Returns the value of attribute match_data.



7
8
9
# File 'lib/urifetch/strategy/base.rb', line 7

def match_data
  @match_data
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/urifetch/strategy/base.rb', line 7

def response
  @response
end

#route_dataObject (readonly)

Returns the value of attribute route_data.



7
8
9
# File 'lib/urifetch/strategy/base.rb', line 7

def route_data
  @route_data
end

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/urifetch/strategy/base.rb', line 7

def uri
  @uri
end

Instance Method Details

#execute!Object



16
17
18
19
20
# File 'lib/urifetch/strategy/base.rb', line 16

def execute!
  perform_request
  process_request if response.status == ["200","OK"]
  response
end

#perform_requestObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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_requestObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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