Class: Horseman::Browser
- Inherits:
-
Object
- Object
- Horseman::Browser
- Defined in:
- lib/horseman/browser.rb
Constant Summary collapse
- MaxRedirects =
10
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#last_action ⇒ Object
readonly
Returns the value of attribute last_action.
-
#multipart_boundary ⇒ Object
readonly
Returns the value of attribute multipart_boundary.
Class Method Summary collapse
Instance Method Summary collapse
- #clear_session ⇒ Object
- #get!(path = '/', options = {}) ⇒ Object
-
#initialize(connection, base_url = '') ⇒ Browser
constructor
A new instance of Browser.
- #post!(path = '/', options = {}) ⇒ Object
Constructor Details
#initialize(connection, base_url = '') ⇒ Browser
Returns a new instance of Browser.
17 18 19 20 21 22 |
# File 'lib/horseman/browser.rb', line 17 def initialize(connection, base_url='') @connection = connection @base_url = base_url @cookies = Horseman::Cookies.new @multipart_boundary = "----HorsemanBoundary#{SecureRandom.hex(8)}" end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
10 11 12 |
# File 'lib/horseman/browser.rb', line 10 def base_url @base_url end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
11 12 13 |
# File 'lib/horseman/browser.rb', line 11 def connection @connection end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
11 12 13 |
# File 'lib/horseman/browser.rb', line 11 def @cookies end |
#last_action ⇒ Object (readonly)
Returns the value of attribute last_action.
11 12 13 |
# File 'lib/horseman/browser.rb', line 11 def last_action @last_action end |
#multipart_boundary ⇒ Object (readonly)
Returns the value of attribute multipart_boundary.
11 12 13 |
# File 'lib/horseman/browser.rb', line 11 def multipart_boundary @multipart_boundary end |
Class Method Details
.with_base_url(base_url) ⇒ Object
13 14 15 |
# File 'lib/horseman/browser.rb', line 13 def self.with_base_url(base_url) Horseman::Browser.new(Horseman::Connection.new, base_url) end |
Instance Method Details
#clear_session ⇒ Object
24 25 26 |
# File 'lib/horseman/browser.rb', line 24 def clear_session @cookies.clear end |
#get!(path = '/', options = {}) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/horseman/browser.rb', line 28 def get!(path = '/', = {}) url = [:no_base_url] ? path : "#{@base_url}#{path}" request = @connection.build_request(:url => url, :verb => :get) redirects = [:redirects] || 0 exec(request, redirects) end |
#post!(path = '/', options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 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 |
# File 'lib/horseman/browser.rb', line 35 def post!(path = '/', = {}) get! path form = [:form] || :form data = [:data] || {} unchecked = [:unchecked] || [] selected_form = @last_action.response.forms.select {|f| f.id && f.id.to_sym == form}.first raise "Could not find form #{form}" if selected_form.nil? selected_form.fields.each do |f| data[f.name.to_sym] ||= f.value unless unchecked.include? f.name.to_sym 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 exec request end |