Class Wrest::Curl::Request

  1. lib/wrest/curl/request.rb
Parent: Object

This represents a HTTP request. Typically you will never need to instantiate one of these yourself - you can use one of the more conveient APIs via Wrest::Uri or Wrest::Curl::Get etc. instead.

Methods

public class

  1. new

public instance

  1. invoke

Attributes

auth_type [R]
body [R]
connection [R]
file_name [R]
follow_redirects [R]
follow_redirects_limit [R]
headers [R]
http_request [R]
multipart [R]
parameters [R]
password [R]
timeout [R]
uri [R]
username [R]

Public class methods

new (wrest_uri, http_verb, parameters = {}, body = nil, headers = {}, options = {})

Valid tuples for the options are:

:username => String, defaults to nil
:password => String, defaults to nil
:follow_redirects => Boolean, defaults to true for Get, false for anything else
:follow_redirects_limit => Integer, defaults to 5. This is the number of redirects
                           that Wrest will automatically follow before raising an
                           Wrest::Exceptions::AutoRedirectLimitExceeded exception.
                           For example, if you set this to 1, the very first redirect
                           will raise the exception.
:timeout => The period, in seconds, after which a Timeout::Error is raised
            in the event of a connection failing to open. Defaulted to 60 by Uri#create_connection.
:connection => The HTTP Connection object to use. This is how a keep-alive connection can be
               used for multiple requests. Not yet fully implemented for Curl.

Curl specific options:

:auth_type => This is a curl specific option and can be one of :basic, :digest, or :any. The default is :basic.
[show source]
    # File lib/wrest/curl/request.rb, line 34
34:       def initialize(wrest_uri, http_verb, parameters = {}, body = nil, headers = {}, options = {})
35:         @uri = wrest_uri
36:         @headers = headers.stringify_keys
37:         @parameters = parameters
38:         @body = body
39: 
40:         @options = options.clone
41:         @multipart = @options[:multipart]
42:         @file_name = @options[:file]
43:         @auth_type = @options[:auth_type] || :basic
44:         @username = @options[:username]
45:         @password = @options[:password]
46:         @follow_redirects = (@options[:follow_redirects] ||= false)
47: 
48:         @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5)
49:         @timeout = @options[:timeout] || 60
50:         @connection = @options[:connection]
51: 
52:         @http_request = Patron::Request.new
53:         @http_request.action = http_verb
54: 
55:         @http_request.headers = headers
56:         @http_request.username = username
57:         @http_request.password = password
58:         @http_request.multipart = multipart
59:         @http_request.upload_data = body 
60:         @http_request.file_name = file_name
61:         @http_request.auth_type = auth_type
62:         @http_request.url = parameters.empty? ? uri.to_s : "#{uri.to_s}?#{parameters.to_query}"
63:         @http_request.max_redirects = follow_redirects_limit if follow_redirects
64:         @http_request.timeout = @timeout
65:       end

Public instance methods

invoke ()

Makes a request and returns a Wrest::Native::Response. Data about the request is and logged to Wrest.logger The log entry contains the following information:

<- indicates a request
-> indicates a response

The type of request is mentioned in caps, followed by a hash uniquely uniquely identifying a particular request/response pair. In a multi-process or multi-threaded scenario, this can be used to identify request-response pairs.

The request hash is followed by a connection hash; requests using the same connection (effectively a keep-alive connection) will have the same connection hash.

This is followed by the response code, the payload size and the time taken.

[show source]
    # File lib/wrest/curl/request.rb, line 84
84:       def invoke
85:         response = nil
86: 
87:         @connection ||= Patron::Session.new
88:         raise ArgumentError, "Empty URL" if http_request.url.empty?
89: 
90:         prefix = "#{http_request.action.to_s.upcase} #{http_request.hash} #{connection.hash}"
91: 
92:         Wrest.logger.debug "<- (#{prefix}) #{http_request.url}"
93:         time = Benchmark.realtime { response =  Wrest::Curl::Response.new(connection.handle_request(http_request))}
94:         Wrest.logger.debug "-> (#{prefix}) %s (%d bytes %.2fs)" % [response.message, response.body ? response.body.length : 0, time]
95: 
96:         response
97:       rescue Patron::TimeoutError => e
98:         raise Wrest::Exceptions::Timeout.new(e)
99:       end