class OAuth2::Response

OAuth2::Response class

Attributes

error[RW]
options[RW]
response[R]

Public Class Methods

new(response, opts = {}) click to toggle source

Initializes a Response instance

@param [Faraday::Response] response The Faraday response instance @param [Hash] opts options in which to initialize the instance @option opts [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)
# File lib/oauth2/response.rb, line 46
def initialize(response, opts = {})
  @response = response
  @options = {:parse => :automatic}.merge(opts)
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] One or more mime types to which this parser applies. @yield [String] A block returning parsed content.

# File lib/oauth2/response.rb, line 32
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 62
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 75
def content_type
  ((response.headers.values_at('content-type', 'Content-Type').compact.first || '').split(';').first || '').strip
end
headers() click to toggle source

The HTTP response headers

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

The parsed response body.

Will attempt to parse application/x-www-form-urlencoded and
application/json Content-Type response bodies
# File lib/oauth2/response.rb, line 69
def parsed
  return nil unless @@parsers.key?(parser)
  @parsed ||= @@parsers[parser].call(body)
end
parser() click to toggle source

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

# File lib/oauth2/response.rb, line 80
def parser
  return options[:parse].to_sym if @@parsers.key?(options[:parse])
  @@content_types[content_type]
end
status() click to toggle source

The HTTP response status code

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