class OAuth2::Response
OAuth2::Response
class
Constants
- DEFAULT_OPTIONS
Attributes
Public Class Methods
Source
# File lib/oauth2/response.rb, line 51 def initialize(response, parse: :automatic, snaky: true, **options) @response = response @options = { parse: parse, snaky: snaky, }.merge(options) end
Initializes a Response
instance
@param [Faraday::Response] response The Faraday response instance @param [Symbol] parse (:automatic) how to parse the response body. one of :query (for x-www-form-urlencoded),
:json, or :automatic (determined by Content-Type response header)
@param [true, false] snaky (true) Convert @parsed to a snake-case,
indifferent-access SnakyHash::StringKeyed, which is a subclass of Hashie::Mash (from hashie gem)?
@param [Hash] options all other options for initializing the instance
Source
# 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
Adds a new content type parser.
@param [Symbol] key A descriptive symbol key such as :json or :query. @param [Array] mime_types One or more mime types to which this parser applies. @yield [String] A block returning parsed content.
Public Instance Methods
Source
# File lib/oauth2/response.rb, line 70 def body response.body || '' end
The HTTP response body
Source
# 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
Attempts to determine the content type of the response.
Source
# File lib/oauth2/response.rb, line 60 def headers response.headers end
The HTTP response headers
Source
# 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 options[:snaky] && @parsed.is_a?(Hash) @parsed end
The {#response} {#body} as parsed by {#parser}.
@return [Object] As returned by {#parser} if it is call-able. @return [nil] If the {#parser} is not call-able.
Source
# File lib/oauth2/response.rb, line 121 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
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.
@return [Proc, call] If a parser was found. @return [nil] If no parser was found.
Source
# File lib/oauth2/response.rb, line 65 def status response.status end
The HTTP response status code