I'm pretty new to ruby, and I'm unable to use a method with parameters in grape

57 views
Skip to first unread message

אוראל ציון

unread,
Aug 25, 2014, 9:27:17 AM8/25/14
to ruby-...@googlegroups.com

I'm pretty new to Ruby, I know a little bit of Sinatra but what I actually need for my app is Grape for rest api.

Working with a method with parameters at all works like a charm, but when I'm trying to add parameters I get 404 not found exception.

Where am I going wrong here? Thanks



 resource
:devs do

 desc
"Get all devs"
 
get do
 authenticate
!
 
Dev.all
 
end

 desc
"Get dev by email"
 
params do
 requires
:email, type: String, desc: "Dev's email"
 
end
 route_param
:email do
 
get do
 authenticate
!
 
@devs = Dev.all(:email == params[:email])
 
#!error('email not found', 204) unless @devs.length > 0
 
end
 
end

 desc
"Get dev by API key"
 
get :key do
 authenticate
!
 
@dev = Dev.first(:api_key == params[:key])

 
!error('email not found', 204) unless @devs.length > 0
 
end
 
end

This is the call I make in PostMan (I also added the header for Apikey there)

localhost:9292/devs/email/orelzion@gmail.com

But it always give me the same result 404

If I'm on the other hand, trying to access the method like this

localhost:9292/devs?email=orel...@gmail.com

Then it will go starightly into the main function, and not into the function with the parameters

Daniel Doubrovkine

unread,
Aug 25, 2014, 9:59:48 AM8/25/14
to ruby-...@googlegroups.com

Does it work if you specify 'foobar' instead of an actual email?

The argument should be URL encoded or you need to change the route's requirements (check README). The default parser is probably confused because it thinks .com is some kind of formatting extension.

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

אוראל ציון

unread,
Aug 25, 2014, 10:04:23 AM8/25/14
to ruby-...@googlegroups.com
Thank you Daniel

So I tried localhost:9292/devs?email=orelzion and it go straight to the main function
And I tried localhost:9292/devs/email=orelzion but it gives me 500 error noMethodFound
And I tried localhost:9292/devs/email/orelzion and it gives me 404 error

BTW How should I access this method?

localhost:9292/devs?email=orelzi...@gmail.com

Then it will go starightly into the main function, and not into the function with the parameters

Daniel Doubrovkine

unread,
Aug 25, 2014, 12:00:20 PM8/25/14
to ruby-...@googlegroups.com
You should post the project somewhere, a lot of things could go wrong. For one, is this mounted on rails? Can you hit *any* method in the API?

אוראל ציון

unread,
Aug 25, 2014, 12:13:35 PM8/25/14
to ruby-...@googlegroups.com
Thank you Daniel
I'll post it to github tomorrow hopefully.
At the mean time, no. It's on rack only.
And yes the simple get with no parameters does work

Elad Ossadon

unread,
Aug 26, 2014, 4:59:35 PM8/26/14
to ruby-...@googlegroups.com
It's related to the dot in the email. 

Write outside of a route_param:

get ':email', :requirements => { :email => /.*/ } do
... params[:email]
end

There's a lot of info about it, just google 'grape dot param'


localhost:9292/devs?email=orelzi...@gmail.com

אוראל ציון

unread,
Aug 27, 2014, 1:45:26 AM8/27/14
to ruby-...@googlegroups.com
Thank you Elad
First of all I've learned that the right call should be localhost:9292/devs/orel...@gmail.com from an answer on StackOverFlow
And thanks to Elad I also learned that I needed to escape that dot from the param
So now my code looks like this

            desc "Get dev by email"
           
params do
                requires
:email, type: String, desc: "Dev's email"
             
end

             
get ':email', requirements: { email: /.*/ } do
               
authenticate!
               
Dev.all(:email => params[:email])
            end
Enter code here...
Reply all
Reply to author
Forward
0 new messages