I am using Ruby on Rails 3 and I would like to route some URLs to some Rack middlewares. That is, if a user try to browse at http://<my_site_name>.com/api/user/1
the system should consider to run before a Rack file and then proceed in the request.
I have a Rack::Api:User located in the lib/rack/api/user
folder.
From the RoR official documentation I discovered this:
Mount a Rack-based application to be used within the application. mount SomeRackApp, :at => "some_route" Alternatively: mount(SomeRackApp => "some_route") All mounted applications come with routing helpers to access them. These are named after the class specified, so for the above example the helper is either +some_rack_app_path+ or +some_rack_app_url+. To customize this helper's name, use the +:as+ option: mount(SomeRackApp => "some_route", :as => "exciting") This will generate the +exciting_path+ and +exciting_url+ helpers which can be used to navigate to this mounted app.
In the routers.rb file I tryed
mount "Rack::Api::User", :at => "/api/user/1"
# => ArgumentError missing :action
scope "/api/user/1" do
mount "Rack::Api::User"
end
# => NoMethodError undefined method `find' for "Rack::Api::User
I also tryed
match '/api/user/1' => Rack::Api::User
# => Routing Error No route matches "/api/user/1"
match '/api/user/1', :to => Rack::Api::User
# ArgumentError missing :controller
but no one works.
UPDATE
My Rack file is something like this:
module Api
class User
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] =~ /^\/api\/user\/i
...
else
@app.call(env)
end
end
end
end