forked from facebookarchive/instagram-ruby-gem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raise_http_exception.rb
45 lines (39 loc) · 1.3 KB
/
raise_http_exception.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
require 'faraday'
# @private
module FaradayMiddleware
# @private
class RaiseHttpException < Faraday::Middleware
def call(env)
@app.call(env).on_complete do |response|
case response[:status].to_i
when 400
raise Instagram::BadRequest, error_message_400(response)
when 404
raise Instagram::NotFound, error_message_400(response)
when 500
raise Instagram::InternalServerError, error_message_500(response, "Something is technically wrong.")
when 503
raise Instagram::ServiceUnavailable, error_message_500(response, "Instagram is rate limiting your requests.")
end
end
end
def initialize(app)
super app
@parser = nil
end
private
def error_message_400(response)
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
end
def error_body(body)
if body.nil?
nil
elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
": #{body['meta']['error_message']}"
end
end
def error_message_500(response, body=nil)
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
end
end
end