Module: Syro::Deck::API

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

Instance Method Summary collapse

Instance Method Details

#call(env, inbox) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/syro.rb', line 231

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



265
266
267
# File 'lib/syro.rb', line 265

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

#consume(arg) ⇒ Object



261
262
263
# File 'lib/syro.rb', line 261

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

#defaultObject



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

def default
  yield; halt(res.finish)
end

#default_headersObject



219
220
221
# File 'lib/syro.rb', line 219

def default_headers
  return {}
end

#deleteObject



310
311
312
# File 'lib/syro.rb', line 310

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

#envObject



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

def env
  @syro_env
end

#getObject



294
295
296
# File 'lib/syro.rb', line 294

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

#halt(response) ⇒ Object



257
258
259
# File 'lib/syro.rb', line 257

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

#inboxObject



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

def inbox
  @syro_inbox
end

#initialize(code) ⇒ Object



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

def initialize(code)
  @syro_code = code
end

#match(arg) ⇒ Object



273
274
275
276
277
278
279
280
# File 'lib/syro.rb', line 273

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



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

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

#patchObject



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

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

#pathObject



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

def path
  @syro_path
end

#postObject



302
303
304
# File 'lib/syro.rb', line 302

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

#putObject



298
299
300
# File 'lib/syro.rb', line 298

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

#reqObject



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

def req
  @syro_req
end

#request_classObject



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

def request_class
  Rack::Request
end

#resObject



207
208
209
# File 'lib/syro.rb', line 207

def res
  @syro_res
end

#response_classObject



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

def response_class
  Syro::Response
end

#rootObject



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

def root
  default { yield } if root?
end

#root?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/syro.rb', line 269

def root?
  @syro_path.root?
end

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



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

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