Class: OAuth2::Response

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

Overview

OAuth2::Response class

Constant Summary collapse

@@parsers =

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

{
  :query => lambda { |body| Rack::Utils.parse_query(body) },
  :text  => lambda { |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, opts = {}) ⇒ Response

Initializes a Response instance

Parameters:

  • response (Faraday::Response)

    The Faraday response instance

  • opts (Hash) (defaults to: {})

    options in which to initialize the instance

Options Hash (opts):

  • :parse (Symbol) — default: :automatic

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


43
44
45
46
# File 'lib/oauth2/response.rb', line 43

def initialize(response, opts = {})
  @response = response
  @options = {:parse => :automatic}.merge(opts)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options


9
10
11
# File 'lib/oauth2/response.rb', line 9

def options
  @options
end

#responseObject (readonly)

Returns the value of attribute response


8
9
10
# File 'lib/oauth2/response.rb', line 8

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.


29
30
31
32
33
34
35
# File 'lib/oauth2/response.rb', line 29

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


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

def body
  response.body || ''
end

#content_typeObject

Attempts to determine the content type of the response.


84
85
86
87
# File 'lib/oauth2/response.rb', line 84

def content_type
  return nil unless response.headers
  ((response.headers.values_at('content-type', 'Content-Type').compact.first || '').split(';').first || '').strip
end

#headersObject

The HTTP response headers


49
50
51
# File 'lib/oauth2/response.rb', line 49

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.


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/oauth2/response.rb', line 67

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
end

#parserProc, ...

Determines the parser (a Proc or other Object which responds to #call) that will be passed the #body (and optionall #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.


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/oauth2/response.rb', line 105

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


54
55
56
# File 'lib/oauth2/response.rb', line 54

def status
  response.status
end