Skip to content

Commit 3def84a

Browse files
author
Ted Kulp
committed
Updated patch to test cleanly against master
1 parent c15d9bb commit 3def84a

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

lib/grape/middleware/base.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'multi_json'
2+
13
module Grape
24
module Middleware
35
class Base
@@ -53,11 +55,18 @@ module Formats
5355
:json => :encode_json,
5456
:txt => :encode_txt,
5557
}
58+
PARSERS = {
59+
:json => :decode_json
60+
}
5661

5762
def formatters
5863
FORMATTERS.merge(options[:formatters] || {})
5964
end
6065

66+
def parsers
67+
PARSERS.merge(options[:parsers] || {})
68+
end
69+
6170
def content_types
6271
CONTENT_TYPES.merge(options[:content_types] || {})
6372
end
@@ -82,6 +91,35 @@ def formatter_for(api_format)
8291
end
8392
end
8493

94+
def parser_for(api_format)
95+
spec = parsers[api_format]
96+
case spec
97+
when nil
98+
nil
99+
when Symbol
100+
method(spec)
101+
else
102+
spec
103+
end
104+
end
105+
106+
def decode_json(object)
107+
MultiJson.decode(object)
108+
end
109+
110+
def encode_json(object)
111+
if object.respond_to? :serializable_hash
112+
MultiJson.encode(object.serializable_hash)
113+
elsif object.respond_to? :to_json
114+
object.to_json
115+
else
116+
MultiJson.encode(object)
117+
end
118+
end
119+
120+
def encode_txt(object)
121+
object.respond_to?(:to_txt) ? object.to_txt : object.to_s
122+
end
85123
end
86124

87125
end

lib/grape/middleware/formatter.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'grape/middleware/base'
2-
require 'multi_json'
32

43
module Grape
54
module Middleware
@@ -83,20 +82,6 @@ def after
8382
headers['Content-Type'] = content_types[env['api.format']]
8483
Rack::Response.new(bodymap, status, headers).to_a
8584
end
86-
87-
def encode_json(object)
88-
if object.respond_to? :serializable_hash
89-
MultiJson.encode(object.serializable_hash)
90-
elsif object.respond_to? :to_json
91-
object.to_json
92-
else
93-
MultiJson.encode(object)
94-
end
95-
end
96-
97-
def encode_txt(object)
98-
object.respond_to?(:to_txt) ? object.to_txt : object.to_s
99-
end
10085
end
10186
end
10287
end

0 commit comments

Comments
 (0)