Class: Kiik::Response
- Inherits:
-
Object
- Object
- Kiik::Response
- Defined in:
- lib/kiik/response.rb
Overview
OAuth2::Response class
Constant Summary collapse
- PARSERS =
Procs that, when called, will parse a response body according to the specified format.
{ :json => lambda { |body| MultiJson.load(body) rescue body }, # rubocop:disable RescueModifier :query => lambda { |body| Rack::Utils.parse_query(body) }, :text => lambda { |body| body }, }
- CONTENT_TYPES =
Content type assignments for various potential HTTP content types.
{ 'application/json' => :json, 'text/javascript' => :json, 'application/x-www-form-urlencoded' => :query, 'text/plain' => :text, }
Instance Attribute Summary collapse
-
#error ⇒ Object
Returns the value of attribute error.
-
#options ⇒ Object
Returns the value of attribute options.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
-
.register_parser(key, mime_types) {|String| ... } ⇒ Object
Adds a new content type parser.
Instance Method Summary collapse
-
#body ⇒ Object
The HTTP response body.
-
#content_type ⇒ Object
Attempts to determine the content type of the response.
-
#headers ⇒ Object
The HTTP response headers.
-
#initialize(response, opts = {}) ⇒ Response
constructor
Initializes a Response instance.
-
#parsed ⇒ Object
The parsed response body.
-
#parser ⇒ Object
Determines the parser that will be used to supply the content of #parsed.
-
#status ⇒ Object
The HTTP response status code.
Constructor Details
#initialize(response, opts = {}) ⇒ Response
Initializes a Response instance
26 27 28 29 |
# File 'lib/kiik/response.rb', line 26 def initialize(response, opts = {}) @response = response @options = {:parse => :automatic}.merge(opts) end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
5 6 7 |
# File 'lib/kiik/response.rb', line 5 def error @error end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/kiik/response.rb', line 5 def @options end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
4 5 6 |
# File 'lib/kiik/response.rb', line 4 def response @response end |
Class Method Details
.register_parser(key, mime_types) {|String| ... } ⇒ Object
Adds a new content type parser.
12 13 14 15 16 17 18 |
# File 'lib/kiik/response.rb', line 12 def self.register_parser(key, mime_types, &block) key = key.to_sym PARSERS[key] = block Array(mime_types).each do |mime_type| CONTENT_TYPES[mime_type] = key end end |
Instance Method Details
#body ⇒ Object
The HTTP response body
42 43 44 |
# File 'lib/kiik/response.rb', line 42 def body response.body || '' end |
#content_type ⇒ Object
Attempts to determine the content type of the response.
71 72 73 |
# File 'lib/kiik/response.rb', line 71 def content_type ((response.headers.values_at('content-type', 'Content-Type').compact.first || '').split(';').first || '').strip end |
#headers ⇒ Object
The HTTP response headers
32 33 34 |
# File 'lib/kiik/response.rb', line 32 def headers response.headers end |
#parsed ⇒ Object
The parsed response body.
Will attempt to parse application/x-www-form-urlencoded and
application/json Content-Type response bodies
65 66 67 68 |
# File 'lib/kiik/response.rb', line 65 def parsed return nil unless PARSERS.key?(parser) @parsed ||= PARSERS[parser].call(body) end |
#parser ⇒ Object
Determines the parser that will be used to supply the content of #parsed
76 77 78 79 |
# File 'lib/kiik/response.rb', line 76 def parser return [:parse].to_sym if PARSERS.key?([:parse]) CONTENT_TYPES[content_type] end |
#status ⇒ Object
The HTTP response status code
37 38 39 |
# File 'lib/kiik/response.rb', line 37 def status response.status end |