Class: Rum
- Inherits:
-
Object
- Object
- Rum
- Defined in:
- lib/cuba/rum.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#req ⇒ Object
readonly
Returns the value of attribute req.
-
#res ⇒ Object
readonly
Returns the value of attribute res.
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #accept(mimetype) ⇒ Object
- #also ⇒ Object
- #any(*args) ⇒ Object
- #call(env) ⇒ Object
- #check(&block) ⇒ Object
- #default ⇒ Object
- #delete ⇒ Object
- #extension(e = "\\w+") ⇒ Object
- #get ⇒ Object
- #header(p, default = nil) ⇒ Object
- #host(h) ⇒ Object
-
#initialize(&blk) ⇒ Rum
constructor
A new instance of Rum.
- #method(m) ⇒ Object
- #number ⇒ Object
- #on(*arg, &block) ⇒ Object
- #param(p, default = nil) ⇒ Object
- #path(p) ⇒ Object
- #post ⇒ Object
- #print(*args) ⇒ Object
- #put ⇒ Object
- #puts(*args) ⇒ Object
- #run(app) ⇒ Object
- #segment ⇒ Object
Constructor Details
#initialize(&blk) ⇒ Rum
Returns a new instance of Rum.
45 46 47 |
# File 'lib/cuba/rum.rb', line 45 def initialize(&blk) @blk = blk end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
43 44 45 |
# File 'lib/cuba/rum.rb', line 43 def env @env end |
#req ⇒ Object (readonly)
Returns the value of attribute req.
43 44 45 |
# File 'lib/cuba/rum.rb', line 43 def req @req end |
#res ⇒ Object (readonly)
Returns the value of attribute res.
43 44 45 |
# File 'lib/cuba/rum.rb', line 43 def res @res end |
Instance Method Details
#_call(env) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cuba/rum.rb', line 53 def _call(env) @env = env @req = Rack::Request.new(env) @res = Rack::Response.new @matched = false catch(:rum_run_next_app) { instance_eval(&@blk) @res.status = 404 unless @matched || !@res.empty? return @res.finish }.call(env) end |
#accept(mimetype) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/cuba/rum.rb', line 132 def accept(mimetype) lambda { env['HTTP_ACCEPT'].split(',').any? { |s| s.strip == mimetype } and res['Content-Type'] = mimetype } end |
#also ⇒ Object
81 82 83 |
# File 'lib/cuba/rum.rb', line 81 def also @matched = false end |
#any(*args) ⇒ Object
77 78 79 |
# File 'lib/cuba/rum.rb', line 77 def any(*args) args.any? { |a| a == true || (a != false && a.call) } end |
#call(env) ⇒ Object
49 50 51 |
# File 'lib/cuba/rum.rb', line 49 def call(env) dup._call(env) end |
#check(&block) ⇒ Object
139 140 141 |
# File 'lib/cuba/rum.rb', line 139 def check(&block) block end |
#default ⇒ Object
115 116 117 |
# File 'lib/cuba/rum.rb', line 115 def default true end |
#delete ⇒ Object
130 |
# File 'lib/cuba/rum.rb', line 130 def delete; req.delete?; end |
#extension(e = "\\w+") ⇒ Object
103 104 105 |
# File 'lib/cuba/rum.rb', line 103 def extension(e="\\w+") lambda { env["PATH_INFO"] =~ /\.(#{e})\z/ && $1 } end |
#get ⇒ Object
127 |
# File 'lib/cuba/rum.rb', line 127 def get; req.get?; end |
#header(p, default = nil) ⇒ Object
111 112 113 |
# File 'lib/cuba/rum.rb', line 111 def header(p, default=nil) lambda { env[p.upcase.tr('-','_')] || default } end |
#host(h) ⇒ Object
119 120 121 |
# File 'lib/cuba/rum.rb', line 119 def host(h) req.host == h end |
#method(m) ⇒ Object
123 124 125 |
# File 'lib/cuba/rum.rb', line 123 def method(m) req.request_method = m end |
#number ⇒ Object
95 96 97 |
# File 'lib/cuba/rum.rb', line 95 def number path("\\d+") end |
#on(*arg, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cuba/rum.rb', line 65 def on(*arg, &block) return if @matched s, p = env["SCRIPT_NAME"], env["PATH_INFO"] yield *arg.map { |a| a == true || (a != false && a.call) || return } env["SCRIPT_NAME"], env["PATH_INFO"] = s, p @matched = true ensure unless @matched env["SCRIPT_NAME"], env["PATH_INFO"] = s, p end end |
#param(p, default = nil) ⇒ Object
107 108 109 |
# File 'lib/cuba/rum.rb', line 107 def param(p, default=nil) lambda { req[p] || default } end |
#path(p) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/cuba/rum.rb', line 85 def path(p) lambda { if env["PATH_INFO"] =~ /\A\/(#{p})(\/|\z)/ #/ env["SCRIPT_NAME"] += "/#{$1}" env["PATH_INFO"] = $2 + $' $1 end } end |
#post ⇒ Object
128 |
# File 'lib/cuba/rum.rb', line 128 def post; req.post?; end |
#print(*args) ⇒ Object
154 155 156 |
# File 'lib/cuba/rum.rb', line 154 def print(*args) args.each { |s| res.write s } end |
#put ⇒ Object
129 |
# File 'lib/cuba/rum.rb', line 129 def put; req.put?; end |
#puts(*args) ⇒ Object
147 148 149 150 151 152 |
# File 'lib/cuba/rum.rb', line 147 def puts(*args) args.each { |s| res.write s res.write "\n" } end |
#run(app) ⇒ Object
143 144 145 |
# File 'lib/cuba/rum.rb', line 143 def run(app) throw :rum_run_next_app, app end |
#segment ⇒ Object
99 100 101 |
# File 'lib/cuba/rum.rb', line 99 def segment path("[^\\/]+") end |