Class: Mechanize::Page::MetaRefresh

Inherits:
Link
  • Object
show all
Defined in:
lib/mechanize/page/meta_refresh.rb

Overview

This class encapsulates a meta element with a refresh http-equiv. Mechanize treats meta refresh elements just like ‘a’ tags. MetaRefresh objects will contain links, but most likely will have no text.

Constant Summary collapse

CONTENT_REGEXP =

Matches the content attribute of a meta refresh element. After the match:

$1:: delay
$3:: url
/^\s*(\d+\.?\d*)(;|;\s*url=\s*['"]?(\S*?)['"]?)?\s*$/i

Instance Attribute Summary collapse

Attributes inherited from Link

#attributes, #href, #node, #page

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Link

#click, #dom_class, #dom_id, #pretty_print, #rel, #rel?, #text, #uri

Constructor Details

#initialize(node, page, delay, href, link_self = false) ⇒ MetaRefresh

Returns a new instance of MetaRefresh.



59
60
61
62
63
64
65
# File 'lib/mechanize/page/meta_refresh.rb', line 59

def initialize node, page, delay, href, link_self = false
  super node, page.mech, page

  @delay     = delay =~ /\./ ? delay.to_f : delay.to_i
  @href      = href
  @link_self = link_self
end

Instance Attribute Details

#delayObject (readonly)

Time to wait before next refresh



11
12
13
# File 'lib/mechanize/page/meta_refresh.rb', line 11

def delay
  @delay
end

This MetaRefresh links did not contain a url= in the content attribute and links to itself.



17
18
19
# File 'lib/mechanize/page/meta_refresh.rb', line 17

def link_self
  @link_self
end

Class Method Details

.from_node(node, page, uri) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/mechanize/page/meta_refresh.rb', line 48

def self.from_node node, page, uri
  http_equiv = node['http-equiv']
  return unless http_equiv and http_equiv.downcase == 'refresh'

  delay, uri, link_self = parse node['content'], uri

  return unless delay

  new node, page, delay, uri.to_s, link_self
end

.parse(content, base_uri) ⇒ Object

Parses the delay and url from the content attribute of a meta refresh element. Parse requires the uri of the current page to infer a url when no url is specified.

Returns an array of [delay, url]. (both in string)

Returns nil if the delay and url cannot be parsed.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mechanize/page/meta_refresh.rb', line 36

def self.parse content, base_uri
  return unless content =~ CONTENT_REGEXP

  link_self = $3.nil? || $3.empty?
  delay, refresh_uri = $1, $3

  dest = base_uri
  dest += refresh_uri if refresh_uri

  return delay, dest, link_self
end