Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no-method-error with Grape::Entity < 0.5.0 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ source 'https://rubygems.org'
gemspec

gem 'codeclimate-test-reporter', group: :test, require: nil

group :test do
gem "rack-test"
gem "grape"
gem "grape-entity"
end
4 changes: 2 additions & 2 deletions lib/rack/grape/endpoint_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Rack
module Grape
module EndpointJson
def as_json(options = nil)
return {}.as_json(options) if body.nil?
body.to_hash.as_json(options)
return {}.as_json(options) if body.nil? || !body.respond_to?(:as_json)
body.as_json(options)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rack/profiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def initialize(app, &block)
subscribe_to_default
block.call(self) if block_given?

if defined?(::Grape::Endpoint)
# This patch is required because of bug with Grape-Entity
# which is fixed in version 0.5.0
if (defined?(::Grape::Endpoint) &&

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use parentheses around the condition of an if.

defined?(::GrapeEntity::VERSION) &&
Gem::Version.new(::GrapeEntity::VERSION) < Gem::Version.new("0.5.0"))
::Grape::Endpoint.include Rack::Grape::EndpointJson
end
end
Expand Down
59 changes: 59 additions & 0 deletions spec/rack/grape/endpoint_json_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "json"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing frozen string literal comment.

require "spec_helper"
require "grape"
require "grape_entity"
require "rack/test"
require "rack/profiler"

describe Rack::Grape::EndpointJson do
include Rack::Test::Methods
subject { Class.new(Grape::API) }
let(:sample_hash) { { ping: :pong } }

def app
subject
end

describe "#as_json" do
before do
subject.use Rack::Profiler
end

it "working with String" do
subject.get "/ping" do
"pong"
end
get "/ping?rack-profiler"
expect(JSON.parse(last_response.body)["response"]["body"]).to eql("pong")
end

it "working with NULL" do
subject.get "/ping" do
nil
end
get "/ping?rack-profiler"
expect(JSON.parse(last_response.body)["response"]["body"]).to eql("")
end

it "working with #present" do
entity_mock = Object.new
allow(entity_mock).to receive(:represent).and_return(sample_hash.to_json)

subject.get "/ping" do
present Object.new, with: entity_mock
end
get "/ping?rack-profiler"
expect(JSON.parse(last_response.body)["response"]["body"]).to eql(sample_hash.to_json)
end

it "working with Grape::Entity" do
entity = Class.new(Grape::Entity) { expose :ping }

subject.get "/ping" do
entity.represent({ ping: :pong }).to_json
end
get "/ping?rack-profiler"
expect(JSON.parse(last_response.body)["response"]["body"]).to eql(sample_hash.to_json)
end
end
end