Skip to content

Commit 889e4dc

Browse files
committed
Don't require multi_json and multi_xml.
1 parent 037cd3d commit 889e4dc

23 files changed

+85
-45
lines changed

.rubocop_todo.yml

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,52 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2017-02-19 15:40:46 -0500 using RuboCop version 0.47.1.
3+
# on 2017-05-02 07:44:23 -0400 using RuboCop version 0.47.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 43
9+
# Offense count: 2
10+
Lint/HandleExceptions:
11+
Exclude:
12+
- 'lib/grape/util/json.rb'
13+
- 'lib/grape/util/xml.rb'
14+
15+
# Offense count: 45
1016
Metrics/AbcSize:
1117
Max: 44
1218

13-
# Offense count: 265
19+
# Offense count: 276
1420
# Configuration parameters: CountComments, ExcludedMethods.
1521
Metrics/BlockLength:
1622
Max: 3104
1723

18-
# Offense count: 1
19-
# Configuration parameters: CountBlocks.
20-
Metrics/BlockNesting:
21-
Max: 4
22-
2324
# Offense count: 8
2425
# Configuration parameters: CountComments.
2526
Metrics/ClassLength:
2627
Max: 283
2728

28-
# Offense count: 26
29+
# Offense count: 28
2930
Metrics/CyclomaticComplexity:
3031
Max: 14
3132

32-
# Offense count: 993
33+
# Offense count: 1085
3334
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
3435
# URISchemes: http, https
3536
Metrics/LineLength:
3637
Max: 215
3738

38-
# Offense count: 56
39+
# Offense count: 57
3940
# Configuration parameters: CountComments.
4041
Metrics/MethodLength:
4142
Max: 33
4243

43-
# Offense count: 9
44+
# Offense count: 10
4445
# Configuration parameters: CountComments.
4546
Metrics/ModuleLength:
4647
Max: 212
4748

