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) ⇒ Object
Ripped from Sinatra ‘cause it’s so sexy in there.
-
#get(path, &block) ⇒ Object
Helper route functions.
- #get_ws(path, &block) ⇒ Object
- #put(path, &block) ⇒ Object
- #route(verb, path, &block) ⇒ Object
- #routes ⇒ Object
- #safe_ignore(ignore) ⇒ Object
- #static_path(default = File.join(File.dirname(__FILE__), "..", "..", "api/public")) ⇒ Object
Instance Method Details
#compile(path) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/artoo/api_route_helpers.rb', line 40 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
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/artoo/api_route_helpers.rb', line 28 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) ⇒ Object
Ripped from Sinatra ‘cause it’s so sexy in there
21 22 23 24 25 26 |
# File 'lib/artoo/api_route_helpers.rb', line 21 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
Helper route functions
98 99 100 |
# File 'lib/artoo/api_route_helpers.rb', line 98 def get(path, &block) route 'GET', path, &block end |
#get_ws(path, &block) ⇒ Object
101 102 103 |
# File 'lib/artoo/api_route_helpers.rb', line 101 def get_ws(path, &block) route 'GET', path, &block end |
#put(path, &block) ⇒ Object
104 105 106 |
# File 'lib/artoo/api_route_helpers.rb', line 104 def put(path, &block) route 'PUT', path, &block end |
#route(verb, path, &block) ⇒ Object
15 16 17 18 |
# File 'lib/artoo/api_route_helpers.rb', line 15 def route(verb, path, &block) signature = compile!(verb, path, block, {}) (routes[verb] ||= []) << signature end |
#routes ⇒ Object
11 12 13 |
# File 'lib/artoo/api_route_helpers.rb', line 11 def routes @routes ||= {} end |
#safe_ignore(ignore) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/artoo/api_route_helpers.rb', line 74 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")) ⇒ Object
7 8 9 |
# File 'lib/artoo/api_route_helpers.rb', line 7 def static_path(default=File.join(File.dirname(__FILE__), "..", "..", "api/public")) @static_path ||= default end |