Sha256: bd663481eeb3caea492006cd1c7524a4283bd711bc77808b3c31164221b79837

Contents?: true

Size: 972 Bytes

Versions: 8

Compression:

Stored size: 972 Bytes

Contents

module Rentjuicer
  class Response
    
    attr_accessor :body
    
    def initialize(response, raise_error = false)
      rash_response(response)
      raise Error.new(self.body.code, self.body.message) if !success? && raise_error
    end
    
    def success?
      self.body && !self.body.blank? && self.body.respond_to?(:status) && 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.8.0 lib/rentjuicer/response.rb
rentjuicer-0.7.3 lib/rentjuicer/response.rb
rentjuicer-0.7.2 lib/rentjuicer/response.rb
rentjuicer-0.7.1 lib/rentjuicer/response.rb
rentjuicer-0.6.3 lib/rentjuicer/response.rb
rentjuicer-0.6.2 lib/rentjuicer/response.rb
rentjuicer-0.6.1 lib/rentjuicer/response.rb
rentjuicer-0.6.0 lib/rentjuicer/response.rb