Class: OAuth2::Response
- Inherits:
-
Object
- Object
- OAuth2::Response
- Defined in:
- lib/oauth2/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 Style/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
48 49 50 51 |
# File 'lib/oauth2/response.rb', line 48 def initialize(response, opts = {}) @response = response @options = {:parse => :automatic}.merge(opts) end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
11 12 13 |
# File 'lib/oauth2/response.rb', line 11 def error @error end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/oauth2/response.rb', line 11 def @options end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
10 11 12 |
# File 'lib/oauth2/response.rb', line 10 def response @response end |
Class Method Details
.register_parser(key, mime_types) {|String| ... } ⇒ Object
Adds a new content type parser.
34 35 36 37 38 39 40 |
# File 'lib/oauth2/response.rb', line 34 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
64 65 66 |
# File 'lib/oauth2/response.rb', line 64 def body response.body || '' end |
#content_type ⇒ Object
Attempts to determine the content type of the response.
78 79 80 |
# File 'lib/oauth2/response.rb', line 78 def content_type ((response.headers.values_at('content-type', 'Content-Type').compact.first || '').split(';').first || '').strip end |
#headers ⇒ Object
The HTTP response headers
54 55 56 |
# File 'lib/oauth2/response.rb', line 54 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
71 72 73 74 75 |
# File 'lib/oauth2/response.rb', line 71 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
83 84 85 86 87 |
# File 'lib/oauth2/response.rb', line 83 def parser return [:parse].to_sym if @@parsers.key?([:parse]) @@content_types[content_type] end |
#status ⇒ Object
The HTTP response status code
59 60 61 |
# File 'lib/oauth2/response.rb', line 59 def status response.status end |