lib/artoo/api_route_helpers.rb in artoo-0.4.0 vs lib/artoo/api_route_helpers.rb in artoo-0.4.1

- old
+ new

@@ -1,32 +1,40 @@ -# route helpers used within the Artoo::Api class module Artoo + # Route helpers used within the Artoo::Api class module ApiRouteHelpers class ResponseHandled < StandardError; end module ClassMethods + # Path to api/public directory + # @return [String] static path def static_path(default=File.join(File.dirname(__FILE__), "..", "..", "api/public")) @static_path ||= default end + # @return [Hash] routes def routes @routes ||= {} end + # Adds compiled signature to routes hash + # @return [Array] signature def route(verb, path, &block) signature = compile!(verb, path, block, {}) (routes[verb] ||= []) << signature end - ## Ripped from Sinatra 'cause it's so sexy in there + # Creates method from block, ripped from Sinatra + # 'cause it's so sexy in there + # @return [Method] generated method def generate_method(method_name, &block) define_method(method_name, &block) method = instance_method method_name remove_method method_name method end + #@todo Add documentation def compile!(verb, path, block, options = {}) options.each_pair { |option, args| send(option, *args) } method_name = "#{verb} #{path}" unbound_method = generate_method(method_name, &block) pattern, keys = compile path @@ -35,10 +43,11 @@ [ pattern, keys, conditions, block.arity != 0 ? proc { |a,p| unbound_method.bind(a).call(*p) } : proc { |a,p| unbound_method.bind(a).call } ] end + #@todo Add documentation def compile(path) keys = [] if path.respond_to? :to_str ignore = "" pattern = path.to_str.gsub(/[^\?\%\\\/\:\*\w]/) do |c| @@ -69,10 +78,11 @@ else raise TypeError, path end end + #@todo Add documentation def safe_ignore(ignore) unsafe_ignore = [] ignore = ignore.gsub(/%[\da-fA-F]{2}/) do |hex| unsafe_ignore << hex[1..2] '' @@ -92,16 +102,25 @@ else "([^#{ignore}/?#]+)" end end - # Helper route functions + # Route function for get def get(path, &block) route 'GET', path, &block end + + # Route function for get_ws def get_ws(path, &block) route 'GET', path, &block end + + # Route function for post + def post(path, &block) + route 'POST', path, &block + end + + # Route function for put def put(path, &block) route 'PUT', path, &block end end