Class: Relevance::Tarantula::Link
Constant Summary
collapse
- METHOD_REGEXPS =
{}
VERSION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#log, #rails_root, #tarantula_home, #verbose
Constructor Details
#initialize(link, crawler, referrer) ⇒ Link
Returns a new instance of Link.
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/relevance/tarantula/link.rb', line 44
def initialize(link, crawler, referrer)
@crawler, @referrer = crawler, referrer
if String === link || link.nil?
@href = transform_url(link)
@method = 'get'
else @href = link['href'] ? transform_url(link['href']) : nil
@tag = link
end
end
|
Instance Attribute Details
Returns the value of attribute crawler.
42
43
44
|
# File 'lib/relevance/tarantula/link.rb', line 42
def crawler
@crawler
end
|
Returns the value of attribute href.
42
43
44
|
# File 'lib/relevance/tarantula/link.rb', line 42
def href
@href
end
|
Returns the value of attribute referrer.
42
43
44
|
# File 'lib/relevance/tarantula/link.rb', line 42
def referrer
@referrer
end
|
Class Method Details
.method_javascript_function(method, url = '', href = nil) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/relevance/tarantula/link.rb', line 14
def method_javascript_function(method, url = '', href = nil)
action = (href && url.size > 0) ? "'#{url}'" : 'this.href'
submit_function =
"var f = document.createElement('form'); f.style.display = 'none'; " +
"this.parentNode.appendChild(f); f.method = 'POST'; f.action = #{action};"
unless method == 'post'
submit_function << "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); "
submit_function << "m.setAttribute('name', '_method'); m.setAttribute('value', '#{method}'); f.appendChild(m);"
end
if protect_against_forgery?
submit_function << "var s = document.createElement('input'); s.setAttribute('type', 'hidden'); "
submit_function << "s.setAttribute('name', '#{request_forgery_protection_token}'); s.setAttribute('value', '#{escape_javascript form_authenticity_token}'); f.appendChild(s);"
end
submit_function << "f.submit();"
end
|
.protect_against_forgery? ⇒ Boolean
method_javascript_function needs this method
10
11
12
|
# File 'lib/relevance/tarantula/link.rb', line 10
def protect_against_forgery?
false
end
|
Instance Method Details
#==(obj) ⇒ Object
Also known as:
eql?
88
89
90
91
|
# File 'lib/relevance/tarantula/link.rb', line 88
def ==(obj)
obj.respond_to?(:href) && obj.respond_to?(:method) &&
self.href.to_s == obj.href.to_s && self.meth.to_s == obj.meth.to_s
end
|
56
57
58
59
60
|
# File 'lib/relevance/tarantula/link.rb', line 56
def crawl
response = crawler.follow(meth, href)
log "Response #{response.code} for #{self}"
crawler.handle_link_results(self, make_result(response))
end
|
94
95
96
|
# File 'lib/relevance/tarantula/link.rb', line 94
def hash
to_s.hash
end
|
#make_result(response) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/relevance/tarantula/link.rb', line 62
def make_result(response)
crawler.make_result(:method => meth,
:url => href,
:response => response,
:referrer => referrer)
end
|
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/relevance/tarantula/link.rb', line 69
def meth
@meth ||= begin
(@tag &&
%w(put delete post patch).detect do |m| @tag['onclick'] =~ METHOD_REGEXPS[m] ||
@tag['data-method'] == m
end) ||
'get'
end
end
|
98
99
100
|
# File 'lib/relevance/tarantula/link.rb', line 98
def to_s
"<Relevance::Tarantula::Link href=#{href}, method=#{meth}>"
end
|
84
85
86
|
# File 'lib/relevance/tarantula/link.rb', line 84
def transform_url(link)
crawler.transform_url(link)
end
|
80
81
82
|
# File 'lib/relevance/tarantula/link.rb', line 80
def url
href
end
|