Skip to content

Commit dc77375

Browse files
committed
Merge pull request #1333 from namusyaka/update-1330
Update some points, ref #1330
2 parents 44418cc + c98a3ae commit dc77375

File tree

8 files changed

+119
-39
lines changed

8 files changed

+119
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
* [#1325](https://github.com/ruby-grape/grape/pull/1325): Params: Fix coerce_with helper with Array types - [@ngonzalez](https://github.com/ngonzalez).
1313
* [#1326](https://github.com/ruby-grape/grape/pull/1326): Fix wrong behavior for OPTIONS and HEAD requests with catch-all - [@ekampp](https://github.com/ekampp), [@namusyaka](https://github.com/namusyaka).
14-
* [#1330](https://github.com/ruby-grape/grape/pull/1330): Fix built-in parsers and formatters - [@namusyaka](https://github.com/namusyaka).
14+
* [#1330](https://github.com/ruby-grape/grape/pull/1330): Add `register` keyword for adding customized parsers and formatters - [@namusyaka](https://github.com/namusyaka).
1515

1616
0.15.0 (3/8/2016)
1717
=================

grape.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
1313
s.license = 'MIT'
1414

1515
s.add_runtime_dependency 'rack', '>= 1.3.0'
16-
s.add_runtime_dependency 'mustermann19', '~> 0.4.2'
16+
s.add_runtime_dependency 'mustermann19', '~> 0.4.3'
1717
s.add_runtime_dependency 'rack-accept'
1818
s.add_runtime_dependency 'activesupport'
1919
s.add_runtime_dependency 'multi_json', '>= 1.3.2'

lib/grape.rb

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,6 @@ module Exceptions
7777
autoload :MethodNotAllowed
7878
end
7979

80-
module ErrorFormatter
81-
extend ActiveSupport::Autoload
82-
autoload :Base
83-
autoload :Json
84-
autoload :Txt
85-
autoload :Xml
86-
end
87-
88-
module Formatter
89-
extend ActiveSupport::Autoload
90-
autoload :Json
91-
autoload :SerializableHash
92-
autoload :Txt
93-
autoload :Xml
94-
end
95-
96-
module Parser
97-
extend ActiveSupport::Autoload
98-
autoload :Json
99-
autoload :Xml
100-
end
101-
10280
module Middleware
10381
extend ActiveSupport::Autoload
10482
autoload :Base
@@ -130,6 +108,29 @@ module Util
130108
autoload :StackableValues
131109
autoload :InheritableSetting
132110
autoload :StrictHashConfiguration
111+
autoload :Registrable
112+
end
113+
114+
module ErrorFormatter
115+
extend ActiveSupport::Autoload
116+
autoload :Base
117+
autoload :Json
118+
autoload :Txt
119+
autoload :Xml
120+
end
121+
122+
module Formatter
123+
extend ActiveSupport::Autoload
124+
autoload :Json
125+
autoload :SerializableHash
126+
autoload :Txt
127+
autoload :Xml
128+
end
129+
130+
module Parser
131+
extend ActiveSupport::Autoload
132+
autoload :Json
133+
autoload :Xml
133134
end
134135

135136
module DSL

lib/grape/error_formatter.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
require 'grape/util/registrable'
2-
31
module Grape
42
module ErrorFormatter
5-
extend Registrable
3+
extend Util::Registrable
4+
65
class << self
76
def builtin_formatters
87
@builtin_formatters ||= {

lib/grape/formatter.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
require 'grape/util/registrable'
2-
31
module Grape
42
module Formatter
5-
extend Registrable
3+
extend Util::Registrable
64

75
class << self
86
def builtin_formmaters

lib/grape/parser.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
require 'grape/util/registrable'
2-
31
module Grape
42
module Parser
5-
extend Registrable
3+
extend Util::Registrable
64

75
class << self
86
def builtin_parsers

lib/grape/util/registrable.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Grape
2-
module Registrable
3-
def default_elements
4-
@default_elements ||= {}
5-
end
2+
module Util
3+
module Registrable
4+
def default_elements
5+
@default_elements ||= {}
6+
end
67

7-
def register(format, element)
8-
default_elements[format] = element unless default_elements[format]
8+
def register(format, element)
9+
default_elements[format] = element unless default_elements[format]
10+
end
911
end
1012
end
1113
end

spec/grape/parser_spec.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require 'spec_helper'
2+
3+
describe Grape::Parser do
4+
subject { described_class }
5+
6+
describe '.builtin_parsers' do
7+
it 'returns an instance of Hash' do
8+
expect(subject.builtin_parsers).to be_an_instance_of(Hash)
9+
end
10+
11+
it 'includes json and xml parsers by default' do
12+
expect(subject.builtin_parsers).to include(json: Grape::Parser::Json, xml: Grape::Parser::Xml)
13+
end
14+
end
15+
16+
describe '.parsers' do
17+
it 'returns an instance of Hash' do
18+
expect(subject.parsers({})).to be_an_instance_of(Hash)
19+
end
20+
21+
it 'includes built-in parsers' do
22+
expect(subject.parsers({})).to include(subject.builtin_parsers)
23+
end
24+
25+
context 'with :parsers option' do
26+
let(:parsers) { { customized: Class.new } }
27+
it 'includes passed :parsers values' do
28+
expect(subject.parsers(parsers: parsers)).to include(parsers)
29+
end
30+
end
31+
32+
context 'with added parser by using `register` keyword' do
33+
let(:added_parser) { Class.new }
34+
before { subject.register :added, added_parser }
35+
it 'includes added parser' do
36+
expect(subject.parsers({})).to include(added: added_parser)
37+
end
38+
end
39+
end
40+
41+
describe '.parser_for' do
42+
let(:options) { {} }
43+
44+
it 'calls .parsers' do
45+
expect(subject).to receive(:parsers).with(options).and_return(subject.builtin_parsers)
46+
subject.parser_for(:json, options)
47+
end
48+
49+
it 'returns parser correctly' do
50+
expect(subject.parser_for(:json)).to eq(Grape::Parser::Json)
51+
end
52+
53+
context 'when parser is available' do
54+
before { subject.register :customized_json, Grape::Parser::Json }
55+
it 'returns registered parser if available' do
56+
expect(subject.parser_for(:customized_json)).to eq(Grape::Parser::Json)
57+
end
58+
end
59+
60+
context 'when parser is an instance of Symbol' do
61+
before do
62+
allow(subject).to receive(:foo).and_return(:bar)
63+
subject.register :foo, :foo
64+
end
65+
66+
it 'returns an instance of Method' do
67+
expect(subject.parser_for(:foo)).to be_an_instance_of(Method)
68+
end
69+
70+
it 'returns object which can be called' do
71+
method = subject.parser_for(:foo)
72+
expect(method.call).to eq(:bar)
73+
end
74+
end
75+
76+
context 'when parser does not exist' do
77+
it 'returns nil' do
78+
expect(subject.parser_for(:undefined)).to be_nil
79+
end
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)