Module: Syro::Deck::API

Included in:
Syro::Deck
Defined in:
lib/syro.rb

Instance Method Summary collapse

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

#defaultObject



321
322
323
# File 'lib/syro.rb', line 321

def default
  yield; halt(res.finish)
end

#default_headersObject



251
252
253
# File 'lib/syro.rb', line 251

def default_headers
  {}
end

#deleteObject



353
354
355
# File 'lib/syro.rb', line 353

def delete
  root { res.status = 200; yield } if req.delete?
end

#envObject



217
218
219
# File 'lib/syro.rb', line 217

def env
  @syro_env
end

#getObject



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

#headObject



341
342
343
# File 'lib/syro.rb', line 341

def head
  root { res.status = 200; yield } if req.head?
end

#inboxObject



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

#optionsObject



357
358
359
# File 'lib/syro.rb', line 357

def options
  root { res.status = 200; yield } if req.options?
end

#patchObject



349
350
351
# File 'lib/syro.rb', line 349

def patch
  root { res.status = 200; yield } if req.patch?
end

#pathObject



243
244
245
# File 'lib/syro.rb', line 243

def path
  @syro_path
end

#postObject



345
346
347
# File 'lib/syro.rb', line 345

def post
  root { res.status = 200; yield } if req.post?
end

#putObject



337
338
339
# File 'lib/syro.rb', line 337

def put
  root { res.status = 200; yield } if req.put?
end

#reqObject

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_classObject



255
256
257
# File 'lib/syro.rb', line 255

def request_class
  Rack::Request
end

#resObject

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_classObject



259
260
261
# File 'lib/syro.rb', line 259

def response_class
  Syro::Response
end

#rootObject



329
330
331
# File 'lib/syro.rb', line 329

def root
  default { yield } if root?
end

#root?Boolean

Returns:

  • (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