Skip to content

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

Open
brlo wants to merge 2 commits intodawanda:masterfrom
brlo:patch-1
Open

fix: no-method-error with Grape::Entity < 0.5.0#9
brlo wants to merge 2 commits intodawanda:masterfrom
brlo:patch-1

Conversation

@brlo
Copy link
Copy Markdown

@brlo brlo commented Sep 7, 2016

There is no method 'to_hash' for Grape::Entity instance. Just call 'as_json' directly.

https://github.com/ruby-grape/grape-entity/blob/07bdc590754a35750a17554beedf3b8025fc4c08/lib/grape_entity/entity.rb#L488

@lucatironi
Copy link
Copy Markdown
Contributor

Thank you for the PR. Can you provide a spec? I am not using Grape and the previous introduction of this feature was not tested.

I also updated the ruby version(s) for Travis on master. Can you rebase so we can run the build?

@lucatironi
Copy link
Copy Markdown
Contributor

@brlo just pinging since I didn't mention you in the original comment.

@brlo
Copy link
Copy Markdown
Author

brlo commented Sep 22, 2016

No problem, but need a few days more.

@lucatironi
Copy link
Copy Markdown
Contributor

Thanks a lot!

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Line is too long. [92/80]

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
subject.get '/ping' do
entity.represent({ping: :pong}).to_json
end
get '/ping?rack-profiler'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
entity = Class.new(Grape::Entity) { expose :ping }

subject.get '/ping' do
entity.represent({ping: :pong}).to_json
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Redundant curly braces around a hash parameter.
Space inside { missing.
Space inside } missing.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
it 'working with Grape::Entity' do
entity = Class.new(Grape::Entity) { expose :ping }

subject.get '/ping' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
expect(JSON.parse(last_response.body)['response']['body']).to eql(sample_hash.to_json)
end

it 'working with Grape::Entity' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Line is too long. [92/80]

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
subject.get '/ping' do
present Object.new, with: entity_mock
end
get '/ping?rack-profiler'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
entity_mock = Object.new
allow(entity_mock).to receive(:represent).and_return(sample_hash.to_json)

subject.get '/ping' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
expect(JSON.parse(last_response.body)['response']['body']).to eql('')
end

it 'working with #present' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
nil
end
get '/ping?rack-profiler'
expect(JSON.parse(last_response.body)['response']['body']).to eql('')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
subject.get '/ping' do
nil
end
get '/ping?rack-profiler'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
end

it 'working with NULL' do
subject.get '/ping' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
expect(JSON.parse(last_response.body)['response']['body']).to eql('pong')
end

it 'working with NULL' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
'pong'
end
get '/ping?rack-profiler'
expect(JSON.parse(last_response.body)['response']['body']).to eql('pong')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
subject.get '/ping' do
'pong'
end
get '/ping?rack-profiler'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated

it 'working with String' do
subject.get '/ping' do
'pong'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Comment thread spec/rack/grape/endpoint_json_spec.rb Outdated
describe Rack::Grape::EndpointJson do
include Rack::Test::Methods
subject { Class.new(Grape::API) }
let(:sample_hash){ {ping: :pong} }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Space missing to the left of {.
Space inside { missing.
Space inside } missing.

@@ -0,0 +1,59 @@
require "json"
Copy link
Copy Markdown

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.

Comment thread lib/rack/profiler.rb Outdated

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"))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Line is too long. [142/80]

Comment thread lib/rack/profiler.rb Outdated
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Line is too long. [95/80]

Comment thread lib/rack/profiler.rb
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) &&
Copy link
Copy Markdown

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.

@brlo
Copy link
Copy Markdown
Author

brlo commented Oct 2, 2016

The bug appears only if you are using library grape-entity version less than 0.5.0. In this case, Body can be anything and #to_hash not work in most cases. You can check it in https://github.com/ruby-grape/grape-on-rack

@brlo brlo changed the title Fix: undefind method "to_hash" for grape-entity fix: no-method-error with Grape::Entity < 0.5.0 Oct 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants