Class: WANG::Client
- Inherits:
-
Object
- Object
- WANG::Client
- Defined in:
- lib/wang.rb
Instance Attribute Summary collapse
-
#responses ⇒ Object
Returns the value of attribute responses.
Instance Method Summary collapse
-
#delete(url, referer = nil) ⇒ Object
Issues a DELETE request.
-
#get(url, referer = nil) ⇒ Object
Fetches a page using GET method.
-
#head(url, referer = nil) ⇒ Object
Issues a HEAD request.
-
#initialize(args = {}) ⇒ Client
constructor
Creates a new instance of WANG::Client.
-
#load_cookies(io) ⇒ Object
Loads cookies to this Client instance’s Jar from the given io.
-
#post(url, data, referer = nil) ⇒ Object
Fetches a page using POST method.
-
#put(url, data, referer = nil) ⇒ Object
Issues a PUT request.
-
#save_cookies(io) ⇒ Object
Saves cookie from this Client instance’s Jar to the given io.
-
#set_auth(username, password = '') ⇒ Object
Sets the HTTP authentication username & password, which are then used for requests Call with
nilas username to remove authentication.
Constructor Details
#initialize(args = {}) ⇒ Client
Creates a new instance of WANG::Client
Accepts a hash containing named arguments. Arguments:
- :read_timeout
-
defines the timeout for socket reading in seconds
- :open_timeout
-
defines the timeout for connecting in seconds
- :debug
-
any value passed defines debug mode
- :proxy_address
-
defines the proxy address to use for all the requests made (host:port)
- :no_keepalive
-
any value passed will disable keep-alive
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/wang.rb', line 66 def initialize args = {} @log = Logger.new(STDOUT) @log.level = args[:debug] ? Logger::DEBUG : Logger::WARN @jar = Jar.new @socket = nil @host, @port = nil, nil # create a struct that combines host & port? @responses = [] @read_timeout = args[:read_timeout] || DEFAULT_READ_TIMEOUT @open_timeout = args[:open_timeout] || DEFAULT_OPEN_TIMEOUT @proxy_host, @proxy_port = args[:proxy_address] ? args[:proxy_address].split(':', 2) : [nil, nil] @no_keepalive = args[:no_keepalive] @log.debug("Connecting through a proxy: #{@proxy_host}:#{@proxy_port}") if @proxy_host @log.debug("Using #{@read_timeout} as the read timeout and #{@open_timeout} as the open timeout") end |
Instance Attribute Details
#responses ⇒ Object
Returns the value of attribute responses.
56 57 58 |
# File 'lib/wang.rb', line 56 def responses @responses end |
Instance Method Details
#delete(url, referer = nil) ⇒ Object
Issues a DELETE request.
Returns nil for the body.
118 119 120 121 |
# File 'lib/wang.rb', line 118 def delete url, referer = nil @log.debug("DELETE: #{url.to_s}") request("DELETE", url.to_uri, referer) end |
#get(url, referer = nil) ⇒ Object
Fetches a page using GET method
If passed, referer will be sent to the server. Otherwise the last visited URL will be sent to the server as the referer.
94 95 96 97 |
# File 'lib/wang.rb', line 94 def get url, referer = nil @log.debug("GET: #{url.to_s}") request("GET", url.to_uri, referer) end |
#head(url, referer = nil) ⇒ Object
Issues a HEAD request.
Returns nil for the body.
86 87 88 89 |
# File 'lib/wang.rb', line 86 def head url, referer = nil @log.debug("HEAD: #{url.to_s}") request('HEAD', url.to_uri, referer) end |
#load_cookies(io) ⇒ Object
Loads cookies to this Client instance’s Jar from the given io
129 130 131 |
# File 'lib/wang.rb', line 129 def io @jar.load(io) end |
#post(url, data, referer = nil) ⇒ Object
Fetches a page using POST method
Data can either be a String or a Hash. If passed a String, it will send it to the server as the POST data. If passed a Hash, it will be converted to post data and correctly escaped.
If passed, referer will be sent to the server. Otherwise the last visited URL will be sent to the server as the referer.
104 105 106 107 |
# File 'lib/wang.rb', line 104 def post url, data, referer = nil @log.debug("POST: #{url.to_s}") request("POST", url.to_uri, referer, data) end |
#put(url, data, referer = nil) ⇒ Object
Issues a PUT request. See post for more details.
110 111 112 113 |
# File 'lib/wang.rb', line 110 def put url, data, referer = nil @log.debug("PUT: #{url.to_s}") request("PUT", url.to_uri, referer, data) end |
#save_cookies(io) ⇒ Object
Saves cookie from this Client instance’s Jar to the given io
124 125 126 |
# File 'lib/wang.rb', line 124 def io @jar.save(io) end |
#set_auth(username, password = '') ⇒ Object
Sets the HTTP authentication username & password, which are then used for requests Call with nil as username to remove authentication
135 136 137 |
# File 'lib/wang.rb', line 135 def set_auth username, password='' @http_auth = username ? Base64.encode64(username+':'+password).chomp : nil # for some reason, encode64 might add a \n end |