class OAuth2::Response
The Response
class handles HTTP responses in the OAuth2
gem, providing methods to access and parse response data in various formats.
@since 1.0.0
Constants
- DEFAULT_OPTIONS
-
Default configuration options for
Response
instances@return [Hash] The default options hash
Attributes
@return [Hash] The options hash for this instance
@return [Faraday::Response] The raw Faraday response object
Public Class Methods
Source
# File lib/oauth2/response.rb 72 def initialize(response, parse: :automatic, snaky: true, snaky_hash_klass: nil, **options) 73 @response = response 74 @options = { 75 parse: parse, 76 snaky: snaky, 77 snaky_hash_klass: snaky_hash_klass, 78 }.merge(options) 79 end
Initializes a Response
instance
@param [Faraday::Response] response The Faraday response instance @param [Symbol] parse (:automatic) How to parse the response body @param [Boolean] snaky (true) Whether to convert parsed response to snake_case using SnakyHash @param [Class, nil] snaky_hash_klass (nil) Custom class for snake_case hash conversion @param [Hash] options Additional options for the response @option options [Symbol] :parse (:automatic) Parse strategy (:query, :json, or :automatic) @option options [Boolean] :snaky (true) Enable/disable snake_case conversion @option options [Class] :snaky_hash_klass (SnakyHash::StringKeyed) Class to use for hash conversion @return [OAuth2::Response] The new Response
instance
Source
# File lib/oauth2/response.rb 53 def self.register_parser(key, mime_types, &block) 54 key = key.to_sym 55 @@parsers[key] = block 56 Array(mime_types).each do |mime_type| 57 @@content_types[mime_type] = key 58 end 59 end
Adds a new content type parser.
@param [Symbol] key A descriptive symbol key such as :json or :query @param [Array<String>, String] mime_types One or more mime types to which this parser applies @yield [String] Block that will be called to parse the response body @yieldparam [String] body The response body to parse @return [void]
Public Instance Methods
Source
# File lib/oauth2/response.rb 98 def body 99 response.body || "" 100 end
The HTTP response body
@return [String] The response body or empty string if nil
Source
# File lib/oauth2/response.rb 132 def content_type 133 return unless response.headers 134 135 ((response.headers.values_at("content-type", "Content-Type").compact.first || "").split(";").first || "").strip.downcase 136 end
Determines the content type of the response
@return [String, nil] The content type or nil if headers are not present
Source
# File lib/oauth2/response.rb 84 def headers 85 response.headers 86 end
The HTTP response headers
@return [Hash] The response headers
Source
# File lib/oauth2/response.rb 106 def parsed 107 return @parsed if defined?(@parsed) 108 109 @parsed = 110 if parser.respond_to?(:call) 111 case parser.arity 112 when 0 113 parser.call 114 when 1 115 parser.call(body) 116 else 117 parser.call(body, response) 118 end 119 end 120 121 if options[:snaky] && @parsed.is_a?(Hash) 122 hash_klass = options[:snaky_hash_klass] || DEFAULT_OPTIONS[:snaky_hash_klass] 123 @parsed = hash_klass[@parsed] 124 end 125 126 @parsed 127 end
The parsed response body
@return [Object, SnakyHash::StringKeyed] The parsed response body @return [nil] If no parser is available
Source
# File lib/oauth2/response.rb 152 def parser 153 return @parser if defined?(@parser) 154 155 @parser = 156 if options[:parse].respond_to?(:call) 157 options[:parse] 158 elsif options[:parse] 159 @@parsers[options[:parse].to_sym] 160 end 161 162 @parser ||= @@parsers[@@content_types[content_type]] 163 end
Determines the parser to be used for the response body
@note 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.
@note If no :parse
option is supplied, the lookup Symbol will be determined
by looking up {#content_type} in {@@content_types}.
@note If {#parser} is a Proc, it will be called with no arguments, just
{#body}, or {#body} and {#response}, depending on the Proc's arity.
@return [Proc, call] The parser proc or callable object @return [nil] If no suitable parser is found
Source
# File lib/oauth2/response.rb 91 def status 92 response.status 93 end
The HTTP response status code
@return [Integer] The response status code