Class: Relevance::Tarantula::Link
- Extended by:
- ActionView::Helpers::UrlHelper
- Includes:
- Relevance::Tarantula
- Defined in:
- lib/relevance/tarantula/link.rb
Constant Summary collapse
- METHOD_REGEXPS =
{}
Constants included from Relevance::Tarantula
Instance Attribute Summary collapse
-
#crawler ⇒ Object
Returns the value of attribute crawler.
-
#href ⇒ Object
Returns the value of attribute href.
-
#referrer ⇒ Object
Returns the value of attribute referrer.
Class Method Summary collapse
-
.method_javascript_function(method, url = '', href = nil) ⇒ Object
fast fix for rails3.
-
.protect_against_forgery? ⇒ Boolean
method_javascript_function needs this method.
Instance Method Summary collapse
- #==(obj) ⇒ Object (also: #eql?)
- #crawl ⇒ Object
- #hash ⇒ Object
-
#initialize(link, crawler, referrer) ⇒ Link
constructor
A new instance of Link.
- #make_result(response) ⇒ Object
- #method ⇒ Object
- #to_s ⇒ Object
- #transform_url(link) ⇒ Object
Methods included from Relevance::Tarantula
#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 # should be a tag @href = link['href'] ? transform_url(link['href'].downcase) : nil @tag = link end end |
Instance Attribute Details
#crawler ⇒ Object
Returns the value of attribute crawler.
42 43 44 |
# File 'lib/relevance/tarantula/link.rb', line 42 def crawler @crawler end |
#href ⇒ Object
Returns the value of attribute href.
42 43 44 |
# File 'lib/relevance/tarantula/link.rb', line 42 def href @href end |
#referrer ⇒ Object
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
fast fix for rails3
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?
83 84 85 86 |
# File 'lib/relevance/tarantula/link.rb', line 83 def ==(obj) obj.respond_to?(:href) && obj.respond_to?(:method) && self.href.to_s == obj.href.to_s && self.method.to_s == obj.method.to_s end |
#crawl ⇒ Object
56 57 58 59 60 |
# File 'lib/relevance/tarantula/link.rb', line 56 def crawl response = crawler.follow(method, href) log "Response #{response.code} for #{self}" crawler.handle_link_results(self, make_result(response)) end |
#hash ⇒ Object
89 90 91 |
# File 'lib/relevance/tarantula/link.rb', line 89 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 => method, :url => href, :response => response, :referrer => referrer) end |
#method ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/relevance/tarantula/link.rb', line 69 def method @method ||= begin (@tag && [:put, :delete, :post].detect do |m| # post should be last since it's least specific @tag['onclick'] =~ METHOD_REGEXPS[m] end) || :get end end |
#to_s ⇒ Object
93 94 95 |
# File 'lib/relevance/tarantula/link.rb', line 93 def to_s "<Relevance::Tarantula::Link href=#{href}, method=#{method}>" end |
#transform_url(link) ⇒ Object
79 80 81 |
# File 'lib/relevance/tarantula/link.rb', line 79 def transform_url(link) crawler.transform_url(link) end |