Class: Horseman::Browser::Browser
- Inherits:
-
Object
- Object
- Horseman::Browser::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.
-
#js_engine ⇒ Object
readonly
Returns the value of attribute js_engine.
-
#last_action ⇒ Object
readonly
Returns the value of attribute last_action.
-
#multipart_boundary ⇒ Object
readonly
Returns the value of attribute multipart_boundary.
Instance Method Summary collapse
- #clear_session ⇒ Object
- #get!(path = '/', options = {}) ⇒ Object
-
#initialize(connection, js_engine, base_url = '', options = {}) ⇒ Browser
constructor
A new instance of Browser.
- #post!(path = '/', options = {}) ⇒ Object
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='', ={}) @connection = connection @js_engine = js_engine @base_url = base_url @cookies = Cookies.new @multipart_boundary = "----HorsemanBoundary#{SecureRandom.hex(8)}" @verbose = [:verbose] || false @enable_js = [:enable_js] || false end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
18 19 20 |
# File 'lib/horseman/browser.rb', line 18 def base_url @base_url end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
19 20 21 |
# File 'lib/horseman/browser.rb', line 19 def connection @connection end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
20 21 22 |
# File 'lib/horseman/browser.rb', line 20 def @cookies end |
#js_engine ⇒ Object (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_action ⇒ Object (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_boundary ⇒ Object (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_session ⇒ Object
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 = '/', = {}) url = [:no_base_url] ? path : "#{@base_url}#{path}" request = @connection.build_request(:url => url, :verb => :get) redirects = [: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 = '/', = {}) get! path form = [:form] || :form data = [:data] || {} unchecked = [: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 |