48-
# Offense count: 16
49+
# Offense count: 18
4950
Metrics/PerceivedComplexity:
5051
Max: 14
5152

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [#1622](https://github.com/ruby-grape/grape/pull/1622): Add `except_values` validator to replace `except` option of `values` validator - [@jlfaber](https://github.com/jlfaber).
77
* [#1635](https://github.com/ruby-grape/grape/pull/1635): Instrument validators with ActiveSupport::Notifications - [@ktimothy](https://github.com/ktimothy).
88
* [#1646](https://github.com/ruby-grape/grape/pull/1646): Add ability to include an array of modules as helpers - [@pablonahuelgomez](https://github.com/pablonahuelgomez).
9+
* [#1623](https://github.com/ruby-grape/grape/pull/1623): Removed `multi_json` and `multi_xml` dependencies - [@dblock](https://github.com/dblock).
910
* Your contribution here.
1011

1112
#### Fixes

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ group :development do
2222
end
2323

2424
group :test do
25+
gem 'multi_json'
26+
gem 'multi_xml'
2527
gem 'grape-entity', '~> 0.6'
2628
gem 'maruku'
2729
gem 'rack-test'

gemfiles/rack_1.5.2.gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ group :development do
2222
end
2323

2424
group :test do
25+
gem 'multi_json'
26+
gem 'multi_xml'
2527
gem 'grape-entity', '~> 0.6'
2628
gem 'maruku'
2729
gem 'rack-test'

gemfiles/rack_edge.gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ group :development do
2222
end
2323

2424
group :test do
25+
gem 'multi_json'
26+
gem 'multi_xml'
2527
gem 'grape-entity', '~> 0.6'
2628
gem 'maruku'
2729
gem 'rack-test'

gemfiles/rails_3.gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ group :development do
2323
end
2424

2525
group :test do
26+
gem 'multi_json'
27+
gem 'multi_xml'
2628
gem 'grape-entity', '~> 0.6'
2729
gem 'maruku'
2830
gem 'rack-test'

gemfiles/rails_4.gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ group :development do
2222
end
2323

2424
group :test do
25+
gem 'multi_json'
26+
gem 'multi_xml'
2527
gem 'grape-entity', '~> 0.6'
2628
gem 'maruku'
2729
gem 'rack-test'

gemfiles/rails_5.gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ group :development do
2222
end
2323

2424
group :test do
25+
gem 'multi_json'
26+
gem 'multi_xml'
2527
gem 'grape-entity', '~> 0.6'
2628
gem 'maruku'
2729
gem 'rack-test'

gemfiles/rails_edge.gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ group :development do
2222
end
2323

2424
group :test do
25+
gem 'multi_json'
26+
gem 'multi_xml'
2527
gem 'grape-entity', '~> 0.6'
2628
gem 'maruku'
2729
gem 'rack-test'

grape.gemspec

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Gem::Specification.new do |s|
1616
s.add_runtime_dependency 'mustermann-grape', '~> 1.0.0'
1717
s.add_runtime_dependency 'rack-accept'
1818
s.add_runtime_dependency 'activesupport'
19-
s.add_runtime_dependency 'multi_json', '>= 1.3.2'
20-
s.add_runtime_dependency 'multi_xml', '>= 0.5.2'
2119
s.add_runtime_dependency 'virtus', '>= 1.0.0'
2220
s.add_runtime_dependency 'builder'
2321

lib/grape.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
require 'active_support/core_ext/hash/conversions'
1717
require 'active_support/dependencies/autoload'
1818
require 'active_support/notifications'
19-
require 'multi_json'
20-
require 'multi_xml'
2119
require 'i18n'
2220
require 'thread'
2321

@@ -44,6 +42,8 @@ module Grape
4442
autoload :Parser
4543
autoload :Request
4644
autoload :Env, 'grape/util/env'
45+
autoload :Json, 'grape/util/json'
46+
autoload :Xml, 'grape/util/xml'
4747
end
4848

4949
module Http

lib/grape/error_formatter/json.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def call(message, backtrace, options = {}, env = nil)
1010
if (options[:rescue_options] || {})[:backtrace] && backtrace && !backtrace.empty?
1111
result = result.merge(backtrace: backtrace)
1212
end
13-
MultiJson.dump(result)
13+
::Grape::Json.dump(result)
1414
end
1515

1616
private

lib/grape/error_formatter/txt.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class << self
77
def call(message, backtrace, options = {}, env = nil)
88
message = present(message, env)
99

10-
result = message.is_a?(Hash) ? MultiJson.dump(message) : message
10+
result = message.is_a?(Hash) ? ::Grape::Json.dump(message) : message
1111
if (options[:rescue_options] || {})[:backtrace] && backtrace && !backtrace.empty?
1212
result += "\r\n "
1313
result += backtrace.join("\r\n ")

lib/grape/formatter/json.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Json
44
class << self
55
def call(object, _env)
66
return object.to_json if object.respond_to?(:to_json)
7-
MultiJson.dump(object)
7+
::Grape::Json.dump(object)
88
end
99
end
1010
end

lib/grape/formatter/serializable_hash.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module SerializableHash
44
class << self
55
def call(object, _env)
66
return object if object.is_a?(String)
7-
return MultiJson.dump(serialize(object)) if serializable?(object)
7+
return ::Grape::Json.dump(serialize(object)) if serializable?(object)
88
return object.to_json if object.respond_to?(:to_json)
9-
MultiJson.dump(object)
9+
::Grape::Json.dump(object)
1010
end
1111

1212
private

lib/grape/parser/json.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module Parser
33
module Json
44
class << self
55
def call(object, _env)
6-
MultiJson.load(object)
7-
rescue MultiJson::ParseError
6+
::Grape::Json.load(object)
7+
rescue ::Grape::Json::ParseError
88
# handle JSON parsing errors via the rescue handlers or provide error message
99
raise Grape::Exceptions::InvalidMessageBody, 'application/json'
1010
end

lib/grape/parser/xml.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module Parser
33
module Xml
44
class << self
55
def call(object, _env)
6-
MultiXml.parse(object)
7-
rescue MultiXml::ParseError
6+
::Grape::Xml.parse(object)
7+
rescue ::Grape::Xml::ParseError
88
# handle XML parsing errors via the rescue handlers or provide error message
99
raise Grape::Exceptions::InvalidMessageBody, 'application/xml'
1010
end

lib/grape/util/json.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
begin
2+
require 'multi_json'
3+
rescue LoadError
4+
end
5+
6+
module Grape
7+
if Object.const_defined? :MultiJson
8+
Json = ::MultiJson
9+
else
10+
Json = ::JSON
11+
Json::ParseError = Json::ParserError
12+
end
13+
end

lib/grape/util/xml.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
begin
2+
require 'multi_xml'
3+
rescue LoadError
4+
end
5+
6+
module Grape
7+
if Object.const_defined? :MultiXml
8+
Xml = ::MultiXml
9+
else
10+
Xml = ::ActiveSupport::XmlMini
11+
Xml::ParseError = StandardError
12+
end
13+
end

spec/grape/api/invalid_format_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ def app
2727
it 'no format' do
2828
get '/foo'
2929
expect(last_response.status).to eq 200
30-
expect(last_response.body).to eq(MultiJson.dump(id: 'foo', format: nil))
30+
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: nil))
3131
end
3232
it 'json format' do
3333
get '/foo.json'
3434
expect(last_response.status).to eq 200
35-
expect(last_response.body).to eq(MultiJson.dump(id: 'foo', format: 'json'))
35+
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: 'json'))
3636
end
3737
it 'invalid format' do
3838
get '/foo.invalid'
3939
expect(last_response.status).to eq 200
40-
expect(last_response.body).to eq(MultiJson.dump(id: 'foo', format: 'invalid'))
40+
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: 'invalid'))
4141
end
4242
end
4343
end

