bellycard / napa

A simple framework for building APIs with Grape
Other
329 stars 72 forks source link

Unable to use Mongoid with Napa Representer #216

Closed leason closed 9 years ago

leason commented 9 years ago

I'm trying to use Mongoid as my service makes more sense to use a document store rather than a SQL database. Mongoid has a find() method which returns a single class, but the Napa Representer chokes on this. I believe that Napa is trying to do some magic and getting confused about the type of object that Mongoid is returning. Any advice? Here is the dump of the error:

NoMethodError: undefined method `map' for #<Message:0x007f7f6ab436b0>
    /var/lib/gems/2.2.0/gems/napa-0.4.3/lib/napa/grape_extensions/grape_helpers.rb:7:in `represent'
    /vagrant/messages-service/app/apis/messages_v1_api.rb:100:in `block (3 levels) in <class:MessagesV1Api>'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/endpoint.rb:47:in `call'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/endpoint.rb:47:in `block in generate_api_method'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/endpoint.rb:247:in `call'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/endpoint.rb:247:in `run'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/endpoint.rb:195:in `block in call!'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/base.rb:24:in `call'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/base.rb:24:in `call!'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/base.rb:18:in `call'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/base.rb:24:in `call!'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/base.rb:18:in `call'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/error.rb:27:in `block in call!'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/error.rb:26:in `catch'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/error.rb:26:in `call!'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/middleware/base.rb:18:in `call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/head.rb:13:in `call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:153:in `call'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/endpoint.rb:196:in `call!'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/endpoint.rb:184:in `call'
    /var/lib/gems/2.2.0/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:152:in `block in call'
    /var/lib/gems/2.2.0/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:96:in `block in recognize'
    /var/lib/gems/2.2.0/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:89:in `optimized_each'
    /var/lib/gems/2.2.0/gems/rack-mount-0.8.3/lib/rack/mount/code_generation.rb:95:in `recognize'
    /var/lib/gems/2.2.0/gems/rack-mount-0.8.3/lib/rack/mount/route_set.rb:141:in `call'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/api.rb:98:in `call'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/api.rb:33:in `call!'
    /var/lib/gems/2.2.0/gems/grape-0.11.0/lib/grape/api.rb:29:in `call'
    /var/lib/gems/2.2.0/gems/napa-0.4.3/lib/napa/middleware/app_monitor.rb:12:in `call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/lint.rb:49:in `_call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/lint.rb:37:in `call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/showexceptions.rb:24:in `call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/commonlogger.rb:33:in `call'
    /var/lib/gems/2.2.0/gems/shotgun-0.9.1/lib/shotgun/loader.rb:86:in `proceed_as_child'
    /var/lib/gems/2.2.0/gems/shotgun-0.9.1/lib/shotgun/loader.rb:31:in `call!'
    /var/lib/gems/2.2.0/gems/shotgun-0.9.1/lib/shotgun/loader.rb:18:in `call'
    /var/lib/gems/2.2.0/gems/shotgun-0.9.1/lib/shotgun/favicon.rb:12:in `call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/urlmap.rb:66:in `block in call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/urlmap.rb:50:in `each'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/urlmap.rb:50:in `call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/builder.rb:153:in `call'
    /var/lib/gems/2.2.0/gems/rack-1.6.1/lib/rack/handler/webrick.rb:89:in `service'
    /usr/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
    /usr/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
    /usr/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
shaqq commented 9 years ago

Thanks for reporting this - it looks like the Representer is assuming that if the data passed into it responds to :to_a, then it must be enumerable:

https://github.com/bellycard/napa/blob/master/lib/napa/grape_extensions/grape_helpers.rb#L6-L10

a quick workaround would be to munge that Message object into a Hash or something, but a PR to fix this would be better :smile:

leason commented 9 years ago

Cool - I figured that was the problem but wanted to be sure. I'm actually late to the game on getting involved in Github and just starting out with Ruby, but I'll work on a fix and try to submit a PR soon. Thanks for confirming that for me, Shaker!

On Tue, Jun 16, 2015 at 11:36 PM Shaker Islam notifications@github.com wrote:

Thanks for reporting this - it looks like the Representer is assuming that if the data passed into it responds to :to_a, then it must be enumerable:

https://github.com/bellycard/napa/blob/master/lib/napa/grape_extensions/grape_helpers.rb#L6-L10

a quick workaround would be to do a :to_a on that Message object you're passing in, but a PR to fix this would be better [image: :smile:]

— Reply to this email directly or view it on GitHub https://github.com/bellycard/napa/issues/216#issuecomment-112641556.