From 6323f50d65ef2d12f4e73a91712de487a3361bdc Mon Sep 17 00:00:00 2001 From: Rodion Vlasov Date: Sun, 2 Oct 2016 22:33:53 +0300 Subject: [PATCH 1/2] fix no-method-error with Grape::Entity < 0.5.0 --- lib/rack/grape/endpoint_json.rb | 4 ++-- lib/rack/profiler.rb | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/rack/grape/endpoint_json.rb b/lib/rack/grape/endpoint_json.rb index b09c806..1311067 100644 --- a/lib/rack/grape/endpoint_json.rb +++ b/lib/rack/grape/endpoint_json.rb @@ -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 diff --git a/lib/rack/profiler.rb b/lib/rack/profiler.rb index 4a33c62..17d8e6c 100644 --- a/lib/rack/profiler.rb +++ b/lib/rack/profiler.rb @@ -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) && + defined?(::GrapeEntity::VERSION) && + Gem::Version.new(::GrapeEntity::VERSION) < Gem::Version.new("0.5.0")) ::Grape::Endpoint.include Rack::Grape::EndpointJson end end From add60d8ac7db61824ad33bc7c5805c4947d5ef7a Mon Sep 17 00:00:00 2001 From: Rodion Vlasov Date: Sun, 2 Oct 2016 22:34:07 +0300 Subject: [PATCH 2/2] add tests for Grape endpoint --- Gemfile | 6 +++ spec/rack/grape/endpoint_json_spec.rb | 59 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 spec/rack/grape/endpoint_json_spec.rb diff --git a/Gemfile b/Gemfile index c39c99e..0cb6633 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/spec/rack/grape/endpoint_json_spec.rb b/spec/rack/grape/endpoint_json_spec.rb new file mode 100644 index 0000000..cc734bb --- /dev/null +++ b/spec/rack/grape/endpoint_json_spec.rb @@ -0,0 +1,59 @@ +require "json" +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