Class: Cuba
- Inherits:
-
Object
show all
- Defined in:
- lib/cuba/safe/secure_headers.rb,
lib/cuba.rb,
lib/cuba/safe.rb,
lib/cuba/render.rb,
lib/cuba/safe/csrf.rb
Overview
Defined Under Namespace
Modules: Render, Safe
Classes: Response
Constant Summary
collapse
- SLASH =
"/".freeze
- EMPTY =
"".freeze
- SEGMENT =
"([^\\/]+)".freeze
- DEFAULT =
"text/html; charset=utf-8".freeze
- REGEXES =
Hash.new { |h, pattern| h[pattern] = /\A\/(#{pattern})(\/|\z)/ }
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(&blk) ⇒ Cuba
Returns a new instance of Cuba.
133
134
135
136
|
# File 'lib/cuba.rb', line 133
def initialize(&blk)
@blk = blk
@captures = []
end
|
Instance Attribute Details
#captures ⇒ Object
Returns the value of attribute captures.
131
132
133
|
# File 'lib/cuba.rb', line 131
def captures
@captures
end
|
#env ⇒ Object
Returns the value of attribute env.
128
129
130
|
# File 'lib/cuba.rb', line 128
def env
@env
end
|
#req ⇒ Object
Returns the value of attribute req.
129
130
131
|
# File 'lib/cuba.rb', line 129
def req
@req
end
|
#res ⇒ Object
Returns the value of attribute res.
130
131
132
|
# File 'lib/cuba.rb', line 130
def res
@res
end
|
Class Method Details
.app ⇒ Object
89
90
91
|
# File 'lib/cuba.rb', line 89
def self.app
@app ||= Rack::Builder.new
end
|
.call(env) ⇒ Object
105
106
107
|
# File 'lib/cuba.rb', line 105
def self.call(env)
prototype.call(env)
end
|
.deepclone(obj) ⇒ Object
120
121
122
|
# File 'lib/cuba.rb', line 120
def self.deepclone(obj)
Marshal.load(Marshal.dump(obj))
end
|
.define(&block) ⇒ Object
97
98
99
|
# File 'lib/cuba.rb', line 97
def self.define(&block)
app.run new(&block)
end
|
.inherited(child) ⇒ Object
124
125
126
|
# File 'lib/cuba.rb', line 124
def self.inherited(child)
child.settings.replace(deepclone(settings))
end
|
.plugin(mixin) ⇒ Object
109
110
111
112
113
114
|
# File 'lib/cuba.rb', line 109
def self.plugin(mixin)
include mixin
extend mixin::ClassMethods if defined?(mixin::ClassMethods)
mixin.setup(self) if mixin.respond_to?(:setup)
end
|
.prototype ⇒ Object
101
102
103
|
# File 'lib/cuba.rb', line 101
def self.prototype
@prototype ||= app.to_app
end
|
.reset! ⇒ Object
84
85
86
87
|
# File 'lib/cuba.rb', line 84
def self.reset!
@app = nil
@prototype = nil
end
|
.settings ⇒ Object
116
117
118
|
# File 'lib/cuba.rb', line 116
def self.settings
@settings ||= {}
end
|
.use(middleware, *args, &block) ⇒ Object
93
94
95
|
# File 'lib/cuba.rb', line 93
def self.use(middleware, *args, &block)
app.use(middleware, *args, &block)
end
|
Instance Method Details
#accept(mimetype) ⇒ Object
If you want to match against the HTTP_ACCEPT value.
313
314
315
316
317
318
319
320
321
|
# File 'lib/cuba.rb', line 313
def accept(mimetype)
lambda do
accept = String(env["HTTP_ACCEPT"]).split(",")
if accept.any? { |s| s.strip == mimetype }
res[Rack::CONTENT_TYPE] = mimetype
end
end
end
|
#call(env) ⇒ Object
142
143
144
|
# File 'lib/cuba.rb', line 142
def call(env)
dup.call!(env)
end
|
#call!(env) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/cuba.rb', line 146
def call!(env)
@env = env
@req = settings[:req].new(env)
@res = settings[:res].new(settings[:default_headers].dup)
catch(:halt) do
instance_eval(&@blk)
not_found
res.finish
end
end
|
#default ⇒ Object
Syntactic sugar for providing catch-all matches.
329
330
331
|
# File 'lib/cuba.rb', line 329
def default
true
end
|
#delete ⇒ Object
357
|
# File 'lib/cuba.rb', line 357
def delete; req.delete? end
|
#extension(ext = "\\w+") ⇒ Object
A matcher for files with a certain extension.
271
272
273
|
# File 'lib/cuba.rb', line 271
def extension(ext = "\\w+")
lambda { consume("([^\\/]+?)\.#{ext}\\z") }
end
|
#get ⇒ Object
Syntatic sugar for providing HTTP Verb matching.
353
|
# File 'lib/cuba.rb', line 353
def get; req.get? end
|
#halt(response) ⇒ Object
381
382
383
|
# File 'lib/cuba.rb', line 381
def halt(response)
throw :halt, response
end
|
#head ⇒ Object
358
|
# File 'lib/cuba.rb', line 358
def head; req.head? end
|
#host(hostname) ⇒ Object
Useful for matching against the request host (i.e. HTTP_HOST).
301
302
303
|
# File 'lib/cuba.rb', line 301
def host(hostname)
hostname === req.host
end
|
#link ⇒ Object
360
|
# File 'lib/cuba.rb', line 360
def link; req.link? end
|
#match(matcher, segment = SEGMENT) ⇒ Object
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/cuba.rb', line 253
def match(matcher, segment = 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
|
#not_found ⇒ Object
428
429
430
|
# File 'lib/cuba.rb', line 428
def not_found
res.status = 404
end
|
#on(*args, &block) ⇒ Object
The heart of the path / verb / any condition matching.
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/cuba.rb', line 192
def on(*args, &block)
try do
@captures = []
return unless args.all? { |arg| match(arg) }
yield(*captures)
if res.status.nil?
if res.body.empty?
not_found
else
res.[Rack::CONTENT_TYPE] ||= DEFAULT
res.status = 200
end
end
halt(res.finish)
end
end
|
#options ⇒ Object
359
|
# File 'lib/cuba.rb', line 359
def options; req.options? end
|
#param(key, default = nil) ⇒ Object
Ensures that certain request parameters are present. Acts like a precondition / assertion for your route. A default value can be provided as a second argument. In that case, it always matches and the result is either the parameter or the default value.
289
290
291
292
293
|
# File 'lib/cuba.rb', line 289
def param(key, default = nil)
value = req.params[key.to_s] || default
lambda { captures << value unless value.to_s.empty? }
end
|
#patch ⇒ Object
356
|
# File 'lib/cuba.rb', line 356
def patch; req.patch? end
|
#post ⇒ Object
354
|
# File 'lib/cuba.rb', line 354
def post; req.post? end
|
#put ⇒ Object
355
|
# File 'lib/cuba.rb', line 355
def put; req.put? end
|
#root ⇒ Object
Access the root of the application.
341
342
343
|
# File 'lib/cuba.rb', line 341
def root
env[Rack::PATH_INFO] == SLASH || env[Rack::PATH_INFO] == EMPTY
end
|
#run(app) ⇒ Object
If you want to halt the processing of an existing handler and continue it via a different handler.
377
378
379
|
# File 'lib/cuba.rb', line 377
def run(app)
halt app.call(req.env)
end
|
#session ⇒ Object
166
167
168
169
170
|
# File 'lib/cuba.rb', line 166
def session
env["rack.session"] || raise(RuntimeError,
"You're missing a session handler. You can get started " +
"by adding Cuba.use Rack::Session::Cookie")
end
|
#settings ⇒ Object
138
139
140
|
# File 'lib/cuba.rb', line 138
def settings
self.class.settings
end
|
#trace ⇒ Object
362
|
# File 'lib/cuba.rb', line 362
def trace; req.trace? end
|
#unlink ⇒ Object
361
|
# File 'lib/cuba.rb', line 361
def unlink; req.unlink? end
|
#vars ⇒ Object
Returns a hash with the information set by the #with method.
with(role: "admin", site: "main") do
on default do
res.write(vars.inspect)
end
end
424
425
426
|
# File 'lib/cuba.rb', line 424
def vars
env["cuba.vars"] ||= {}
end
|
#with(dict = {}) ⇒ Object
Adds ability to pass information to a nested Cuba application. It receives two parameters: a hash that represents the passed information and a block. The #vars method is used to retrieve a hash with the passed information.
class Platforms < Cuba
define do
platform = vars[:platform]
on default do
res.write(platform) end
end
end
Cuba.define do
on "(heroku|salesforce)" do |platform|
with(platform: platform) do
run(Platforms)
end
end
end
408
409
410
411
412
413
|
# File 'lib/cuba.rb', line 408
def with(dict = {})
old, env["cuba.vars"] = vars, vars.merge(dict)
yield
ensure
env["cuba.vars"] = old
end
|