Module: Artoo::Api::RouteHelpers::ClassMethods

Defined in:
lib/artoo/api/route_helpers.rb

Instance Method Summary collapse

Instance Method Details

#any(path, &block) ⇒ Object

Route function for put



129
130
131
132
# File 'lib/artoo/api/route_helpers.rb', line 129

def any(path, &block)
  route 'GET', path, &block
  route 'POST', path, &block
end

#compile(path) ⇒ Object

TODO:

Add documentation



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
82
# File 'lib/artoo/api/route_helpers.rb', line 50

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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/artoo/api/route_helpers.rb', line 37

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
  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

Returns:

  • (Method)

    generated method



29
30
31
32
33
34
# File 'lib/artoo/api/route_helpers.rb', line 29

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



109
110
111
# File 'lib/artoo/api/route_helpers.rb', line 109

def get(path, &block)
  route 'GET', path, &block
end

#get_ws(path, &block) ⇒ Object

Route function for get_ws



114
115
116
# File 'lib/artoo/api/route_helpers.rb', line 114

def get_ws(path, &block)
  route 'GET', path, &block
end

#post(path, &block) ⇒ Object

Route function for post



119
120
121
# File 'lib/artoo/api/route_helpers.rb', line 119

def post(path, &block)
  route 'POST', path, &block
end

#put(path, &block) ⇒ Object

Route function for put



124
125
126
# File 'lib/artoo/api/route_helpers.rb', line 124

def put(path, &block)
  route 'PUT', path, &block
end

#route(verb, path, &block) ⇒ Array

Adds compiled signature to routes hash

Returns:

  • (Array)

    signature



21
22
23
24
# File 'lib/artoo/api/route_helpers.rb', line 21

def route(verb, path, &block)
  signature = compile!(verb, path, block, {})
  (routes[verb] ||= []) << signature
end

#routesHash

Returns routes.

Returns:

  • (Hash)

    routes



15
16
17
# File 'lib/artoo/api/route_helpers.rb', line 15

def routes
  @routes ||= {}
end

#safe_ignore(ignore) ⇒ Object

TODO:

Add documentation



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/artoo/api/route_helpers.rb', line 85

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 = Gem.loaded_specs['robeaux'].full_gem_path) ⇒ String

Path to api/public directory

Returns:

  • (String)

    static path



10
11
12
# File 'lib/artoo/api/route_helpers.rb', line 10

def static_path(default = Gem.loaded_specs['robeaux'].full_gem_path)
  @static_path ||= default
end