Module: Artoo::ApiRouteHelpers::ClassMethods
- Defined in:
- lib/artoo/api_route_helpers.rb
Instance Method Summary collapse
- #compile(path) ⇒ Object
- #compile!(verb, path, block, options = {}) ⇒ Object
-
#generate_method(method_name, &block) ⇒ Method
Creates method from block, ripped from Sinatra 'cause it's so sexy in there.
-
#get(path, &block) ⇒ Object
Route function for get.
-
#get_ws(path, &block) ⇒ Object
Route function for get_ws.
-
#post(path, &block) ⇒ Object
Route function for post.
-
#put(path, &block) ⇒ Object
Route function for put.
-
#route(verb, path, &block) ⇒ Array
Adds compiled signature to routes hash.
-
#routes ⇒ Hash
Routes.
- #safe_ignore(ignore) ⇒ Object
-
#static_path(default = File.join(File.dirname(__FILE__), "..", "..", "api/public")) ⇒ String
Path to api/public directory.
Instance Method Details
#compile(path) ⇒ Object
TODO:
Add documentation
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/artoo/api_route_helpers.rb', line 49 def compile(path) keys = [] if path.respond_to? :to_str ignore = "" pattern = path.to_str.gsub(/[^\?\%\\\/\:\*\w]/) do |c| ignore << escaped(c).join if c.match(/[\.@]/) patt = encoded(c) patt.gsub(/%[\da-fA-F]{2}/) do |match| match.split(//).map {|char| char =~ /[A-Z]/ ? "[#{char}#{char.tr('A-Z', 'a-z')}]" : char}.join end end pattern.gsub!(/((:\w+)|\*)/) do |match| if match == "*" keys << 'splat' "(.*?)" else keys << $2[1..-1] ignore_pattern = safe_ignore(ignore) ignore_pattern end end [/\A#{pattern}\z/, keys] elsif path.respond_to?(:keys) && path.respond_to?(:match) [path, path.keys] elsif path.respond_to?(:names) && path.respond_to?(:match) [path, path.names] elsif path.respond_to? :match [path, keys] else raise TypeError, path end end |
#compile!(verb, path, block, options = {}) ⇒ Object
TODO:
Add documentation
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/artoo/api_route_helpers.rb', line 36 def compile!(verb, path, block, = {}) .each_pair { |option, args| send(option, *args) } method_name = "#{verb} #{path}" unbound_method = generate_method(method_name, &block) pattern, keys = compile path conditions, @conditions = @conditions, [] [ pattern, keys, conditions, block.arity != 0 ? proc { |a,p| unbound_method.bind(a).call(*p) } : proc { |a,p| unbound_method.bind(a).call } ] end |
#generate_method(method_name, &block) ⇒ Method
Creates method from block, ripped from Sinatra 'cause it's so sexy in there
28 29 30 31 32 33 |
# File 'lib/artoo/api_route_helpers.rb', line 28 def generate_method(method_name, &block) define_method(method_name, &block) method = instance_method method_name remove_method method_name method end |
#get(path, &block) ⇒ Object
Route function for get
108 109 110 |
# File 'lib/artoo/api_route_helpers.rb', line 108 def get(path, &block) route 'GET', path, &block end |
#get_ws(path, &block) ⇒ Object
Route function for get_ws
113 114 115 |
# File 'lib/artoo/api_route_helpers.rb', line 113 def get_ws(path, &block) route 'GET', path, &block end |
#post(path, &block) ⇒ Object
Route function for post
118 119 120 |
# File 'lib/artoo/api_route_helpers.rb', line 118 def post(path, &block) route 'POST', path, &block end |
#put(path, &block) ⇒ Object
Route function for put
123 124 125 |
# File 'lib/artoo/api_route_helpers.rb', line 123 def put(path, &block) route 'PUT', path, &block end |
#route(verb, path, &block) ⇒ Array
Adds compiled signature to routes hash
20 21 22 23 |
# File 'lib/artoo/api_route_helpers.rb', line 20 def route(verb, path, &block) signature = compile!(verb, path, block, {}) (routes[verb] ||= []) << signature end |
#routes ⇒ Hash
Returns routes.
14 15 16 |
# File 'lib/artoo/api_route_helpers.rb', line 14 def routes @routes ||= {} end |
#safe_ignore(ignore) ⇒ Object
TODO:
Add documentation
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/artoo/api_route_helpers.rb', line 84 def safe_ignore(ignore) unsafe_ignore = [] ignore = ignore.gsub(/%[\da-fA-F]{2}/) do |hex| unsafe_ignore << hex[1..2] '' end unsafe_patterns = unsafe_ignore.map do |unsafe| chars = unsafe.split(//).map do |char| if char =~ /[A-Z]/ char <<= char.tr('A-Z', 'a-z') end char end "|(?:%[^#{chars[0]}].|%[#{chars[0]}][^#{chars[1]}])" end if unsafe_patterns.length > 0 "((?:[^#{ignore}/?#%]#{unsafe_patterns.join()})+)" else "([^#{ignore}/?#]+)" end end |
#static_path(default = File.join(File.dirname(__FILE__), "..", "..", "api/public")) ⇒ String
Path to api/public directory
9 10 11 |
# File 'lib/artoo/api_route_helpers.rb', line 9 def static_path(default=File.join(File.dirname(__FILE__), "..", "..", "api/public")) @static_path ||= default end |