forked from lostisland/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.rb
35 lines (31 loc) · 1.03 KB
/
json.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
module Faraday
class Request::JSON < Request::UrlEncoded
self.mime_type = 'application/json'.freeze
class << self
attr_writer :adapter
def adapter
@adapter or raise Error::MissingDependency, "No JSON adapter available. Install either activesupport or yajl-ruby."
end
end
# loads the JSON encoder either from yajl-ruby or activesupport
dependency do
begin
require 'yajl'
self.adapter = Yajl::Encoder
rescue LoadError, NameError
require 'active_support/core_ext/module/attribute_accessors' # AS 2.3.11
require 'active_support/core_ext/kernel/reporting' # AS 2.3.11
require 'active_support/json/encoding'
require 'active_support/ordered_hash' # AS 3.0.4
self.adapter = ActiveSupport::JSON
end
end
def call(env)
match_content_type(env) do |data|
# encode with the first successfully loaded adapter
env[:body] = self.class.adapter.encode data
end
@app.call env
end
end
end