lib/grape-dsl/mounter.rb in grape-dsl-1.2.0 vs lib/grape-dsl/mounter.rb in grape-dsl-1.2.1
- old
+ new
@@ -9,15 +9,18 @@
class API
class << self
- # Args description
+ # Args will be seperated by they class type
# string = path part, will be joined with "/"
# hash = options element
# Class = target class
# Symbol = method name
+ # Proc = These procs will be called with the binding of GrapeEndpoint,
+ # so params and headers Hash::Mash will be allowed to use
+ # they will run BEFORE the method been called, so ideal for auth stuffs
#
# Array = This is for argument pre parsing, like when you use "hash" and the input will be a json
#
# simple use case: [:hello,:json],[:sup,:yaml]
#
@@ -25,29 +28,30 @@
# ---------------
#
# looks like this:
# mount_method TestClass, :test_method, "funny_path",:GET
#
- # you can give hash options just like to any othere get,post put delete etc methods, it will work
+ # you can give hash options just like to any other get,post put delete etc methods, it will work
#
def mount_method *args
- options = Hash[*args.extract_class!(Hash)]
- path_name = args.extract_class!(String).join('/')
- class_name = args.extract_class!(Class)[0]
+ options = Hash[*args.extract_class!(Hash)]
+ path_name = args.extract_class!(String).join('/')
+ class_name = args.extract_class!(Class)[0]
+ before_procs = args.extract_class!(Proc)
- tmp_array = args.extract_class!(Array)
- adapter_opt = Hash.new
+ tmp_array = args.extract_class!(Array)
+ adapter_opt = Hash.new
tmp_array.each do |array_obj|
if array_obj.count == 2
adapter_opt[array_obj[0]]= array_obj[1]
end
end
- method_name = nil
- rest_method = nil
+ method_name = nil
+ rest_method = nil
args.extract_class!(Symbol).each do |element|
if element.to_s == element.to_s.downcase
method_name = element
elsif element.to_s == element.to_s.upcase
@@ -56,11 +60,15 @@
end
rest_method ||= "get"
method_obj = class_name.method(method_name).clone
+ if path_name == String.new
+ path_name= method_name.to_s
+ end
+
params do
method_obj.parameters.each do |array_obj|
case array_obj[0]
@@ -103,10 +111,15 @@
params[element[1]]
end
}-[nil])
+ before_procs.each do |proc_obj|
+ proc_obj.call_with_binding self.binding?
+ end
+
method_obj.call(*method_arguments)
+
end
end
\ No newline at end of file