- Notifications
You must be signed in to change notification settings - Fork8
URL helper functions for Grape APIs
License
GitLab/grape-path-helpers
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Provides named route helpers for Grape APIs, similar toRails' route helpers.
If you're using grape 0.16.0 or higher, you'll need version 2.0.0 or higher of grape-route-helpers.
1.) Add the gem to your Gemfile.
$ bundle install grape-route-helpers
1.) Add the gem to your Gemfile if you're using Bundler.
If you're not using Bundler to install/manage dependencies:
$ gem install grape-route-helpers
# environment setup filerequire'grape'require'grape/route_helpers'
2.) Write a rake task called:environment
that loads the application's environment first. This gem's tasks are dependent on it. You could put this in the root of your project directory:
# Rakefilerequire'rake'require'bundler'Bundler.setuprequire'grape-route-helpers'require'grape-route-helpers/tasks'desc'load the Sinatra environment.'task:environmentdorequireFile.expand_path('your_app_file',File.dirname(__FILE__))end
To see which methods correspond to which paths, and which options you can pass them:
# In your API root directory, at the command line$ rake grape:route_helpers
You can use helper methods in your REPL session by including a module:
[1]pry(main)>includeGrapeRouteHelpers::NamedRouteMatcher
Use the methods inside your Grape API actions. Given this example API:
classExampleAPI <Grape::APIversion'v1'prefix'api'format'json'get'ping'do'pong'endresource:catsdoget'/'do%w(catscatscats)endroute_param:iddogetdo'cat'endendendroute:any,'*anything'doredirectapi_v1_cats_pathendend
You'd have the following methods available inside your Grape API actions:
# specifying the version when using Grape's "path" versioning strategyapi_v1_ping_path# => '/api/v1/ping.json'# specifying the formatapi_v1_cats_path(format:'.xml')# => '/api/v1/cats.xml'# adding a query stringapi_v1_cats_path(params:{sort_by::age})# => '/api/v1/cats?sort_by=age'# passing in values required to build a pathapi_v1_cats_path(id:1)# => '/api/v1/cats/1.json'# catch-all paths have helpersapi_v1_anything_path# => '/api/v1/*anything'
If you want to assign a custom helper name to a route, pass the:as
option when creating your route in your API:
classBase <Grape::APIget'ping',as:'is_the_server_running''pong'endend
This results in creating a helper calledis_the_server_running_path
.
You can use route helpers in your API tests by including theGrapeRouteHelpers::NamedRouteMatcher
module inside your specs. Here's an example:
require'spec_helper'describeApi::BasedoincludeGrapeRouteHelpers::NamedRouteMatcherdescribe'GET /ping'doit'returns a 200 OK'dogetapi_v2_ping_pathexpect(response.status).tobe(200)endendend
1.) Fork it
2.) Create your feature branch(git checkout -b my-new-feature)
3.) Write specs for your feature
4.) Commit your changes(git commit -am 'Add some feature')
5.) Push to the branch(git push origin my-new-feature)
6.) Create a new pull request
See LICENSE
About
URL helper functions for Grape APIs
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- Ruby100.0%