I am creating my Api in Sinatra but i want for example use this routes:
/places
/places.meta
/places.list
/users/id/places.list
i have this working in rails but fails in sinatra
def index
case request.format.to_sym.to_s
when 'list'
result = Place.single_list(parameters)
when 'meta'
result = @parameters.to_meta
else
result = Place.get_all(parameters)
end
render json: result, status: 200
end
Sinatra doesn't have a built-in concept of a "request format", so you have to manually specify a format-aware route pattern which Rails provide you automatically.
Here I use route pattern specified as a Regexp with a named capture:
require 'sinatra'
get /\/places(\.(?<format>meta|list))?/ do # named capture 'format'
case params['format'] # params populated with named captures from the route pattern
when 'list'
result = Place.single_list(parameters)
when 'meta'
result = @parameters.to_meta
else
result = Place.get_all(parameters)
end
result.to_json # replace with your favourite way of building a Sinatra response
end