Class: Horseman::Browser::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/horseman/browser.rb

Constant Summary collapse

MaxRedirects =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, js_engine, base_url = '', options = {}) ⇒ Browser

Returns a new instance of Browser.



22
23
24
25
26
27
28
29
30
31
# File 'lib/horseman/browser.rb', line 22

def initialize(connection, js_engine, base_url='', options={})
  @connection = connection
  @js_engine = js_engine
  @base_url = base_url
  @cookies = Cookies.new
  @multipart_boundary = "----HorsemanBoundary#{SecureRandom.hex(8)}"

  @verbose = options[:verbose] || false
  @enable_js = options[:enable_js] || false
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



18
19
20
# File 'lib/horseman/browser.rb', line 18

def base_url
  @base_url
end

#connectionObject (readonly)

Returns the value of attribute connection.



19
20
21
# File 'lib/horseman/browser.rb', line 19

def connection
  @connection
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



20
21
22
# File 'lib/horseman/browser.rb', line 20

def cookies
  @cookies
end

#js_engineObject (readonly)

Returns the value of attribute js_engine.



19
20
21
# File 'lib/horseman/browser.rb', line 19

def js_engine
  @js_engine
end

#last_actionObject (readonly)

Returns the value of attribute last_action.



20
21
22
# File 'lib/horseman/browser.rb', line 20

def last_action
  @last_action
end

#multipart_boundaryObject (readonly)

Returns the value of attribute multipart_boundary.



20
21
22
# File 'lib/horseman/browser.rb', line 20

def multipart_boundary
  @multipart_boundary
end

Instance Method Details

#clear_sessionObject



33
34
35
# File 'lib/horseman/browser.rb', line 33

def clear_session
  @cookies.clear
end

#get!(path = '/', options = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/horseman/browser.rb', line 37

def get!(path = '/', options = {})
  url = options[:no_base_url] ? path : "#{@base_url}#{path}"
  request = @connection.build_request(:url => url, :verb => :get)
  redirects = options[:redirects] || 0

  puts "GET: #{path}" if @verbose
  exec(request, redirects)
end

#post!(path = '/', options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/horseman/browser.rb', line 46

def post!(path = '/', options = {})
  get! path
  
  form = options[:form] || :form
  data = options[:data] || {}
  unchecked = options[:unchecked] || []
  
  selected_form = @last_action.response.document.forms[form]
  raise "Could not find form #{form}" if selected_form.nil?

  selected_form.fields.each do |name, field|
    data[name] ||= field.value unless unchecked.include? name
  end
  request_body = build_request_body(data, selected_form.encoding)
  
  if is_absolute_url?(selected_form.action)
    # Absolute action http://www.example.com/action
    url = selected_form.action
  elsif selected_form.action == ''
    # No action, post to same URL as GET request
    url = "#{@last_action.url}"
  else
    # Relative action, use relative root from last action
    url = "#{@last_action.relative_root}#{selected_form.action}"
  end
  
  request = @connection.build_request(:url => "#{url}", :verb => :post, :body => request_body)
  request['Content-Type'] = case selected_form.encoding
                            when :multipart
                              "multipart/form-data; boundary=#{@multipart_boundary}"
                            else
                              "application/x-www-form-urlencoded"
                            end
  request['Referer'] = @last_action.url 
  
  puts "POST: #{path}" if @verbose
  exec request
end