Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

URL helper functions for Grape APIs

License

NotificationsYou must be signed in to change notification settings

GitLab/grape-path-helpers

 
 

Repository files navigation

Build Status

Provides named route helpers for Grape APIs, similar toRails' route helpers.

Installation

Compatibility with Grape

If you're using grape 0.16.0 or higher, you'll need version 2.0.0 or higher of grape-route-helpers.

Rails

1.) Add the gem to your Gemfile.

$ bundle install grape-route-helpers

Sinatra/Rack

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

Usage

List All Helper Names

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

Use Helpers in IRB/Pry

You can use helper methods in your REPL session by including a module:

[1]pry(main)>includeGrapeRouteHelpers::NamedRouteMatcher

Use Helpers in Your API

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'

Custom Helper Names

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.

Testing

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

Contributing

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

License

See LICENSE

About

URL helper functions for Grape APIs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby100.0%

[8]ページ先頭

©2009-2025 Movatter.jp