Sha256: 27ef7496132be71648e0a7772918175979050defd68f1ce3f0220450d6c79405

Contents?: true

Size: 1.81 KB

Versions: 13

Compression:

Stored size: 1.81 KB

Contents

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

    def initialize(message, http_headers)
      @http_headers = Hash[http_headers]
      super message
    end

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

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

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

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

  # Raised when AngellistApi returns the HTTP status code 400
  class BadRequest < Error; end

  # Raised when AngellistApi returns the HTTP status code 401
  class Unauthorized < Error; end

  # Raised when AngellistApi returns the HTTP status code 403
  class Forbidden < Error; end

  # Raised when AngellistApi returns the HTTP status code 404
  class NotFound < Error; end

  # Raised when AngellistApi returns the HTTP status code 406
  class NotAcceptable < Error; end

  # Raised when AngellistApi returns the HTTP status code 420
  class EnhanceYourCalm < Error
    # The number of seconds your application should wait before requesting date from the API again
    def retry_after
      @http_headers.values_at('retry-after', 'Retry-After').detect {|value| value }.to_i
    end
  end

  # Raised when AngellistApi returns the HTTP status code 500
  class InternalServerError < Error; end

  # Raised when AngellistApi returns the HTTP status code 502
  class BadGateway < Error; end

  # Raised when AngellistApi returns the HTTP status code 503
  class ServiceUnavailable < Error; end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
angellist_api-1.0.1 lib/angellist_api/error.rb
angellist_api-0.1.2 lib/angellist_api/error.rb
angellist_api-1.0.0 lib/angellist_api/error.rb
angellist_api-0.1.0 lib/angellist_api/error.rb
angellist_api-0.0.9 lib/angellist_api/error.rb
angellist_api-0.0.8 lib/angellist_api/error.rb
angellist_api-0.0.7 lib/angellist_api/error.rb
angellist_api-0.0.6 lib/angellist_api/error.rb
angellist_api-0.0.5 lib/angellist_api/error.rb
angellist_api-0.0.4 lib/angellist_api/error.rb
angellist_api-0.0.3 lib/angellist_api/error.rb
angellist_api-0.0.2 lib/angellist_api/error.rb
angellist_api-0.0.1 lib/angellist_api/error.rb