Class: Saxxy::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/saxxy/utils/agent.rb

Overview

The Agent is a thin wrapper over Net::HTTP::Proxy in order to be used for crawling purposes. Supports GET and POST via its get and post methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ Agent

Initializes an agent with optional proxy options. Url: A string that it is the url that the agent is going to use for issuing

requests. It can be reset to another via the self.uri = method.

Options:

  • proxy:

    • address: The address of the proxy.

    • port: The port the proxy will use.

    • username: The username if the proxy needs auth.

    • password: The password if the proxy needs auth.


21
22
23
24
25
# File 'lib/saxxy/utils/agent.rb', line 21

def initialize(url, opts = {})
  @proxy = opts[:proxy] || {}
  @agent = proxy.empty? ? Net::HTTP : Net::HTTP::Proxy(proxy[:address], proxy[:port], proxy[:username], proxy[:password])
  self.uri = url
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent


10
11
12
# File 'lib/saxxy/utils/agent.rb', line 10

def agent
  @agent
end

#proxyObject (readonly)

Returns the value of attribute proxy


10
11
12
# File 'lib/saxxy/utils/agent.rb', line 10

def proxy
  @proxy
end

#responseObject (readonly)

Returns the value of attribute response


10
11
12
# File 'lib/saxxy/utils/agent.rb', line 10

def response
  @response
end

#uriObject

Returns the value of attribute uri


10
11
12
# File 'lib/saxxy/utils/agent.rb', line 10

def uri
  @uri
end

#urlObject (readonly)

Returns the value of attribute url


10
11
12
# File 'lib/saxxy/utils/agent.rb', line 10

def url
  @url
end

Instance Method Details

#get(url = nil) ⇒ Object

Issues a get request either by using the url provided as an argument or the one the agent currently holds. Note: if the provided url is different from the agent's it updates the

agent's url also. See set_uri_for.

38
39
40
# File 'lib/saxxy/utils/agent.rb', line 38

def get(url = nil)
  issue_request(url, :get_response)
end

#post(url = nil, opts = {}) ⇒ Object

Issues a post request either by using the url provided as an argument or the one the agent currently holds. Uses the post_form method of the Net::HTTP::Proxy and forwards any passed options to the underlying agent. Note: if the provided url is different from the agent's it updates the

agent's url also. See set_uri_for.

47
48
49
# File 'lib/saxxy/utils/agent.rb', line 47

def post(url = nil, opts = {})
  issue_request(url, :post_form, opts)
end