Sha256: 74a4f01ced212cf9de590cf2a0d0c0db2b39af5742f670a93f926358f47682b5

Contents?: true

Size: 884 Bytes

Versions: 12

Compression:

Stored size: 884 Bytes

Contents

require "myfinance/exception"

module Myfinance
  RequestTimeout = Class.new(Exception)
  RequestError   = Class.new(Exception)

  class Response < SimpleDelegator

    def resolve!(&block)
      if success?
        block_given? ? yield(self) : self
      elsif timed_out?
        timeout!
      else
        error!
      end
    end

    def parsed_body(key=nil)
      return MultiJson.load(self.body)[key] unless key.nil?
      MultiJson.load(self.body)
    rescue MultiJson::ParseError
      {}
    end

    private

    def timeout!
      raise RequestTimeout
    end

    def error!
      raise RequestError.new(
        code:    code,
        message: request_error_message,
        body:    parsed_body
      )
    end

    def request_error_message
      return status_message if !status_message.nil? && status_message != ""
      parsed_body["error"] || ""
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
myfinance-1.2.1 lib/myfinance/response.rb
myfinance-1.2.0 lib/myfinance/response.rb
myfinance-1.1.0 lib/myfinance/response.rb
myfinance-1.0.0 lib/myfinance/response.rb
myfinance-0.7.0 lib/myfinance/response.rb
myfinance-0.6.0 lib/myfinance/response.rb
myfinance-0.5.0 lib/myfinance/response.rb
myfinance-0.4.0 lib/myfinance/response.rb
myfinance-0.3.3 lib/myfinance/response.rb
myfinance-0.3.2 lib/myfinance/response.rb
myfinance-0.3.1 lib/myfinance/response.rb
myfinance-0.3.0 lib/myfinance/response.rb