Sha256: 11a4e2ef95c757e610d89c86b9f76add536211d7cc46e4541df04b249571a84f

Contents?: true

Size: 721 Bytes

Versions: 3

Compression:

Stored size: 721 Bytes

Contents

require "emites/exception"

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

  class Response < SimpleDelegator

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

    def redirected?
      (300..308).include?(self.code)
    end

    def parsed_body
      MultiJson.load(body)
    rescue MultiJson::ParseError
      {}
    end

    private

    def timeout!
      raise RequestTimeout
    end

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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
emites-client-0.1.4 lib/emites/response.rb
emites-client-0.1.3 lib/emites/response.rb
emites-client-0.1.2 lib/emites/response.rb