class OAuth2::Response

Constants

CONTENT_TYPES

Content type assignments for various potential HTTP content types.

PARSERS

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

Attributes

body[R]
data[RW]
error[RW]
headers[RW]
options[RW]
response[RW]
status[RW]

Public Class Methods

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

Initializes a Response instance

@param params [Hash] @option params [NSData] :data @option params [Hash] :headers @option params [Integer] :status @param data [NSData] @param opts [Hash] 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-cocoa/response.rb, line 16
def initialize(params = {}, opts = {})
  params.each do |key, value|
    self.send("#{key}=", value)
  end
  @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 10
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

content_type() click to toggle source

Attempts to determine the content type of the response.

# File lib/oauth2-cocoa/response.rb, line 30
def content_type
  ((self.headers.values_at("content-type", "Content-Type").compact.first || "").split(";").first || "").strip
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 37
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 43
def parser
  return options[:parse].to_sym if PARSERS.key?(options[:parse])
  CONTENT_TYPES[content_type]
end