Sha256: 1589f17d7aef7c3ce4b59d491da4a3a94204e26240c095766970bb1e1146da25

Contents?: true

Size: 683 Bytes

Versions: 5

Compression:

Stored size: 683 Bytes

Contents

require "emites/exception"

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

  class Response < SimpleDelegator

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

    def redirected?
      response_code && response_code >= 300 && response_code < 400
    end

    private

    def timeout!
      raise RequestTimeout
    end

    def error!
      raise RequestError.new(
        code:    code,
        message: status_message,
        body:    (MultiJson.load(body) rescue {})
      )
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
emites-client-0.1.1 lib/emites/response.rb
emites-client-0.1.0 lib/emites/response.rb
emites-client-0.0.4 lib/emites/response.rb
emites-client-0.0.3 lib/emites/response.rb
emites-client-0.0.2 lib/emites/response.rb