Class: OAuth2::Response
- Inherits:
-
Object
- Object
- OAuth2::Response
- Defined in:
- lib/oauth2/response.rb
Overview
OAuth2::Response class
Constant Summary collapse
- DEFAULT_OPTIONS =
{ parse: :automatic, snaky: true, }.freeze
- @@parsers =
Procs that, when called, will parse a response body according to the specified format.
{ query: ->(body) { Rack::Utils.parse_query(body) }, text: ->(body) { body }, }
- @@content_types =
Content type assignments for various potential HTTP content types.
{ 'application/x-www-form-urlencoded' => :query, 'text/plain' => :text, }
Instance Attribute Summary collapse
-
#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, parse: :automatic, snaky: true, **options) ⇒ Response
constructor
Initializes a Response instance.
- #parsed ⇒ Object?
- #parser ⇒ Proc, ...
-
#status ⇒ Object
The HTTP response status code.
Constructor Details
#initialize(response, parse: :automatic, snaky: true, **options) ⇒ Response
Initializes a Response instance
51 52 53 54 55 56 57 |
# File 'lib/oauth2/response.rb', line 51 def initialize(response, parse: :automatic, snaky: true, **) @response = response @options = { parse: parse, snaky: snaky, }.merge() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/oauth2/response.rb', line 15 def @options end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
14 15 16 |
# File 'lib/oauth2/response.rb', line 14 def response @response end |
Class Method Details
.register_parser(key, mime_types) {|String| ... } ⇒ Object
Adds a new content type parser.
35 36 37 38 39 40 41 |
# File 'lib/oauth2/response.rb', line 35 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
70 71 72 |
# File 'lib/oauth2/response.rb', line 70 def body response.body || '' end |
#content_type ⇒ Object
Attempts to determine the content type of the response.
99 100 101 102 103 |
# File 'lib/oauth2/response.rb', line 99 def content_type return nil unless response.headers ((response.headers.values_at('content-type', 'Content-Type').compact.first || '').split(';').first || '').strip.downcase end |
#headers ⇒ Object
The HTTP response headers
60 61 62 |
# File 'lib/oauth2/response.rb', line 60 def headers response.headers end |
#parsed ⇒ Object?
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/oauth2/response.rb', line 78 def parsed return @parsed if defined?(@parsed) @parsed = if parser.respond_to?(:call) case parser.arity when 0 parser.call when 1 parser.call(body) else parser.call(body, response) end end @parsed = SnakyHash::StringKeyed.new(@parsed) if [:snaky] && @parsed.is_a?(Hash) @parsed end |
#parser ⇒ Proc, ...
Determines the parser (a Proc or other Object which responds to #call) that will be passed the #body (and optional #response) to supply #parsed.
The parser can be supplied as the :parse
option in the form of a Proc (or other Object responding to #call) or a Symbol. In the latter case, the actual parser will be looked up in @@parsers by the supplied Symbol.
If no :parse
option is supplied, the lookup Symbol will be determined by looking up #content_type in @@content_types.
If #parser is a Proc, it will be called with no arguments, just #body, or #body and #response, depending on the Proc’s arity.
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/oauth2/response.rb', line 121 def parser return @parser if defined?(@parser) @parser = if [:parse].respond_to?(:call) [:parse] elsif [:parse] @@parsers[[:parse].to_sym] end @parser ||= @@parsers[@@content_types[content_type]] end |
#status ⇒ Object
The HTTP response status code
65 66 67 |
# File 'lib/oauth2/response.rb', line 65 def status response.status end |