class OAuth2::Response
@since 1.0.0
to access and parse response data in various formats.
The Response class handles HTTP responses in the OAuth2 gem, providing methods
def body
-
(String)- The response body or empty string if nil
def body response.body || "" end
def content_type
-
(String, nil)- The content type or nil if headers are not present
def content_type return unless response.headers ((response.headers.values_at("content-type", "Content-Type").compact.first || "").split(";").first || "").strip.downcase end
def headers
-
(Hash)- The response headers
def headers response.headers end
def initialize(response, parse: :automatic, snaky: true, snaky_hash_klass: nil, **options)
-
(OAuth2::Response)- The new Response instance
Options Hash:
(**options)-
:snaky_hash_klass(Class) -- Class to use for hash conversion -
:snaky(Boolean) -- Enable/disable snake_case conversion -
:parse(Symbol) -- Parse strategy (:query, :json, or :automatic)
Parameters:
-
options(Hash) -- Additional options for the response -
snaky_hash_klass(Class, nil) -- (nil) Custom class for snake_case hash conversion -
snaky(Boolean) -- (true) Whether to convert parsed response to snake_case using SnakyHash -
parse(Symbol) -- (:automatic) How to parse the response body -
response(Faraday::Response) -- The Faraday response instance
def initialize(response, parse: :automatic, snaky: true, snaky_hash_klass: nil, **options) @response = response @options = { parse: parse, snaky: snaky, snaky_hash_klass: snaky_hash_klass, }.merge(options) end
def parsed
-
(nil)- If no parser is available -
(Object, SnakyHash::StringKeyed)- The parsed response body
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 options[:snaky] && @parsed.is_a?(Hash) hash_klass = options[:snaky_hash_klass] || DEFAULT_OPTIONS[:snaky_hash_klass] @parsed = hash_klass[@parsed] end @parsed end
def parser
-
(nil)- If no suitable parser is found -
(Proc, #call)- The parser proc or callable object
Other tags:
- Note: - If {#parser} is a Proc, it will be called with no arguments, just
Note: - If no +:parse+ option is supplied, the lookup Symbol will be determined
Note: - The parser can be supplied as the +:parse+ option in the form of a Proc
def parser return @parser if defined?(@parser) @parser = if options[:parse].respond_to?(:call) options[:parse] elsif options[:parse] @@parsers[options[:parse].to_sym] end @parser ||= @@parsers[@@content_types[content_type]] end
def register_parser(key, mime_types, &block)
-
(void)-
Other tags:
- Yieldparam: body - The response body to parse
Other tags:
- Yield: - Block that will be called to parse the response body
Parameters:
-
mime_types(Array) -- One or more mime types to which this parser applies, String -
key(Symbol) -- A descriptive symbol key such as :json or :query
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
def status
-
(Integer)- The response status code
def status response.status end