Class: Wrest::Curl::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
HttpCodes, HttpShared::Headers
Defined in:
lib/wrest/curl/response.rb

Overview

Decorates a response providing support for deserialisation.

Also provides set of HTTP response code checkers. For instance, the method ok? checks if the response was successful with HTTP code 200. See HttpCodes for a list of all such response checkers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HttpCodes

#accepted?, #bad_request?, #created?, #forbidden?, #found?, #internal_server_error?, #method_not_allowed?, #moved_permanently?, #no_content?, #not_acceptable?, #not_found?, #not_modified?, #ok?, #see_other?, #temporary_redirect?, #unauthorized?, #unprocessable_entity?

Methods included from HttpShared::Headers

#[]

Constructor Details

#initialize(http_response) ⇒ Response

Returns a new instance of Response



34
35
36
37
# File 'lib/wrest/curl/response.rb', line 34

def initialize(http_response)
  @http_response = http_response
  initialize_http_header
end

Instance Attribute Details

#deserialised_bodyObject

Returns the value of attribute deserialised_body



19
20
21
# File 'lib/wrest/curl/response.rb', line 19

def deserialised_body
  @deserialised_body
end

#http_responseObject (readonly)

Returns the value of attribute http_response



18
19
20
# File 'lib/wrest/curl/response.rb', line 18

def http_response
  @http_response
end

Instance Method Details

#codeObject



47
48
49
# File 'lib/wrest/curl/response.rb', line 47

def code
  @http_response.status
end

#connection_closed?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/wrest/curl/response.rb', line 72

def connection_closed?
  self[StandardHeaders::Connection].downcase == StandardTokens::Close.downcase
end

#content_lengthObject



55
56
57
# File 'lib/wrest/curl/response.rb', line 55

def content_length
  self[H::ContentLength].try(:to_i)
end

#content_typeObject



59
60
61
# File 'lib/wrest/curl/response.rb', line 59

def content_type
  self[H::ContentType].split(';').first
end

#deserialise(options = {}) ⇒ Object



39
40
41
# File 'lib/wrest/curl/response.rb', line 39

def deserialise(options = {})
    @deserialised_body ||= deserialise_using(Wrest::Components::Translators.lookup(content_type),options)
end

#deserialise_using(translator, options = {}) ⇒ Object



43
44
45
# File 'lib/wrest/curl/response.rb', line 43

def deserialise_using(translator,options={})
  translator.deserialise(@http_response,options)
end

#follow(redirect_request_options = {}) ⇒ Object

A null object implementation - invoking this method on a response simply returns the same response unless the response is a Redirection (code 3xx), in which case a get is invoked on the url stored in the response headers under the key 'location' and the new Response is returned.



68
69
70
# File 'lib/wrest/curl/response.rb', line 68

def follow(redirect_request_options = {})
  self
end

#initialize_http_headerObject



26
27
28
29
30
31
32
# File 'lib/wrest/curl/response.rb', line 26

def initialize_http_header
  headers.merge!(headers.inject({}) do |downcased_headers, tuple|
    key, value = tuple
    downcased_headers[key.downcase] = value
    downcased_headers
  end)
end

#messageObject



51
52
53
# File 'lib/wrest/curl/response.rb', line 51

def message
  @http_response.status_line
end