Class: Relevance::Tarantula::Link

Inherits:
Object
  • Object
show all
Extended by:
ActionView::Helpers::UrlHelper
Includes:
Relevance::Tarantula
Defined in:
lib/relevance/tarantula/link.rb

Constant Summary collapse

METHOD_REGEXPS =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relevance::Tarantula

#log, #rails_root, #tarantula_home, #verbose

Constructor Details

#initialize(link, crawler, referrer) ⇒ Link

Returns a new instance of Link.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/relevance/tarantula/link.rb', line 41

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

#crawlerObject

Returns the value of attribute crawler.



39
40
41
# File 'lib/relevance/tarantula/link.rb', line 39

def crawler
  @crawler
end

#hrefObject

Returns the value of attribute href.



39
40
41
# File 'lib/relevance/tarantula/link.rb', line 39

def href
  @href
end

#referrerObject

Returns the value of attribute referrer.



39
40
41
# File 'lib/relevance/tarantula/link.rb', line 39

def referrer
  @referrer
end

Class Method Details

.method_javascript_function(method, url = '', href = nil) ⇒ Object

fast fix for rails3



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/relevance/tarantula/link.rb', line 11

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

Returns:

  • (Boolean)


7
8
9
# File 'lib/relevance/tarantula/link.rb', line 7

def protect_against_forgery?
  false
end

Instance Method Details

#==(obj) ⇒ Object Also known as: eql?



80
81
82
83
# File 'lib/relevance/tarantula/link.rb', line 80

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

#crawlObject



53
54
55
56
57
# File 'lib/relevance/tarantula/link.rb', line 53

def crawl
  response = crawler.follow(method, href)
  log "Response #{response.code} for #{self}"
  crawler.handle_link_results(self, make_result(response))
end

#hashObject



86
87
88
# File 'lib/relevance/tarantula/link.rb', line 86

def hash
  to_s.hash
end

#make_result(response) ⇒ Object



59
60
61
62
63
64
# File 'lib/relevance/tarantula/link.rb', line 59

def make_result(response)
  crawler.make_result(:method    => method,
                      :url       => href,
                      :response  => response,
                      :referrer  => referrer)
end

#methodObject



66
67
68
69
70
71
72
73
74
# File 'lib/relevance/tarantula/link.rb', line 66

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_sObject



90
91
92
# File 'lib/relevance/tarantula/link.rb', line 90

def to_s
  "<Relevance::Tarantula::Link href=#{href}, method=#{method}>"
end

#transform_url(link) ⇒ Object



76
77
78
# File 'lib/relevance/tarantula/link.rb', line 76

def transform_url(link)
  crawler.transform_url(link)
end