Class: RFuzz::Browser
- Inherits:
-
Object
- Object
- RFuzz::Browser
- Defined in:
- lib/rfuzz/browser.rb
Overview
A simple class that emulates a browser using hpricot.
Constant Summary collapse
- DEFAULT_AGENT =
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.4) Gecko/20060608 Ubuntu/dapper-security Firefox/1.5.0.4"
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#client ⇒ Object
Returns the value of attribute client.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
-
#initialize(host, port = 80, ops = {}, agent = DEFAULT_AGENT) ⇒ Browser
constructor
The default agent used is Mozilla (from linux Dapper Drake), but you can change it to something else.
-
#links(matching = nil) ⇒ Object
Returns an Array of Hpricot objects that are the links on the current page.
-
#start(uri, ops = {}) ⇒ Object
Makes the browser do a GET to this location.
Constructor Details
#initialize(host, port = 80, ops = {}, agent = DEFAULT_AGENT) ⇒ Browser
The default agent used is Mozilla (from linux Dapper Drake), but you can change it to something else.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rfuzz/browser.rb', line 17 def initialize(host, port=80, ops={}, agent=DEFAULT_AGENT) @agent = agent @client = HttpClient.new(host, port, ops) ops[:head] ||= {} ops[:head]["User-Agent"] ||= @agent @doc = nil @response = nil end |
Instance Attribute Details
#agent ⇒ Object
Returns the value of attribute agent.
11 12 13 |
# File 'lib/rfuzz/browser.rb', line 11 def agent @agent end |
#client ⇒ Object
Returns the value of attribute client.
8 9 10 |
# File 'lib/rfuzz/browser.rb', line 8 def client @client end |
#doc ⇒ Object
Returns the value of attribute doc.
9 10 11 |
# File 'lib/rfuzz/browser.rb', line 9 def doc @doc end |
#response ⇒ Object
Returns the value of attribute response.
10 11 12 |
# File 'lib/rfuzz/browser.rb', line 10 def response @response end |
Instance Method Details
#links(matching = nil) ⇒ Object
Returns an Array of Hpricot objects that are the links on the current page. If you pass in matching as a regex (or any === compatible with String) then it’ll only return those links.
42 43 44 45 46 47 48 49 50 |
# File 'lib/rfuzz/browser.rb', line 42 def links(matching=nil) links = @doc/:a if matching # return only the ones that match return links.select {|l| matching === l.attributes["href"]} else return links end end |
#start(uri, ops = {}) ⇒ Object
Makes the browser do a GET to this location. It takes the same params as HttpClient does for any method.
30 31 32 33 34 35 36 37 |
# File 'lib/rfuzz/browser.rb', line 30 def start(uri, ops={}) @response = @client.get(uri,ops) if @response.http_status != "200" raise "Invalid status: #{@response.http_status}" end @doc = Hpricot(@response.http_body) end |