lib/grape/api.rb
# Add helper methods that will be accessible from any
# endpoint within this namespace (and child namespaces).
#
# When called without a block, all known helpers within this scope
# are included.
#
# @param [Module] new_mod optional module of methods to include
# @param [Block] block optional block of methods to include
#
# @example Define some helpers.
#
#     class ExampleAPI < Grape::API
#       helpers do
#         def current_user
#           User.find_by_id(params[:token])
#         end
#       end
#     end
#
Add helper methods that will be accessible from any
endpoint within this namespace (and child namespaces).

When called without a block, all known helpers within this scope
are included.
Add helper methods that will be accessible from any endpoint within this namespace ( and child namespaces ) . When called without a block all known helpers within this scope are included .
def helpers(new_mod = nil, &block)
        if block_given? || new_mod
          mod = settings.peek[:helpers] || Module.new
          if new_mod
            mod.class_eval do
              include new_mod
            end
          end
          mod.class_eval &block if block_given?
          set(:helpers, mod)
        else
          mod = Module.new
          settings.stack.each do |s|
            mod.send :include, s[:helpers] if s[:helpers]
          end
          change!
          mod
        end
      end
def helpers ( new_mod = nil , & block ) if block_given? || new_mod mod = settings . peek [ :helpers ] || Module . new if new_mod mod . class_eval do include new_mod end end mod . class_eval & block if block_given? set ( :helpers , mod ) else mod = Module . new settings . stack . each do | s | mod . send :include , s [ :helpers ] if s [ :helpers ] end change! mod end end
Add helper methods that will be accessible from any
endpoint within this namespace (and child namespaces).
Add helper methods that will be accessible from any endpoint within this namespace ( and child namespaces ) .
new_mod
<not_specific>
block
<not_specific>
<not_specific>
block optional block of methods to include
block optional block of methods to include
example
Define some helpers. class ExampleAPI < Grape::API helpers do def current_user User.find_by_id(params[:token]) end end end
Define some helpers . class ExampleAPI < Grape :: API helpers do def current_user User . find_by_id ( params [ : token ] ) end end end