Class: OAuth2::Response
- Inherits:
-
Object
- Object
- OAuth2::Response
- Defined in:
- lib/oauth2/response.rb
Overview
The Response class handles HTTP responses in the OAuth2 gem, providing methods
to access and parse response data in various formats.
Constant Summary collapse
- DEFAULT_OPTIONS =
Default configuration options for Response instances
{ parse: :automatic, snaky: true, snaky_hash_klass: SnakyHash::StringKeyed, }.freeze
- @@parsers =
Storage for response body parser procedures
{ query: ->(body) { Rack::Utils.parse_query(body) }, text: ->(body) { body }, }
- @@content_types =
Maps content types to parser symbols
{ "application/x-www-form-urlencoded" => :query, "text/plain" => :text, }
Instance Attribute Summary collapse
-
#options ⇒ Hash
The options hash for this instance.
-
#response ⇒ Faraday::Response
readonly
The raw Faraday response object.
Class Method Summary collapse
-
.register_parser(key, mime_types) {|String| ... } ⇒ void
Adds a new content type parser.
Instance Method Summary collapse
-
#body ⇒ String
The HTTP response body.
-
#content_type ⇒ String?
Determines the content type of the response.
-
#headers ⇒ Hash
The HTTP response headers.
-
#initialize(response, parse: :automatic, snaky: true, snaky_hash_klass: nil, **options) ⇒ OAuth2::Response
constructor
Initializes a Response instance.
-
#parsed ⇒ Object, ...
The parsed response body.
-
#parser ⇒ Proc, ...
Determines the parser to be used for the response body.
-
#status ⇒ Integer
The HTTP response status code.
Constructor Details
#initialize(response, parse: :automatic, snaky: true, snaky_hash_klass: nil, **options) ⇒ OAuth2::Response
Initializes a Response instance
72 73 74 75 76 77 78 79 |
# File 'lib/oauth2/response.rb', line 72 def initialize(response, parse: :automatic, snaky: true, snaky_hash_klass: nil, **) @response = response @options = { parse: parse, snaky: snaky, snaky_hash_klass: snaky_hash_klass, }.merge() end |
Instance Attribute Details
#options ⇒ Hash
Returns The options hash for this instance.
26 27 28 |
# File 'lib/oauth2/response.rb', line 26 def @options end |
#response ⇒ Faraday::Response (readonly)
Returns The raw Faraday response object.
23 24 25 |
# File 'lib/oauth2/response.rb', line 23 def response @response end |
Class Method Details
.register_parser(key, mime_types) {|String| ... } ⇒ void
This method returns an undefined value.
Adds a new content type parser.
53 54 55 56 57 58 59 |
# File 'lib/oauth2/response.rb', line 53 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 ⇒ String
The HTTP response body
98 99 100 |
# File 'lib/oauth2/response.rb', line 98 def body response.body || "" end |
#content_type ⇒ String?
Determines the content type of the response
132 133 134 135 136 |
# File 'lib/oauth2/response.rb', line 132 def content_type return unless response.headers ((response.headers.values_at("content-type", "Content-Type").compact.first || "").split(";").first || "").strip.downcase end |
#headers ⇒ Hash
The HTTP response headers
84 85 86 |
# File 'lib/oauth2/response.rb', line 84 def headers response.headers end |
#parsed ⇒ Object, ...
The parsed response body
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/oauth2/response.rb', line 106 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 if [:snaky] && @parsed.is_a?(Hash) hash_klass = [:snaky_hash_klass] || DEFAULT_OPTIONS[:snaky_hash_klass] @parsed = hash_klass[@parsed] end @parsed end |
#parser ⇒ Proc, ...
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.
Determines the parser to be used for the response body
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/oauth2/response.rb', line 152 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 ⇒ Integer
The HTTP response status code
91 92 93 |
# File 'lib/oauth2/response.rb', line 91 def status response.status end |