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
75 76 77 78 79 80 81 82 |
# File 'lib/oauth2/response.rb', line 75 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.
27 28 29 |
# File 'lib/oauth2/response.rb', line 27 def @options end |
#response ⇒ Faraday::Response (readonly)
Returns The raw Faraday response object.
24 25 26 |
# File 'lib/oauth2/response.rb', line 24 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.
55 56 57 58 59 60 61 |
# File 'lib/oauth2/response.rb', line 55 def 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
101 102 103 |
# File 'lib/oauth2/response.rb', line 101 def body response.body || "" end |
#content_type ⇒ String?
Determines the content type of the response
136 137 138 139 140 141 |
# File 'lib/oauth2/response.rb', line 136 def content_type response_headers = response.headers 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
87 88 89 |
# File 'lib/oauth2/response.rb', line 87 def headers response.headers end |
#parsed ⇒ Object, ...
The parsed response body
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/oauth2/response.rb', line 109 def parsed return @parsed if defined?(@parsed) response_parser = parser @parsed = if response_parser.respond_to?(:call) case response_parser.arity when 0 response_parser.call when 1 response_parser.call(body) else response_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
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/oauth2/response.rb', line 157 def parser return @parser if defined?(@parser) parse_option = [:parse] @parser = if parse_option.respond_to?(:call) parse_option elsif parse_option @@parsers[parse_option.to_sym] end @parser ||= @@parsers[@@content_types[content_type]] end |
#status ⇒ Integer
The HTTP response status code
94 95 96 |
# File 'lib/oauth2/response.rb', line 94 def status response.status end |