module Artoo::Api::RouteHelpers::ClassMethods

Public Instance Methods

any(path, &block) click to toggle source

Route function for put

# File lib/artoo/api/route_helpers.rb, line 129
def any(path, &block)
  route 'GET', path, &block
  route 'POST', path, &block
end
compile(path) click to toggle source

@todo Add documentation

# 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 = {}) click to toggle source

@todo Add documentation

# 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) click to toggle source

Creates method from block, ripped from Sinatra ‘cause it’s so sexy in there @return [Method] generated method

# 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) click to toggle source

Route function for get

# File lib/artoo/api/route_helpers.rb, line 109
def get(path, &block)
  route 'GET', path, &block
end
get_ws(path, &block) click to toggle source

Route function for get_ws

# File lib/artoo/api/route_helpers.rb, line 114
def get_ws(path, &block)
  route 'GET', path, &block
end
post(path, &block) click to toggle source

Route function for post

# File lib/artoo/api/route_helpers.rb, line 119
def post(path, &block)
  route 'POST', path, &block
end
put(path, &block) click to toggle source

Route function for put

# File lib/artoo/api/route_helpers.rb, line 124
def put(path, &block)
  route 'PUT', path, &block
end
route(verb, path, &block) click to toggle source

Adds compiled signature to routes hash @return [Array] signature

# File lib/artoo/api/route_helpers.rb, line 21
def route(verb, path, &block)
  signature = compile!(verb, path, block, {})
  (routes[verb] ||= []) << signature
end
routes() click to toggle source

@return [Hash] routes

# File lib/artoo/api/route_helpers.rb, line 15
def routes
  @routes ||= {}
end
safe_ignore(ignore) click to toggle source

@todo Add documentation

# 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) click to toggle source

Path to api/public directory @return [String] static path

# File lib/artoo/api/route_helpers.rb, line 10
def static_path(default = Gem.loaded_specs['robeaux'].full_gem_path)
  @static_path ||= default
end