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_id, #pretty_print, #rel, #rel?, #text, #uri

Constructor Details

#initialize(node, page, delay, href) ⇒ MetaRefresh

Returns a new instance of MetaRefresh.



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

def initialize node, page, delay, href
  super node, page.mech, page

  @delay = delay.to_i
  @href  = href
end

Instance Attribute Details

#delayObject (readonly)

Returns the value of attribute delay.



8
9
10
# File 'lib/mechanize/page/meta_refresh.rb', line 8

def delay
  @delay
end

Class Method Details

.from_node(node, page, uri) ⇒ Object



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

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

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

  return unless delay

  new node, page, delay, uri.to_s
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 a MetaRefresh instance.

Returns nil if the delay and url cannot be parsed.



26
27
28
29
30
31
32
33
34
35
# File 'lib/mechanize/page/meta_refresh.rb', line 26

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

  delay, refresh_uri = $1, $3

  dest = base_uri
  dest += refresh_uri if refresh_uri

  return delay, dest
end