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.

{
  # Can't reliably detect whether MultiJson responds to load, since it's
  # a reserved word. Use adapter as a proxy for new features.
  :json  => lambda{ |body| MultiJson.respond_to?(:adapter) ? MultiJson.load(body) : MultiJson.decode(body) rescue body },
  :query => lambda{ |body| Rack::Utils.parse_query(body) },
  :text  => lambda{ |body| body }
}
CONTENT_TYPES =

Content type assignments for various potential HTTP content types.

{
  'application/json' => :json,
  'text/javascript' => :json,
  '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)



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

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

Instance Attribute Details

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#responseObject (readonly)

Returns the value of attribute response.



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

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.

  • One (Array)

    or more mime types to which this parser applies.

Yields:

  • (String)

    A block returning parsed content.



15
16
17
18
19
20
21
# File 'lib/oauth2/response.rb', line 15

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



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

def body
  response.body || ''
end

#content_typeObject

Attempts to determine the content type of the response.



76
77
78
# File 'lib/oauth2/response.rb', line 76

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

#headersObject

The HTTP response headers



35
36
37
# File 'lib/oauth2/response.rb', line 35

def headers
  response.headers
end

#parsedObject

The parsed response body.

Will attempt to parse application/x-www-form-urlencoded and
application/json Content-Type response bodies


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

def parsed
  return nil unless PARSERS.key?(parser)
  @parsed ||= PARSERS[parser].call(body)
end

#parserObject

Determines the parser that will be used to supply the content of #parsed



81
82
83
84
# File 'lib/oauth2/response.rb', line 81

def parser
  return options[:parse].to_sym if PARSERS.key?(options[:parse])
  CONTENT_TYPES[content_type]
end

#statusObject

The HTTP response status code



40
41
42
# File 'lib/oauth2/response.rb', line 40

def status
  response.status
end