Sha256: 12d23893bb115b85e9fe9c0e661e3cbce71535e78cee6833ee995d1e3a9f537e

Contents?: true

Size: 839 Bytes

Versions: 1

Compression:

Stored size: 839 Bytes

Contents

module Goauth2
  class HashResponse
    attr_accessor :hash, :response
    
    def initialize(response)
      @response = response
      if response.is_a?(Hash)
        @hash = response
      else
        @hash = JSON.parse(response.body)
      end
      @hash = @hash['data'] if @hash['data']
    end
    
    def first
      wrap(@hash.first)
    end
    
    def method_missing(name)
      val = @hash[name] if @hash.key? name
      @hash.each { |k,v| val = v if k.to_s.to_sym == name } unless val
      unless val.nil?
        return wrap(val)
      end
      super.method_missing name
    end
    
    private
      def wrap(val)
        if val.is_a? Hash
          Goauth2::HashResponse.new(val)
        elsif val.is_a? Array
          Goauth2::ArrayResponse.new(val)
        else
          val
        end
      end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
goauth2-0.0.13 lib/goauth2/hash_response.rb