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

How grape recognizes a path? #1072

Closed
vasilakisfil opened this issue Jul 20, 2015 · 16 comments
Closed

How grape recognizes a path? #1072

vasilakisfil opened this issue Jul 20, 2015 · 16 comments

Comments

@vasilakisfil
Copy link

In rails there is Rails.application.routes.recognize_path: http://www.rubydoc.info/docs/rails/ActionDispatch/Routing/RouteSet#recognize_path-instance_method

Is there anything similar for Grape?

@dblock
Copy link
Member

dblock commented Jul 20, 2015

Grape uses Rack::Mount, which is now yanked from Github. I actually would like to use this opportunity to figure out what the recommended replacement is and maybe find out a bit about the history of why it was yanked?

@dblock
Copy link
Member

dblock commented Jul 20, 2015

/cc: @josh

@vasilakisfil
Copy link
Author

anyone?

@dblock
Copy link
Member

dblock commented Jul 30, 2015

@vasilakisfil The "how" part is rack-mount, and you can look at the source via bundle show rack-mount in any project that has a Grape dependency. But the question of what we should do with it remains, I now asked rack-devel.

@vasilakisfil
Copy link
Author

I hate it when such vital gems don't have documentation......... and why on earth they removed it? it's free to have it somewhere on github...

After 2 hours looking on the rack-mount code, I couldn't figure out what object accepts recognize method of RouteSet class...

Any help is really appreciated.. I have a route string ('/api/v1/foobars') and some request options (like {method: :get} etc) and I need somehow to get the path from grape api.....

@dm1try
Copy link
Member

dm1try commented Aug 11, 2015

After 2 hours looking on the rack-mount code, I couldn't figure out what object accepts recognize method of RouteSet class...

seems it's just an instance of Rack::Request(or Grape)

@request_class = options.delete(:request_class) || Rack::Request
...
req = @request_class.new(env)
recognize(req) do |route, matches, params|
...
end

so you can prepare env using Rack::MockRequest
then build an instance of Rack::Request
then call recognize

mocked_env = Rack::MockRequest.env_for('/api/v1/foobars', {:method => 'GET', :params => { foo: 'bar' }})
request = Rack::Request.new(mocked_env)
routes = route_set.recognize(request)

or smth similar

why it was yanked?

really interesting..

@vasilakisfil
Copy link
Author

Thanks!

@dblock
Copy link
Member

dblock commented Aug 13, 2015

Nice work @dm1try. Should Grape provide a helper? Or maybe an extension or a separate gem?

@dm1try
Copy link
Member

dm1try commented Aug 13, 2015

@dblock I honestly don't have a good use-case of using this feature in my flow(debugging? seems we need to make grape-console first :) )
Anyway, it will be great for a start if someone can implement a helper/extension.

@voxik
Copy link

voxik commented Nov 2, 2015

As far as I remember, rack-mount used to be router used by Rails up to 3.1 [1], then the Journey was developed as a replacement and later subsumed into Rails, where it still lives [2].

[1] rails/rails@5f0b37c
[2] https://github.com/rails/rails/commits/master/actionpack/lib/action_dispatch/journey

@voxik
Copy link

voxik commented Nov 2, 2015

Just FTR, there is attempt to package Grape for Fedora [1], since it is in GitLab's dependency chain and it'd be great if the rack-mount dependency could be avoided. As much as I hate to say that, it might be even reasonable to vendor the rack-mount, if the functionality fit Grape's needs.

[1] https://bugzilla.redhat.com/show_bug.cgi?id=1268771

@dblock
Copy link
Member

dblock commented Nov 2, 2015

I would take a PR that replaces rack-mount with journey, vendors it, or whatever is needed.

@dblock
Copy link
Member

dblock commented Jan 27, 2016

We (someone :) should try https://github.com/hanami/router.

@dblock
Copy link
Member

dblock commented Mar 14, 2016

We replaced the router in #1276. Maybe we can do something about this now? /cc: @namusyaka

@namusyaka
Copy link
Contributor

Yeah, I guess we can do provide the helper, I'm going to try to implement it.

@dblock
Copy link
Member

dblock commented Mar 28, 2016

Closed via #1339.

@dblock dblock closed this as completed Mar 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants