Class: Wrest::Http::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/wrest/http/request.rb

Direct Known Subclasses

Delete, Get, Options, Post, Put

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {}) ⇒ Request

Returns a new instance of Request.



13
14
15
16
17
18
19
20
21
# File 'lib/wrest/http/request.rb', line 13

def initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {})
  @uri = wrest_uri
  @headers = headers.stringify_keys
  @http_request = http_request_klass.new(parameters.empty? ? wrest_uri.full_path : "#{wrest_uri.full_path}?#{parameters.to_query}", @headers)
  @body = body
  @options = options
  @username = options[:username]
  @password = options[:password]
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/wrest/http/request.rb', line 12

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/wrest/http/request.rb', line 12

def headers
  @headers
end

#http_requestObject (readonly)

Returns the value of attribute http_request.



12
13
14
# File 'lib/wrest/http/request.rb', line 12

def http_request
  @http_request
end

#passwordObject (readonly)

Returns the value of attribute password.



12
13
14
# File 'lib/wrest/http/request.rb', line 12

def password
  @password
end

#uriObject (readonly)

Returns the value of attribute uri.



12
13
14
# File 'lib/wrest/http/request.rb', line 12

def uri
  @uri
end

#usernameObject (readonly)

Returns the value of attribute username.



12
13
14
# File 'lib/wrest/http/request.rb', line 12

def username
  @username
end

Instance Method Details

#invokeObject

Makes a request and returns a Wrest::Http::Response. Data about the request is and logged to Wrest.logger



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wrest/http/request.rb', line 25

def invoke
  response = nil

  prefix = "#{http_request.method} #{http_request.hash}"
  http_request.basic_auth username, password

  Wrest.logger.debug "--> (#{prefix}) #{@uri.protocol}://#{@uri.host}:#{@uri.port}#{@http_request.path}"
  time = Benchmark.realtime { response = Wrest::Http::Response.new( http.request(@http_request, @body) ) }
  Wrest.logger.debug "<-- (#{prefix}) %d %s (%d bytes %.2fs)" % [response.code, response.message, response.body ? response.body.length : 0, time]

  response
end