Skip to content

Commit

Permalink
Drop multijson dependency (#385)
Browse files Browse the repository at this point in the history
* Replace Multijson by Grape::Entity::Json
Add require spec_helper in rspec file
Add spec

* Fix rubocop
  • Loading branch information
ericproulx authored May 10, 2024
1 parent e71e3cd commit d42199e
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--require spec_helper
--color
--profile
--format documentation
2 changes: 0 additions & 2 deletions grape-entity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'

s.add_runtime_dependency 'activesupport', '>= 3.0.0'
# FIXME: remove dependecy
s.add_runtime_dependency 'multi_json', '>= 1.3.2'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
Expand Down
4 changes: 2 additions & 2 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'multi_json'
require 'grape_entity/json'

module Grape
# An Entity is a lightweight structure that allows you to easily
Expand Down Expand Up @@ -558,7 +558,7 @@ def is_defined_in_entity?(attribute)

def to_json(options = {})
options = options.to_h if options&.respond_to?(:to_h)
MultiJson.dump(serializable_hash(options))
Grape::Entity::Json.dump(serializable_hash(options))
end

def to_xml(options = {})
Expand Down
12 changes: 12 additions & 0 deletions lib/grape_entity/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Grape
class Entity
if defined?(::MultiJson)
Json = ::MultiJson
else
Json = ::JSON
Json::ParseError = Json::ParserError
end
end
end
11 changes: 10 additions & 1 deletion spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'
require 'ostruct'

describe Grape::Entity do
Expand Down Expand Up @@ -1755,6 +1754,16 @@ class NoPathCharacterEntity < Grape::Entity
end
end

describe '#to_json' do
before do
fresh_class.expose :name
end

it 'returns a json' do
expect(fresh_class.new(model).to_json).to eq(JSON.generate(attributes.slice(:name)))
end
end

describe '#inspect' do
before do
fresh_class.class_eval do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Grape::Entity::Exposure::NestingExposure::NestedExposures do
subject(:nested_exposures) { described_class.new([]) }

Expand Down
2 changes: 0 additions & 2 deletions spec/grape_entity/exposure/represent_exposure_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Grape::Entity::Exposure::RepresentExposure do
subject(:exposure) { described_class.new(:foo, {}, {}, double, double) }

Expand Down
2 changes: 0 additions & 2 deletions spec/grape_entity/exposure_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Grape::Entity::Exposure do
let(:fresh_class) { Class.new(Grape::Entity) }
let(:model) { double(attributes) }
Expand Down
2 changes: 0 additions & 2 deletions spec/grape_entity/hash_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Grape::Entity do
it 'except option for nested entity', :aggregate_failures do
module EntitySpec
Expand Down
7 changes: 7 additions & 0 deletions spec/grape_entity/json_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

describe Grape::Entity::Json do
subject { described_class }

it { is_expected.to eq(JSON) }
end
2 changes: 0 additions & 2 deletions spec/grape_entity/options_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe Grape::Entity::Options do
module EntitySpec
class Crystalline
Expand Down

0 comments on commit d42199e

Please sign in to comment.