/
error.rb
67 lines (51 loc) · 1.98 KB
/
error.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Assistly
# Custom error class for rescuing from all Assistly errors
class Error < StandardError
attr_reader :http_headers
def initialize(message, http_headers)
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-limit', 'X-RateLimit-Limit').detect {|value| value }.to_i
end
def retry_after
[(ratelimit_reset - Time.now).ceil, 0].max
end
end
# Raised when Assistly returns the HTTP status code 400
class BadRequest < Error; end
# Raised when Assistly returns the HTTP status code 401
class Unauthorized < Error; end
# Raised when Assistly returns the HTTP status code 403
class Forbidden < Error; end
# Raised when Assistly returns the HTTP status code 404
class NotFound < Error; end
# Raised when Assistly returns the HTTP status code 406
class NotAcceptable < Error; end
# Raised when Assistly returns the HTTP status code 420
class EnhanceYourCalm < Error
# The number of seconds your application should wait before requesting date from the Search API again
#
# @see http://dev.desk.com/pages/rate-limiting
def retry_after
@http_headers.values_at('retry-after', 'Retry-After').detect {|value| value }.to_i
end
end
# Raised when Assistly returns the HTTP status code 500
class InternalServerError < Error; end
# Raised when Assistly returns the HTTP status code 502
class BadGateway < Error; end
# Raised when Assistly returns the HTTP status code 503
class ServiceUnavailable < Error; end
# Gem Specific Errors
class AssistlyError < StandardError; end
class SupportEmailNotSet < AssistlyError; end
end