No commit activity in last 3 years
No release in over 3 years
This implementation is really simple and not very dynamic, but may be handy.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 10.0
~> 3.0

Runtime

>= 0.11
 Project Readme

Grape::NamedRoutes

It is an extenstion for API creating framework Grape. With this gem you can create named routes to call them later without hardcoding pathes.

Installation

Add these lines to your application's Gemfile:

gem 'grape'
gem 'grape-named_routes'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grape
$ gem install grape-named_routes

Usage

These endpoints simply return their URLs.

module Twitter
  class API < Grape::API
    resource :statuses do
      desc 'Endpoint without params'
      get :home_timeline, as: :home_timeline do
        Twitter::API.home_timeline_path
      end

      desc 'Endpoint with route param'
      params do
        requires :id, type: Integer
      end
      route_param :id do
        get as: :status do
          Twitter::API.status_path(id: params[:id])
        end
      end

      desc 'Endpoint with inline param'
      params do
        requires :param, type: String
      end
      get '/custom_route/:param', as: :inline do
        Twitter::API.inline_path(param: params[:param])
      end
    end
  end
end

If you somewhy need to get endpoint object instead of compiled path, you can use following:

  Twitter::API.find_endpoint(:home_timeline) # returns Grape::Endpoint instance

  # also supports "dangerous" version (raises an error if route is not declared)
  Twitter::API.find_endpoint!(:home_timeline)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hedgesky/grape-named_routes.

License

The gem is available as open source under the terms of the MIT License.