Class: Nancy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nancy/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/nancy/base.rb', line 5

def env
  @env
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/nancy/base.rb', line 5

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/nancy/base.rb', line 5

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/nancy/base.rb', line 5

def response
  @response
end

Class Method Details

.call(env) ⇒ Object



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

def self.call(env)
  @builder.dup.call(env)
end

.compile(pattern) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/nancy/base.rb', line 15

def self.compile(pattern)
  keys = []
  pattern.gsub!(/(:\w+)/) do |match|
    keys << $1[1..-1]
    "([^/?#]+)"
  end
  [%r{^#{pattern}$}, keys]
end

.inherited(child) ⇒ Object



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

def self.inherited(child)
  child.instance_eval do
    @builder = Rack::Builder.new
    @builder.run(child.new)
  end
end

.map(*args, &block) ⇒ Object



40
41
42
# File 'lib/nancy/base.rb', line 40

def self.map(*args, &block)
  @builder.map(*args, &block)
end

.route_setObject



24
25
26
# File 'lib/nancy/base.rb', line 24

def self.route_set
  @route_set ||= Hash.new { |hash, key| hash[key] = [] }
end

.use(*args, &block) ⇒ Object



36
37
38
# File 'lib/nancy/base.rb', line 36

def self.use(*args, &block)
  @builder.use(*args, &block)
end

Instance Method Details

#call(env) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/nancy/base.rb', line 55

def call(env)
  @request = Rack::Request.new(env)
  @response = Rack::Response.new
  @params = request.params
  @env = env
  response = catch(:halt) do
    route_eval(request.request_method, request.path_info)
  end.finish
end

#halt(*res) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/nancy/base.rb', line 80

def halt(*res)
  throw :halt, res.first if res.first.is_a?(Rack::Response)
  response.status = res.detect{|x| x.is_a?(Fixnum) } || 200
  response.header.merge!(res.detect{|x| x.is_a?(Hash) } || {})
  response.body = [res.detect{|x| x.is_a?(String) } || ""]
  throw :halt, response
end

#redirect(uri) ⇒ Object



32
33
34
# File 'lib/nancy/base.rb', line 32

def redirect(uri)
  halt 302, {"Location" => uri}
end

#route_eval(request_method, path_info) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/nancy/base.rb', line 65

def route_eval(request_method, path_info)
  path_info = "/" if path_info == ""
  self.class.route_set[request_method].each do |matcher, block|
    if match = path_info.match(matcher[0])
      if (captures = match.captures) && !captures.empty?
        url_params = Hash[*matcher[1].zip(captures).flatten]
        @params = url_params.merge(params)
      end
      response.write instance_eval(&block)
      halt response
    end
  end
  halt 404
end

#sessionObject



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

def session
  request.env["rack.session"] || raise("Rack::Session handler is missing")
end