-
Notifications
You must be signed in to change notification settings - Fork 13
/
json_response.rb
48 lines (40 loc) · 1.44 KB
/
json_response.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
require 'rest-core/util/json'
module RestCore
class JsonResponse
def self.members; [:json_response]; end
include Middleware
class ParseError < Json.const_get(:ParseError)
attr_reader :cause, :body
def initialize cause, body
msg = cause.message.force_encoding('utf-8')
super("#{msg}\nOriginal text: #{body}")
@cause, @body = cause, body
end
end
JSON_RESPONSE_HEADER = {'Accept' => 'application/json'}.freeze
def call env, &k
return app.call(env, &k) if env[DRY]
return app.call(env, &k) unless json_response(env)
app.call(env.merge(REQUEST_HEADERS =>
JSON_RESPONSE_HEADER.merge(env[REQUEST_HEADERS]||{}))){ |response|
yield(process(response))
}
end
def process response
body = response[RESPONSE_BODY]
json = if body.kind_of?(String)
Json.normalize(body)
else
# Yajl supports streaming, so let's pass it directly to make
# it possible to do streaming here. Although indeed we should
# use RESPONSE_SOCKET in this case, but doing that could
# introduce some incompatibility which I don't want to take
# care of for now.
body
end
response.merge(RESPONSE_BODY => json && Json.decode(json))
rescue Json.const_get(:ParseError) => error
fail(response, ParseError.new(error, body))
end
end
end