Class: OAuth2::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/oauth2/response.rb

Overview

OAuth2::Response class

Constant Summary collapse

DEFAULT_OPTIONS =
{
  parse: :automatic,
  snaky: true,
}.freeze
@@parsers =

Procs that, when called, will parse a response body according to the specified format.

{
  query: ->(body) { Rack::Utils.parse_query(body) },
  text: ->(body) { body },
}
@@content_types =

Content type assignments for various potential HTTP content types.

{
  'application/x-www-form-urlencoded' => :query,
  'text/plain' => :text,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, parse: :automatic, snaky: true, **options) ⇒ Response

Initializes a Response instance

Parameters:

  • response (Faraday::Response)

    The Faraday response instance

  • parse (Symbol) (defaults to: :automatic)

    (:automatic) how to parse the response body. one of :query (for x-www-form-urlencoded), :json, or :automatic (determined by Content-Type response header)

  • snaky (true, false) (defaults to: true)

    (true) Convert @parsed to a snake-case, indifferent-access SnakyHash::StringKeyed, which is a subclass of Hashie::Mash (from hashie gem)?

  • options (Hash)

    all other options for initializing the instance



51
52
53
54
55
56
57
# 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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/oauth2/response.rb', line 15

def options
  @options
end

#responseObject (readonly)

Returns the value of attribute response.



14
15
16
# File 'lib/oauth2/response.rb', line 14

def response
  @response
end

Class Method Details

.register_parser(key, mime_types) {|String| ... } ⇒ Object

Adds a new content type parser.

Parameters:

  • key (Symbol)

    A descriptive symbol key such as :json or :query.

  • mime_types (Array)

    One or more mime types to which this parser applies.

Yields:

  • (String)

    A block returning parsed content.



35
36
37
38
39
40
41
# 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

Instance Method Details

#bodyObject

The HTTP response body



70
71
72
# File 'lib/oauth2/response.rb', line 70

def body
  response.body || ''
end

#content_typeObject

Attempts to determine the content type of the response.



99
100
101
102
103
# 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

#headersObject

The HTTP response headers



60
61
62
# File 'lib/oauth2/response.rb', line 60

def headers
  response.headers
end

#parsedObject?

The #response #body as parsed by #parser.

Returns:

  • (Object)

    As returned by #parser if it is #call-able.

  • (nil)

    If the #parser is not #call-able.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

#parserProc, ...

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.

Returns:

  • (Proc, #call)

    If a parser was found.

  • (nil)

    If no parser was found.



121
122
123
124
125
126
127
128
129
130
131
132
# 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

#statusObject

The HTTP response status code



65
66
67
# File 'lib/oauth2/response.rb', line 65

def status
  response.status
end