Sha256: 63221949dfbebcdae6dab23b27898a8f3add214bea5f166e11a2c43338548c53

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

module OAuth2
  module ResponseObject
    def self.from(response)
      object = MultiJson.decode(response.body)

      case object
        when Array
          ResponseArray.new(response, object)
        when Hash
          ResponseHash.new(response, object)
        else
          ResponseString.new(response)
      end
    rescue
      ResponseString.new(response)
    end

    def self.included(base)
      base.class_eval do
        attr_accessor :response
      end
    end

    def headers; response.headers end
    def status; response.status end
  end

  class ResponseHash < Hash
    include ResponseObject

    def initialize(response, hash)
      self.response = response
      hash.keys.each{|k| self[k] = hash[k]}
    end
  end

  class ResponseArray < Array
    include ResponseObject

    def initialize(response, array)
      self.response = response
      super(array)
    end
  end

  # This special String class is returned from HTTP requests
  # and contains the original full response along with convenience
  # methods for accessing the HTTP status code and headers. It
  # is returned from all access token requests.
  class ResponseString < String
    include ResponseObject

    def initialize(response)
      super(response.body.to_s)
      self.response = response
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
panjiva-oauth2-0.4.1 lib/oauth2/response_object.rb
oauth2-0.4.1 lib/oauth2/response_object.rb
oauth2-0.4.0 lib/oauth2/response_object.rb