Class: Cuba
- Inherits:
-
Object
- Object
- Cuba
- Defined in:
- lib/cuba/version.rb,
lib/cuba.rb
Defined Under Namespace
Classes: RedefinitionError
Constant Summary collapse
- VERSION =
"2.1.0"
- @@methods =
[]
Instance Attribute Summary collapse
-
#captures ⇒ Object
readonly
Returns the value of attribute captures.
-
#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.
Class Method Summary collapse
- .app ⇒ Object
- .build ⇒ Object
- .call(env) ⇒ Object
- .define(&block) ⇒ Object
-
.method_added(meth) ⇒ Object
In order to prevent people from overriding the standard Cuba methods like ‘get`, `put`, etc, we add this as a safety measure.
- .prototype ⇒ Object
- .reset! ⇒ Object
- .use(middleware, *args, &block) ⇒ Object
Instance Method Summary collapse
- #_call(env) ⇒ Object
-
#accept(mimetype) ⇒ Object
If you want to match against the HTTP_ACCEPT value.
- #call(env) ⇒ Object
-
#default ⇒ Object
Syntactic sugar for providing catch-all matches.
- #delete ⇒ Object
-
#extension(ext = "\\w+") ⇒ Object
A matcher for files with a certain extension.
-
#get ⇒ Object
Syntatic sugar for providing HTTP Verb matching.
- #header(key) ⇒ Object
-
#host(hostname) ⇒ Object
Useful for matching against the request host (i.e. HTTP_HOST).
-
#initialize(&blk) ⇒ Cuba
constructor
A new instance of Cuba.
- #match(matcher, segment = "([^\\/]+)") ⇒ Object
-
#on(*args, &block) ⇒ Object
The heart of the path / verb / any condition matching.
-
#param(key) ⇒ Object
Used to ensure that certain request parameters are present.
- #post ⇒ Object
- #put ⇒ Object
-
#render(template, locals = {}, options = {}, &block) ⇒ Object
Render any type of template file supported by Tilt.
-
#run(app) ⇒ Object
If you want to halt the processing of an existing handler and continue it via a different handler.
Constructor Details
#initialize(&blk) ⇒ Cuba
Returns a new instance of Cuba.
60 61 62 63 |
# File 'lib/cuba.rb', line 60 def initialize(&blk) @blk = blk @captures = [] end |
Instance Attribute Details
#captures ⇒ Object (readonly)
Returns the value of attribute captures.
58 59 60 |
# File 'lib/cuba.rb', line 58 def captures @captures end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
55 56 57 |
# File 'lib/cuba.rb', line 55 def env @env end |
#req ⇒ Object (readonly)
Returns the value of attribute req.
56 57 58 |
# File 'lib/cuba.rb', line 56 def req @req end |
#res ⇒ Object (readonly)
Returns the value of attribute res.
57 58 59 |
# File 'lib/cuba.rb', line 57 def res @res end |
Class Method Details
.app ⇒ Object
31 32 33 |
# File 'lib/cuba.rb', line 31 def self.app @app ||= Rack::Builder.new end |
.build ⇒ Object
43 44 45 |
# File 'lib/cuba.rb', line 43 def self.build Class.new(self) end |
.call(env) ⇒ Object
51 52 53 |
# File 'lib/cuba.rb', line 51 def self.call(env) prototype.call(env) end |
.define(&block) ⇒ Object
39 40 41 |
# File 'lib/cuba.rb', line 39 def self.define(&block) app.run Cuba.new(&block) end |
.method_added(meth) ⇒ Object
In order to prevent people from overriding the standard Cuba methods like ‘get`, `put`, etc, we add this as a safety measure.
300 301 302 |
# File 'lib/cuba.rb', line 300 def self.method_added(meth) @@methods << meth end |
.prototype ⇒ Object
47 48 49 |
# File 'lib/cuba.rb', line 47 def self.prototype @prototype ||= app.to_app end |
.reset! ⇒ Object
26 27 28 29 |
# File 'lib/cuba.rb', line 26 def self.reset! @app = nil @prototype = nil end |
.use(middleware, *args, &block) ⇒ Object
35 36 37 |
# File 'lib/cuba.rb', line 35 def self.use(middleware, *args, &block) app.use(middleware, *args, &block) end |
Instance Method Details
#_call(env) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cuba.rb', line 69 def _call(env) @env = env @req = Rack::Request.new(env) @res = Rack::Response.new @matched = false catch(:ron_run_next_app) do instance_eval(&@blk) @res.status = 404 unless @matched || !@res.empty? return @res.finish end.call(env) end |
#accept(mimetype) ⇒ Object
If you want to match against the HTTP_ACCEPT value.
251 252 253 254 255 256 |
# File 'lib/cuba.rb', line 251 def accept(mimetype) lambda do String(env["HTTP_ACCEPT"]).split(",").any? { |s| s.strip == mimetype } and res["Content-Type"] = mimetype end end |
#call(env) ⇒ Object
65 66 67 |
# File 'lib/cuba.rb', line 65 def call(env) dup._call(env) end |
#default ⇒ Object
Syntactic sugar for providing catch-all matches.
264 265 266 |
# File 'lib/cuba.rb', line 264 def default true end |
#delete ⇒ Object
279 |
# File 'lib/cuba.rb', line 279 def delete ; req.delete? end |
#extension(ext = "\\w+") ⇒ Object
A matcher for files with a certain extension.
213 214 215 |
# File 'lib/cuba.rb', line 213 def extension(ext = "\\w+") lambda { consume("([^\\/]+?)\.#{ext}\\z") } end |
#get ⇒ Object
Syntatic sugar for providing HTTP Verb matching.
276 |
# File 'lib/cuba.rb', line 276 def get ; req.get? end |
#header(key) ⇒ Object
229 230 231 |
# File 'lib/cuba.rb', line 229 def header(key) lambda { env[key.upcase.tr("-","_")] } end |
#host(hostname) ⇒ Object
Useful for matching against the request host (i.e. HTTP_HOST).
239 240 241 |
# File 'lib/cuba.rb', line 239 def host(hostname) hostname === req.host end |
#match(matcher, segment = "([^\\/]+)") ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/cuba.rb', line 195 def match(matcher, segment = "([^\\/]+)") case matcher when String then consume(matcher.gsub(/:\w+/, segment)) when Regexp then consume(matcher) when Symbol then consume(segment) when Proc then matcher.call else matcher end end |
#on(*args, &block) ⇒ Object
The heart of the path / verb / any condition matching.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/cuba.rb', line 133 def on(*args, &block) # No use running any other matchers if we've already found a # proper matcher. return if @matched try do # For every block, we make sure to reset captures so that # nesting matchers won't mess with each other's captures. @captures = [] # We stop evaluation of this entire matcher unless # each and every `arg` defined for this matcher evaluates # to a non-false value. # # Short circuit examples: # on true, false do # # # PATH_INFO=/user # on true, "signup" return unless args.all? { |arg| match(arg) } begin # The captures we yield here were generated and assembled # by evaluating each of the `arg`s above. Most of these # are carried out by #consume. yield *captures ensure # Regardless of what happens in the `yield`, we should ensure that # we successfully set `@matched` to true. # At this point, we've successfully matched with some corresponding # matcher, so we can skip all other matchers defined. @matched = true end end end |
#param(key) ⇒ Object
Used to ensure that certain request parameters are present. Acts like a precondition / assertion for your route.
225 226 227 |
# File 'lib/cuba.rb', line 225 def param(key) lambda { captures << req[key] unless req[key].to_s.empty? } end |
#post ⇒ Object
277 |
# File 'lib/cuba.rb', line 277 def post ; req.post? end |
#put ⇒ Object
278 |
# File 'lib/cuba.rb', line 278 def put ; req.put? end |
#render(template, locals = {}, options = {}, &block) ⇒ Object
Render any type of template file supported by Tilt.
107 108 109 110 111 |
# File 'lib/cuba.rb', line 107 def render(template, locals = {}, = {}, &block) _cache.fetch(template, locals) { Tilt.new(template, 1, ) }.render(self, locals, &block) end |
#run(app) ⇒ Object
If you want to halt the processing of an existing handler and continue it via a different handler.
294 295 296 |
# File 'lib/cuba.rb', line 294 def run(app) throw :ron_run_next_app, app end |