Module: Syro::Deck::API

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

Instance Method Summary collapse

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

#defaultObject



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

def default
  yield; halt(res.finish)
end

#default_headersObject



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

def default_headers
  {}
end

#deleteObject



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

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

#envObject



203
204
205
# File 'lib/syro.rb', line 203

def env
  @syro_env
end

#getObject



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

#headObject



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

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

#inboxObject



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

#optionsObject



343
344
345
# File 'lib/syro.rb', line 343

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

#patchObject



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

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

#pathObject



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

def path
  @syro_path
end

#postObject



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

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

#putObject



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

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"


214
215
216
# File 'lib/syro.rb', line 214

def req
  @syro_req
end

#request_classObject



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

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


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

def res
  @syro_res
end

#response_classObject



245
246
247
# File 'lib/syro.rb', line 245

def response_class
  Syro::Response
end

#rootObject



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

def root
  default { yield } if root?
end

#root?Boolean

Returns:

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