Top Level Namespace

Defined Under Namespace

Modules: BigSword Classes: Request, Response

Instance Method Summary collapse

Instance Method Details

#_call(env) ⇒ Object



44
45
46
47
48
49
# File 'lib/bigsword/base.rb', line 44

def _call(env)
  $env = env
  $request = Request.new $env
  $response = Response.new
  catch(:halt) { route_eval }
end

#afterObject



4
# File 'lib/bigsword/base.rb', line 4

def after; end

#beforeObject



3
# File 'lib/bigsword/base.rb', line 3

def before; end

#compile_route(path, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bigsword/base.rb', line 32

def compile_route(path, &block)
  route = {block: block, compiled_path: nil, extra_params: [], path: path}

  compiled_path = path.gsub(/:\w+/) do |match|
    route[:extra_params] << match.gsub(':', '').to_sym
    '([^/?#]+)'
  end

  route[:compiled_path] = /^#{compiled_path}$/
  route
end

#convert_to_lambda(&block) ⇒ Object



6
7
8
9
10
# File 'lib/bigsword/base.rb', line 6

def convert_to_lambda &block
  obj = Object.new
  obj.define_singleton_method(:_, &block)
  return obj.method(:_).to_proc
end

#envObject



97
98
99
# File 'lib/bigsword/base.rb', line 97

def env
  $env
end

#filtersObject



28
29
30
# File 'lib/bigsword/base.rb', line 28

def filters
  $filters ||= Hash.new { |hash, key| hash[key] = [] }
end

#filters_eval(type) ⇒ Object



55
56
57
# File 'lib/bigsword/base.rb', line 55

def filters_eval(type)
  filters[type].map { |filter| convert_to_lambda(&filter[:block]).call}
end

#find_routeObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bigsword/base.rb', line 74

def find_route
  route = routes[$request.request_method].detect do |r|
    r[:compiled_path] =~ $request.path_info
  end

  if route
    $~.captures.each_with_index do |value, index|
      param = route[:extra_params][index]
      $request.params[param] = value
    end
  end

  route
end

#halt(response) ⇒ Object



51
52
53
# File 'lib/bigsword/base.rb', line 51

def halt(response)
  throw :halt, response
end

#paramsObject



89
90
91
# File 'lib/bigsword/base.rb', line 89

def params
  $request.params.deep_symbolize_keys!
end

#responseObject



93
94
95
# File 'lib/bigsword/base.rb', line 93

def response
  $response
end

#route_evalObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bigsword/base.rb', line 59

def route_eval
  filters_eval(:before)
  route = find_route

  if route
    proc = convert_to_lambda &route[:block]
    $response.body = proc.call
    # $response.write instance_eval(&route[:block])
  else
    $response.status = 404
  end
  filters_eval(:after)
  $response.finish
end

#routesObject



18
19
20
# File 'lib/bigsword/base.rb', line 18

def routes
  $routes ||= Hash.new { |hash, key| hash[key] = [] }
end