Sha256: 92936c5f3b63b550547e1f23243c03a0140504e039694af136929e8ac33ce944

Contents?: true

Size: 986 Bytes

Versions: 2

Compression:

Stored size: 986 Bytes

Contents

module Evrythng
  # Custom error class for rescuing from all Evrythng errors
  class Error < StandardError
    attr_reader :http_headers

    # Initializes a new Error object
    #
    # @param message [String]
    # @param http_headers [Hash]
    # @return [Evrythng::Error]
    def initialize(message, http_headers)
      @http_headers = Hash[http_headers]
      super(message)
    end

    # @return [Time]
    def ratelimit_reset
      Time.at(@http_headers.values_at('x-ratelimit-reset', 'X-RateLimit-Reset').detect{|value| value}.to_i)
    end

    # @return [Integer]
    def ratelimit_limit
      @http_headers.values_at('x-ratelimit-limit', 'X-RateLimit-Limit').detect{|value| value}.to_i
    end

    # @return [Integer]
    def ratelimit_remaining
      @http_headers.values_at('x-ratelimit-remaining', 'X-RateLimit-Remaining').detect{|value| value}.to_i
    end

    # @return [Integer]
    def retry_after
      [(ratelimit_reset - Time.now).ceil, 0].max
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
evrythng-0.1.1 lib/evrythng/error.rb
evrythng-0.1.0 lib/evrythng/error.rb