Sha256: b7a10fb02666982f7c5add25046c4496038c832a0a406c2e6c5be2591d682877

Contents?: true

Size: 903 Bytes

Versions: 8

Compression:

Stored size: 903 Bytes

Contents

module Rentjuicer
  class Response
    
    attr_accessor :body
    
    def initialize(response, raise_error = true)
      rash_response(response)
      raise Error.new(self.body.code, self.body.message) if !success? && raise_error
    end
    
    def success?
      self.body.status == "ok"
    end
    
    def method_missing(method_name, *args)
      if self.body.respond_to?(method_name)
        self.body.send(method_name)
      else
        super
      end
    end
    
    private
    
    def rash_response(response)
      if response.is_a?(Array)
        self.body = []
        response.each do |b|
          if b.is_a?(Hash)
            self.body << Hashie::Rash.new(b)
          else
            self.body << b
          end
        end
      elsif response.is_a?(Hash)
        self.body = Hashie::Rash.new(response)
      else
        self.body = response
      end
    end
    
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rentjuicer-0.5.1 lib/rentjuicer/response.rb
rentjuicer-0.5.0 lib/rentjuicer/response.rb
rentjuicer-0.4.4 lib/rentjuicer/response.rb
rentjuicer-0.4.3 lib/rentjuicer/response.rb
rentjuicer-0.4.2 lib/rentjuicer/response.rb
rentjuicer-0.4.1 lib/rentjuicer/response.rb
rentjuicer-0.4.0 lib/rentjuicer/response.rb
rentjuicer-0.3.0 lib/rentjuicer/response.rb