spec/grape/api_spec.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22
require 'shared/versioning_examples'
3-
require 'grape-entity'
3+
# require 'grape-entity'
44

55
describe Grape::API do
66
subject { Class.new(Grape::API) }
@@ -444,29 +444,29 @@ class DummyFormatClass
444444
subject.send(verb) do
445445
env['api.request.body']
446446
end
447-
send verb, '/', MultiJson.dump(object), 'CONTENT_TYPE' => 'application/json'
447+
send verb, '/', ::Grape::Json.dump(object), 'CONTENT_TYPE' => 'application/json'
448448
expect(last_response.status).to eq(verb == :post ? 201 : 200)
449-
expect(last_response.body).to eql MultiJson.dump(object)
449+
expect(last_response.body).to eql ::Grape::Json.dump(object)
450450
expect(last_request.params).to eql({})
451451
end
452452
it 'stores input in api.request.input' do
453453
subject.format :json
454454
subject.send(verb) do
455455
env['api.request.input']
456456
end
457-
send verb, '/', MultiJson.dump(object), 'CONTENT_TYPE' => 'application/json'
457+
send verb, '/', ::Grape::Json.dump(object), 'CONTENT_TYPE' => 'application/json'
458458
expect(last_response.status).to eq(verb == :post ? 201 : 200)
459-
expect(last_response.body).to eql MultiJson.dump(object).to_json
459+
expect(last_response.body).to eql ::Grape::Json.dump(object).to_json
460460
end
461461
context 'chunked transfer encoding' do
462462
it 'stores input in api.request.input' do
463463
subject.format :json
464464
subject.send(verb) do
465465
env['api.request.input']
466466
end
467-
send verb, '/', MultiJson.dump(object), 'CONTENT_TYPE' => 'application/json', 'HTTP_TRANSFER_ENCODING' => 'chunked', 'CONTENT_LENGTH' => nil
467+
send verb, '/', ::Grape::Json.dump(object), 'CONTENT_TYPE' => 'application/json', 'HTTP_TRANSFER_ENCODING' => 'chunked', 'CONTENT_LENGTH' => nil
468468
expect(last_response.status).to eq(verb == :post ? 201 : 200)
469-
expect(last_response.body).to eql MultiJson.dump(object).to_json
469+
expect(last_response.body).to eql ::Grape::Json.dump(object).to_json
470470
end
471471
end
472472
end
@@ -2053,7 +2053,7 @@ def self.call(message, _backtrace, _option, _env)
20532053
raise 'rain!'
20542054
end
20552055
get '/exception'
2056-
json = MultiJson.load(last_response.body)
2056+
json = ::Grape::Json.load(last_response.body)
20572057
expect(json['error']).to eql 'rain!'
20582058
expect(json['backtrace'].length).to be > 0
20592059
end
@@ -3156,7 +3156,7 @@ def static
31563156
end
31573157
it 'path' do
31583158
get '/endpoint/options'
3159-
options = MultiJson.load(last_response.body)
3159+
options = ::Grape::Json.load(last_response.body)
31603160
expect(options['path']).to eq(['/endpoint/options'])
31613161
expect(options['source_location'][0]).to include 'api_spec.rb'
31623162
expect(options['source_location'][1].to_i).to be > 0

