Module: Syro::Deck::API
- Included in:
- Syro::Deck
- Defined in:
- lib/syro.rb
Instance Method Summary collapse
- #call(env, inbox) ⇒ Object
- #capture(arg) ⇒ Object
- #consume(arg) ⇒ Object
- #default ⇒ Object
- #default_headers ⇒ Object
- #delete ⇒ Object
- #env ⇒ Object
- #get ⇒ Object
-
#halt(response) ⇒ Object
Immediately stops the request and returns ‘response` as per Rack’s specification.
- #head ⇒ Object
- #inbox ⇒ Object
- #initialize(code) ⇒ Object
- #match(arg) ⇒ Object
- #on(arg) ⇒ Object
- #options ⇒ Object
- #patch ⇒ Object
- #path ⇒ Object
- #post ⇒ Object
- #put ⇒ Object
-
#req ⇒ Object
Returns the incoming request object.
- #request_class ⇒ Object
-
#res ⇒ Object
Returns the current response object.
- #response_class ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
- #run(app, inbox = {}) ⇒ Object
Instance Method Details
#call(env, inbox) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/syro.rb', line 263 def call(env, inbox) @syro_env = env @syro_req = request_class.new(env) @syro_res = response_class.new(default_headers) @syro_path = Seg.new(env.fetch(Rack::PATH_INFO)) @syro_inbox = inbox catch(:halt) do instance_eval(&@syro_code) @syro_res.finish end end |
#capture(arg) ⇒ Object
304 305 306 |
# File 'lib/syro.rb', line 304 def capture(arg) @syro_path.capture(arg, inbox) end |
#consume(arg) ⇒ Object
300 301 302 |
# File 'lib/syro.rb', line 300 def consume(arg) @syro_path.consume(arg) end |
#default ⇒ Object
321 322 323 |
# File 'lib/syro.rb', line 321 def default yield; halt(res.finish) end |
#default_headers ⇒ Object
251 252 253 |
# File 'lib/syro.rb', line 251 def default_headers {} end |
#delete ⇒ Object
353 354 355 |
# File 'lib/syro.rb', line 353 def delete root { res.status = 200; yield } if req.delete? end |
#env ⇒ Object
217 218 219 |
# File 'lib/syro.rb', line 217 def env @syro_env end |
#get ⇒ Object
333 334 335 |
# File 'lib/syro.rb', line 333 def get root { res.status = 200; yield } if req.get? end |
#halt(response) ⇒ Object
Immediately stops the request and returns ‘response` as per Rack’s specification.
halt([200, { "Content-Type" => "text/html" }, ["hello"]])
halt([res.status, res.headers, res.body])
halt(res.finish)
296 297 298 |
# File 'lib/syro.rb', line 296 def halt(response) throw(:halt, response) end |
#head ⇒ Object
341 342 343 |
# File 'lib/syro.rb', line 341 def head root { res.status = 200; yield } if req.head? end |
#inbox ⇒ Object
247 248 249 |
# File 'lib/syro.rb', line 247 def inbox @syro_inbox end |
#initialize(code) ⇒ Object
213 214 215 |
# File 'lib/syro.rb', line 213 def initialize(code) @syro_code = code end |
#match(arg) ⇒ Object
312 313 314 315 316 317 318 319 |
# File 'lib/syro.rb', line 312 def match(arg) case arg when String then consume(arg) when Symbol then capture(arg) when true then true else false end end |
#on(arg) ⇒ Object
325 326 327 |
# File 'lib/syro.rb', line 325 def on(arg) default { yield } if match(arg) end |
#options ⇒ Object
357 358 359 |
# File 'lib/syro.rb', line 357 def root { res.status = 200; yield } if req. end |
#patch ⇒ Object
349 350 351 |
# File 'lib/syro.rb', line 349 def patch root { res.status = 200; yield } if req.patch? end |
#path ⇒ Object
243 244 245 |
# File 'lib/syro.rb', line 243 def path @syro_path end |
#post ⇒ Object
345 346 347 |
# File 'lib/syro.rb', line 345 def post root { res.status = 200; yield } if req.post? end |
#put ⇒ Object
337 338 339 |
# File 'lib/syro.rb', line 337 def put root { res.status = 200; yield } if req.put? end |
#req ⇒ Object
Returns the incoming request object. This object is an instance of Rack::Request.
req.post? # => true
req.params # => { "username" => "bob", "password" => "secret" }
req[:username] # => "bob"
228 229 230 |
# File 'lib/syro.rb', line 228 def req @syro_req end |
#request_class ⇒ Object
255 256 257 |
# File 'lib/syro.rb', line 255 def request_class Rack::Request end |
#res ⇒ Object
Returns the current response object. This object is an instance of Syro::Response.
res.status = 200
res["Content-Type"] = "text/html"
res.write("<h1>Welcome back!</h1>")
239 240 241 |
# File 'lib/syro.rb', line 239 def res @syro_res end |
#response_class ⇒ Object
259 260 261 |
# File 'lib/syro.rb', line 259 def response_class Syro::Response end |
#root ⇒ Object
329 330 331 |
# File 'lib/syro.rb', line 329 def root default { yield } if root? end |
#root? ⇒ Boolean
308 309 310 |
# File 'lib/syro.rb', line 308 def root? @syro_path.root? end |
#run(app, inbox = {}) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/syro.rb', line 277 def run(app, inbox = {}) path, script = env[Rack::PATH_INFO], env[Rack::SCRIPT_NAME] env[Rack::PATH_INFO] = @syro_path.curr env[Rack::SCRIPT_NAME] = @syro_path.prev env[Syro::INBOX] = inbox halt(app.call(env)) ensure env[Rack::PATH_INFO], env[Rack::SCRIPT_NAME] = path, script end |