class OAuth2::Response

OAuth2::Response class

Constants

DEFAULT_OPTIONS

Attributes

options[RW]
response[R]

Public Class Methods

new(response, parse: :automatic, snaky: true, **options) click to toggle source

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

# 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
register_parser(key, mime_types, &block) click to toggle source

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.

# 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

Public Instance Methods

body() click to toggle source

The HTTP response body

# File lib/oauth2/response.rb, line 70
def body
  response.body || ''
end
content_type() click to toggle source

Attempts to determine the content type of the response.

# 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
headers() click to toggle source

The HTTP response headers

# File lib/oauth2/response.rb, line 60
def headers
  response.headers
end
parsed() click to toggle source

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.

# 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
parser() click to toggle source

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.

# 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
status() click to toggle source

The HTTP response status code

# File lib/oauth2/response.rb, line 65
def status
  response.status
end