Skip to content

Commit

Permalink
Merge pull request #1333 from namusyaka/update-1330
Browse files Browse the repository at this point in the history
Update some points, ref #1330
  • Loading branch information
dblock committed Mar 18, 2016
2 parents 44418cc + c98a3ae commit dc77375
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

* [#1325](https://github.com/ruby-grape/grape/pull/1325): Params: Fix coerce_with helper with Array types - [@ngonzalez](https://github.com/ngonzalez).
* [#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).
* [#1330](https://github.com/ruby-grape/grape/pull/1330): Fix built-in parsers and formatters - [@namusyaka](https://github.com/namusyaka).
* [#1330](https://github.com/ruby-grape/grape/pull/1330): Add `register` keyword for adding customized parsers and formatters - [@namusyaka](https://github.com/namusyaka).

0.15.0 (3/8/2016)
=================
Expand Down
2 changes: 1 addition & 1 deletion grape.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.add_runtime_dependency 'rack', '>= 1.3.0'
s.add_runtime_dependency 'mustermann19', '~> 0.4.2'
s.add_runtime_dependency 'mustermann19', '~> 0.4.3'
s.add_runtime_dependency 'rack-accept'
s.add_runtime_dependency 'activesupport'
s.add_runtime_dependency 'multi_json', '>= 1.3.2'
Expand Down
45 changes: 23 additions & 22 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,6 @@ module Exceptions
autoload :MethodNotAllowed
end

module ErrorFormatter
extend ActiveSupport::Autoload
autoload :Base
autoload :Json
autoload :Txt
autoload :Xml
end

module Formatter
extend ActiveSupport::Autoload
autoload :Json
autoload :SerializableHash
autoload :Txt
autoload :Xml
end

module Parser
extend ActiveSupport::Autoload
autoload :Json
autoload :Xml
end

module Middleware
extend ActiveSupport::Autoload
autoload :Base
Expand Down Expand Up @@ -130,6 +108,29 @@ module Util
autoload :StackableValues
autoload :InheritableSetting
autoload :StrictHashConfiguration
autoload :Registrable
end

module ErrorFormatter
extend ActiveSupport::Autoload
autoload :Base
autoload :Json
autoload :Txt
autoload :Xml
end

module Formatter
extend ActiveSupport::Autoload
autoload :Json
autoload :SerializableHash
autoload :Txt
autoload :Xml
end

module Parser
extend ActiveSupport::Autoload
autoload :Json
autoload :Xml
end

module DSL
Expand Down
5 changes: 2 additions & 3 deletions lib/grape/error_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'grape/util/registrable'

module Grape
module ErrorFormatter
extend Registrable
extend Util::Registrable

class << self
def builtin_formatters
@builtin_formatters ||= {
Expand Down
4 changes: 1 addition & 3 deletions lib/grape/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'grape/util/registrable'

module Grape
module Formatter
extend Registrable
extend Util::Registrable

class << self
def builtin_formmaters
Expand Down
4 changes: 1 addition & 3 deletions lib/grape/parser.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'grape/util/registrable'

module Grape
module Parser
extend Registrable
extend Util::Registrable

class << self
def builtin_parsers
Expand Down
14 changes: 8 additions & 6 deletions lib/grape/util/registrable.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Grape
module Registrable
def default_elements
@default_elements ||= {}
end
module Util
module Registrable
def default_elements
@default_elements ||= {}
end

def register(format, element)
default_elements[format] = element unless default_elements[format]
def register(format, element)
default_elements[format] = element unless default_elements[format]
end
end
end
end
82 changes: 82 additions & 0 deletions spec/grape/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'spec_helper'

describe Grape::Parser do
subject { described_class }

describe '.builtin_parsers' do
it 'returns an instance of Hash' do
expect(subject.builtin_parsers).to be_an_instance_of(Hash)
end

it 'includes json and xml parsers by default' do
expect(subject.builtin_parsers).to include(json: Grape::Parser::Json, xml: Grape::Parser::Xml)
end
end

describe '.parsers' do
it 'returns an instance of Hash' do
expect(subject.parsers({})).to be_an_instance_of(Hash)
end

it 'includes built-in parsers' do
expect(subject.parsers({})).to include(subject.builtin_parsers)
end

context 'with :parsers option' do
let(:parsers) { { customized: Class.new } }
it 'includes passed :parsers values' do
expect(subject.parsers(parsers: parsers)).to include(parsers)
end
end

context 'with added parser by using `register` keyword' do
let(:added_parser) { Class.new }
before { subject.register :added, added_parser }
it 'includes added parser' do
expect(subject.parsers({})).to include(added: added_parser)
end
end
end

describe '.parser_for' do
let(:options) { {} }

it 'calls .parsers' do
expect(subject).to receive(:parsers).with(options).and_return(subject.builtin_parsers)
subject.parser_for(:json, options)
end

it 'returns parser correctly' do
expect(subject.parser_for(:json)).to eq(Grape::Parser::Json)
end

context 'when parser is available' do
before { subject.register :customized_json, Grape::Parser::Json }
it 'returns registered parser if available' do
expect(subject.parser_for(:customized_json)).to eq(Grape::Parser::Json)
end
end

context 'when parser is an instance of Symbol' do
before do
allow(subject).to receive(:foo).and_return(:bar)
subject.register :foo, :foo
end

it 'returns an instance of Method' do
expect(subject.parser_for(:foo)).to be_an_instance_of(Method)
end

it 'returns object which can be called' do
method = subject.parser_for(:foo)
expect(method.call).to eq(:bar)
end
end

context 'when parser does not exist' do
it 'returns nil' do
expect(subject.parser_for(:undefined)).to be_nil
end
end
end
end

0 comments on commit dc77375

Please sign in to comment.