Class: HCl::HarvestMiddleware
- Inherits:
-
Faraday::Request::BasicAuthentication
- Object
- Faraday::Request::BasicAuthentication
- HCl::HarvestMiddleware
show all
- Defined in:
- lib/hcl/harvest_middleware.rb
Defined Under Namespace
Classes: AuthFailure, Failure, ThrottleFailure
Constant Summary
collapse
- MIME_TYPE =
'application/json'.freeze
Instance Method Summary
collapse
Instance Method Details
#call(env) ⇒ Object
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
|
# File 'lib/hcl/harvest_middleware.rb', line 12
def call(env)
env[:request_headers]['Accept'] = MIME_TYPE
env[:request_headers]['Content-Type'] = MIME_TYPE
env[:body] = Yajl::Encoder.encode(env[:body])
super(env).on_complete do |env|
case env[:status]
when 200..299
begin
env[:body] = deep_html_unescape(Yajl::Parser.parse(env[:body], symbolize_keys:true))
rescue Yajl::ParseError
env[:body]
end
when 300..399
raise Failure, "Redirected!"
when 400..499
raise AuthFailure, "Login failed."
when 503
raise ThrottleFailure, env
else
raise Failure, "Unexpected response from the upstream API."
end
end
end
|
#deep_html_unescape(obj) ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/hcl/harvest_middleware.rb', line 39
def deep_html_unescape obj
if obj.kind_of? Hash
obj.inject({}){|o,(k,v)| o.update(k => deep_html_unescape(v)) }
elsif obj.kind_of? Array
obj.inject([]){|o,v| o << deep_html_unescape(v) }
else
EscapeUtils.unescape_html(obj.to_s)
end
end
|