desc "This endpoint does things"
throttle daily: 3, except: ->(user) { user.is_admin? }
params do
requires :param1, type: String, desc: "The param"
end
get "/:param1" do
{result: "You sent #{param1}"}
end
module Grape | |
module Extensions | |
module AdminExtension | |
[:admin, :user].each do |role| | |
define_method role do |value| | |
@last_description ||= {} | |
@last_description.merge!("role_#{role}".to_sym => value) | |
value | |
end | |
end | |
Grape::API.extend self | |
end | |
end | |
end |
module Api | |
module Middleware | |
class UserRoleMiddleware < Grape::Middleware::Base | |
def before | |
if route_options[:role_admin] | |
authenticated_admin | |
elsif route_options[:role_user] | |
authenticated_user | |
end | |
end | |
private | |
def route_options | |
env['api.endpoint'].options[:route_options] | |
end | |
end | |
end | |
end |
module Grape | |
module Extensions | |
module AdminExtension | |
[:admin, :user].each do |role| | |
define_method role do |value| | |
route_setting :role, "role_#{role}".to_sym => value | |
value | |
end | |
end | |
Grape::API.extend self | |
end | |
end | |
end |
module Api | |
module V2 | |
module Middleware | |
class UserRoleMiddleware < Grape::Middleware::Base | |
def before | |
if role[:role_admin] | |
authenticated_admin | |
elsif role[:role_user] | |
authenticated_user | |
end | |
end | |
private | |
def role | |
env['api.endpoint'].route_setting(:role) || {} | |
end | |
end | |
end | |
end | |
end |
--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
...
--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
module Api
class Users < Grape::API
use ::Api::Middleware::ThrottleMiddleware
# ... endpoints
desc "My method"
throttle true
get "/" do
{"success": true}
end
end
end
...
--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
...
--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
...
--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-grape/a14aeecd-baf9-43d8-b8ba-5f110b11492fn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-grape/d90e10ed-d8a6-46df-86a1-a28edc2b8104n%40googlegroups.com.