spec/grape/endpoint_spec.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def app
429429
''
430430
end
431431

432-
post '/declared', MultiJson.dump(first: 'one', boolean: false), 'CONTENT_TYPE' => 'application/json'
432+
post '/declared', ::Grape::Json.dump(first: 'one', boolean: false), 'CONTENT_TYPE' => 'application/json'
433433
expect(last_response.status).to eq(201)
434434
end
435435

@@ -444,7 +444,7 @@ def app
444444
''
445445
end
446446

447-
post '/declared', MultiJson.dump(first: 'one', second: nil), 'CONTENT_TYPE' => 'application/json'
447+
post '/declared', ::Grape::Json.dump(first: 'one', second: nil), 'CONTENT_TYPE' => 'application/json'
448448
expect(last_response.status).to eq(201)
449449
end
450450

@@ -832,7 +832,7 @@ def app
832832
end
833833

834834
it 'converts JSON bodies to params' do
835-
post '/request_body', MultiJson.dump(user: 'Bobby T.'), 'CONTENT_TYPE' => 'application/json'
835+
post '/request_body', ::Grape::Json.dump(user: 'Bobby T.'), 'CONTENT_TYPE' => 'application/json'
836836
expect(last_response.body).to eq('Bobby T.')
837837
end
838838

@@ -856,7 +856,7 @@ def app
856856
error! 400, 'expected nil' if params[:version]
857857
params[:user]
858858
end
859-
post '/omitted_params', MultiJson.dump(user: 'Bob'), 'CONTENT_TYPE' => 'application/json'
859+
post '/omitted_params', ::Grape::Json.dump(user: 'Bob'), 'CONTENT_TYPE' => 'application/json'
860860
expect(last_response.status).to eq(201)
861861
expect(last_response.body).to eq('Bob')
862862
end
@@ -879,7 +879,7 @@ def app
879879
subject.put '/request_body' do
880880
params[:user]
881881
end
882-
put '/request_body', MultiJson.dump(user: 'Bob'), 'CONTENT_TYPE' => 'text/plain'
882+
put '/request_body', ::Grape::Json.dump(user: 'Bob'), 'CONTENT_TYPE' => 'text/plain'
883883

884884
expect(last_response.status).to eq(406)
885885
expect(last_response.body).to eq('{"error":"The requested content-type \'text/plain\' is not supported."}')
@@ -893,7 +893,7 @@ def app
893893
subject.post do
894894
params[:data]
895895
end
896-
post '/', MultiJson.dump(data: { some: 'payload' }), 'CONTENT_TYPE' => 'application/json'
896+
post '/', ::Grape::Json.dump(data: { some: 'payload' }), 'CONTENT_TYPE' => 'application/json'
897897
end
898898

899899
it 'should not response with 406 for same type without params' do

spec/grape/middleware/formatter_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
let(:body) { { 'abc' => 'def' } }
1212
it 'looks at the bodies for possibly serializable data' do
1313
_, _, bodies = *subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json')
14-
bodies.each { |b| expect(b).to eq(MultiJson.dump(body)) }
14+
bodies.each { |b| expect(b).to eq(::Grape::Json.dump(body)) }
1515
end
1616

1717
context 'default format' do

0 commit comments

Comments
 (0)