Class: Webrat::MechanizeAdapter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/webrat/adapters/mechanize.rb

Overview

:nodoc:

Constant Summary collapse

Mechanize =
WWW::Mechanize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MechanizeAdapter

Returns a new instance of MechanizeAdapter


12
13
# File 'lib/webrat/adapters/mechanize.rb', line 12

def initialize(*args)
end

Instance Attribute Details

#responseObject Also known as: page

Returns the value of attribute response


9
10
11
# File 'lib/webrat/adapters/mechanize.rb', line 9

def response
  @response
end

Instance Method Details

#absolute_url(url) ⇒ Object

:nodoc:


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/webrat/adapters/mechanize.rb', line 64

def absolute_url(url) #:nodoc:
  current_host, current_path = split_current_url
  if url =~ Regexp.new('^https?://')
    url
  elsif url =~ Regexp.new('^/')
    current_host + url
  elsif url =~ Regexp.new('^\.')
    current_host + absolute_path(current_path, url)
  else
    url
  end
end

#get(url, data, headers_argument_not_used = nil) ⇒ Object


19
20
21
# File 'lib/webrat/adapters/mechanize.rb', line 19

def get(url, data, headers_argument_not_used = nil)
  @response = mechanize.get(url, data)
end

#mechanizeObject


54
55
56
57
58
59
60
# File 'lib/webrat/adapters/mechanize.rb', line 54

def mechanize
  @mechanize ||= begin
    mechanize = Mechanize.new
    mechanize.redirect_ok = false
    mechanize
  end
end

#post(url, data, headers_argument_not_used = nil) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/webrat/adapters/mechanize.rb', line 23

def post(url, data, headers_argument_not_used = nil)
  post_data = data.inject({}) do |memo, param|
    case param
    when Hash
      param.each {|attribute, value| memo[attribute] = value }
      memo
    when Array
      case param.last
      when Hash
        param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
      else
        memo[param.first] = param.last
      end
      memo
    end
  end
  @response = mechanize.post(url, post_data)
end

#request_page(url, http_method, data) ⇒ Object

:nodoc:


15
16
17
# File 'lib/webrat/adapters/mechanize.rb', line 15

def request_page(url, http_method, data) #:nodoc:
  super(absolute_url(url), http_method, data)
end

#response_bodyObject


42
43
44
# File 'lib/webrat/adapters/mechanize.rb', line 42

def response_body
  @response.content
end

#response_codeObject


46
47
48
# File 'lib/webrat/adapters/mechanize.rb', line 46

def response_code
  @response.code.to_i
end

#response_headersObject


50
51
52
# File 'lib/webrat/adapters/mechanize.rb', line 50

def response_headers
  @response.header
end