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
249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/syro.rb', line 249 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
290 291 292 |
# File 'lib/syro.rb', line 290 def capture(arg) @syro_path.capture(arg, inbox) end |
#consume(arg) ⇒ Object
286 287 288 |
# File 'lib/syro.rb', line 286 def consume(arg) @syro_path.consume(arg) end |
#default ⇒ Object
307 308 309 |
# File 'lib/syro.rb', line 307 def default yield; halt(res.finish) end |
#default_headers ⇒ Object
237 238 239 |
# File 'lib/syro.rb', line 237 def default_headers {} end |
#delete ⇒ Object
339 340 341 |
# File 'lib/syro.rb', line 339 def delete root { yield } if req.delete? end |
#env ⇒ Object
203 204 205 |
# File 'lib/syro.rb', line 203 def env @syro_env end |
#get ⇒ Object
319 320 321 |
# File 'lib/syro.rb', line 319 def get root { 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)
282 283 284 |
# File 'lib/syro.rb', line 282 def halt(response) throw(:halt, response) end |
#head ⇒ Object
327 328 329 |
# File 'lib/syro.rb', line 327 def head root { yield } if req.head? end |
#inbox ⇒ Object
233 234 235 |
# File 'lib/syro.rb', line 233 def inbox @syro_inbox end |
#initialize(code) ⇒ Object
199 200 201 |
# File 'lib/syro.rb', line 199 def initialize(code) @syro_code = code end |
#match(arg) ⇒ Object
298 299 300 301 302 303 304 305 |
# File 'lib/syro.rb', line 298 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
311 312 313 |
# File 'lib/syro.rb', line 311 def on(arg) default { yield } if match(arg) end |
#options ⇒ Object
343 344 345 |
# File 'lib/syro.rb', line 343 def root { yield } if req. end |
#patch ⇒ Object
335 336 337 |
# File 'lib/syro.rb', line 335 def patch root { yield } if req.patch? end |
#path ⇒ Object
229 230 231 |
# File 'lib/syro.rb', line 229 def path @syro_path end |
#post ⇒ Object
331 332 333 |
# File 'lib/syro.rb', line 331 def post root { yield } if req.post? end |
#put ⇒ Object
323 324 325 |
# File 'lib/syro.rb', line 323 def put root { 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"
214 215 216 |
# File 'lib/syro.rb', line 214 def req @syro_req end |
#request_class ⇒ Object
241 242 243 |
# File 'lib/syro.rb', line 241 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>")
225 226 227 |
# File 'lib/syro.rb', line 225 def res @syro_res end |
#response_class ⇒ Object
245 246 247 |
# File 'lib/syro.rb', line 245 def response_class Syro::Response end |
#root ⇒ Object
315 316 317 |
# File 'lib/syro.rb', line 315 def root default { yield } if root? end |
#root? ⇒ Boolean
294 295 296 |
# File 'lib/syro.rb', line 294 def root? @syro_path.root? end |
#run(app, inbox = {}) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/syro.rb', line 263 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 |