how to get sinatra route_path like in grape - ruby

I want to report stats for my routes.
To do that, I have to know what they are
I am using ruby's sinatra
so I have
get '/test/:username' do
....
end
after do
request.path_info
end
But I get '/test/test_username123' as an output
Is there a variable (like grape route.route_path) that will enable me to get the route?
Edit: Grape's route.route_path will output "/api/:version/test/:username(.:format)"

Related

How do I add custom logging for which route is satisfying a request in Sinatra?

I'm working on a Sinatra app that has a bunch of routes of all sorts. I'd like to add some custom logging that logs the params of the get or post call that ends up generating the response for the request. I realize I could subclass the get/post definition to wrap the block with a logging call. But I suspect there is a more appropriate approach.
You can use Sinatra's before hook in your controller, and print out some information contained on the request
before do
if request.request_method == :get || request.request_method == :post
puts request.path_info, params.inspect # check out the request variable for more info you might like to ouput
end
end

Cant set custom header params with rspec and Grape

I use Grape api and i need to write a test with custom header
my code:
it "should accept message" do
post "/api/v1/my/route", post_data, secret: "ASDFGHJKL"
last_response.status.should == 201
end
but the route gets no headers at all,
i also tried headers['secret'] = "ASDFGHJKL"
and also request.env['secret']
nothing works.
how can i pass headers in rspec to grape route?
Did you try header 'secret', 'ASDFGHJKL'?
More on the rack-test docs
Try using #request variable, e.g. #request.env['secret'] = 'ASDF..'. Hope this helps.
Also, take a look at this - Sending custom headers through RSpec

Difference between use and run in Rack

What's the difference between the use and run methods in rackup files? It seems run is always at the end of config.ru, but it seems as if you should just be able to use use. Enlightening resources would also be very much appreciated.
use is for middlewares
class MyCustomMiddleware
def initialize(app)
#app = app
end
def call(env)
if condition
env['set-header'] = 'Middleware Can modify the response & pass it into next middleware'
end
#app.call(env)
end
run takes an argument that responds to call and returns a final Rack response with a HTTP Response code like 200.
class MyApp
def self.call(env)
[200, { "Content-Type" => "text/html" }, ["OK"]]
end
end
To understand the difference between use & run. lets see structure of typically rack app.
Typical Rack App
Rack app includes multiple middleware(s) which respond to call but do not return final rack response & an Object that responds to call which returns final rack response which includes HTTP response code(200,404, 500 etc). so typically there will be multiple objects which act as middlewares & then a object which returns final rack response with response code.
Difference between use & run
Now with this,
it seems can we can invoke use multiple times, once for each middleware & run only once in a single Rack App. so use will only invoke a middleware, while run will run the rack object which will return final rack response with HTTP status code.
example config.ru
use MyCustomMiddleware
use MyCustomMiddleware2
use MyCustomMiddleware3
run MyApp
In case if anything above is wrong, Let me know. So I can correct it.

sinatra routes first path param is a number

I want to be able to match a route that looks something like
/2/monkey/session
I have the following in sinatra but
/:version_number/:name/session
And I keep getting the Sinatra doesn’t know this ditty. Anyone knows a way to getting this to work so that I can have params[:version_number] and params[:name] matched.
I wrote the code below (Ruby 2.0.0 / Sinatra 1.4.3).
require "sinatra"
get "/:version_number/:name/session" do
params.inspect
end
The response seems like correctly.
{"splat"=>[], "captures"=>["2", "monkey"], "version_number"=>"2", "name"=>"monkey"}
Why don't you check HTTP method or comment out other code?

Local variables in a block that are not passed as parameters [duplicate]

This question already has answers here:
Rails params explained?
(5 answers)
Closed 9 years ago.
I'm trying to understand how to make a variable available to a block that is not passed to the block as a parameter.
For example, how does Sinatra make params hash available?
get '/hello/:name' do
howAmIAccessingThis = params[:name]
end
Where is params coming from? This:
get '/hello/:name' do |params|
#hisName = params[:name]
end
might make sense because params is declared as a block argument, but that's not how it works. Looking through the source I cannot find how the params hash is getting passed to the block without it being a block parameter.
If it is not a local variable or a block variable, then it is a method. I don't know about Sinatra, but there must be a method params defined somewhere.
Using Parameters
Parameters in Sinatra are like everything else--simple and straightforward.
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
get '/hello/:name' do
"Hello #{params[:name]}!"
end
Once you've made this change, you'll need to restart the Sinatra application. Kill it with Ctrl-C and run it again. (There's a way around this, but we'll look at that in a future article.) Now, the parameters are straightforward. We've made an action called /hello/:name. This syntax is imitating what the URLs will look like, so go to http://localhost:4567/hello/Your Name to see it in action.
The /hello portion matches that portion of the URL from the request you made, and :name will absorb any other text you give it and put it in the params hash under the key :name. Parameters are just that easy. There is of course much more you can do with these, including regexp-based parameters, but this is all you'll need in almost every case.
Reference: http://ruby.about.com/od/sinatra/a/sinatra2.htm
EDIT
params values can come from the query string of a GET request, or the form data of a POST request, but there's also a third place they can come from: The path of the URL.
As you might know, Rails uses something called routes to direct requests to their corresponding controller actions. These routes may contain segments that are extracted from the URL and put into params. For example, if you have a route like this:
match 'products/:id', ...
Then a request to a URL like http://example.com/products/42 will set params[:id] to 42
So, whenever an URL GET, POST or Path contains such pattern then params hash is automatically constructed by rails.
Also check the Parameters section(Section 4) here

Resources