Module: Syro::Deck::API

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

Instance Method Summary collapse

Instance Method Details

#call(env, inbox) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/syro.rb', line 246

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



287
288
289
# File 'lib/syro.rb', line 287

def capture(arg)
  @syro_path.capture(arg, inbox)
end

#consume(arg) ⇒ Object



283
284
285
# File 'lib/syro.rb', line 283

def consume(arg)
  @syro_path.consume(arg)
end

#defaultObject



304
305
306
# File 'lib/syro.rb', line 304

def default
  yield; halt(res.finish)
end

#default_headersObject



234
235
236
# File 'lib/syro.rb', line 234

def default_headers
  {}
end

#deleteObject



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

def delete
  root { yield } if req.delete?
end

#envObject



200
201
202
# File 'lib/syro.rb', line 200

def env
  @syro_env
end

#getObject



316
317
318
# File 'lib/syro.rb', line 316

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)


279
280
281
# File 'lib/syro.rb', line 279

def halt(response)
  throw(:halt, response)
end

#headObject



324
325
326
# File 'lib/syro.rb', line 324

def head
  root { yield } if req.head?
end

#inboxObject



230
231
232
# File 'lib/syro.rb', line 230

def inbox
  @syro_inbox
end

#initialize(code) ⇒ Object



196
197
198
# File 'lib/syro.rb', line 196

def initialize(code)
  @syro_code = code
end

#match(arg) ⇒ Object



295
296
297
298
299
300
301
302
# File 'lib/syro.rb', line 295

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



308
309
310
# File 'lib/syro.rb', line 308

def on(arg)
  default { yield } if match(arg)
end

#optionsObject



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

def options
  root { yield } if req.options?
end

#patchObject



332
333
334
# File 'lib/syro.rb', line 332

def patch
  root { yield } if req.patch?
end

#pathObject



226
227
228
# File 'lib/syro.rb', line 226

def path
  @syro_path
end

#postObject



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

def post
  root { yield } if req.post?
end

#putObject



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

def put
  root { 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"


211
212
213
# File 'lib/syro.rb', line 211

def req
  @syro_req
end

#request_classObject



238
239
240
# File 'lib/syro.rb', line 238

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>")


222
223
224
# File 'lib/syro.rb', line 222

def res
  @syro_res
end

#response_classObject



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

def response_class
  Syro::Response
end

#rootObject



312
313
314
# File 'lib/syro.rb', line 312

def root
  default { yield } if root?
end

#root?Boolean

Returns:

  • (Boolean)


291
292
293
# File 'lib/syro.rb', line 291

def root?
  @syro_path.root?
end

#run(app, inbox = {}) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
# File 'lib/syro.rb', line 260

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