Class: Squad
- Inherits:
-
Object
show all
- Defined in:
- lib/squad.rb
Defined Under Namespace
Classes: BadRequestError, Collection, Error, NotFoundError, NotImplementedError, Resource
Constant Summary
collapse
{"Content-Type" => 'application/json;charset=UTF-8'}
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(&block) ⇒ Squad
Returns a new instance of Squad.
10
11
12
|
# File 'lib/squad.rb', line 10
def initialize(&block)
instance_eval(&block)
end
|
Class Method Details
.application(&block) ⇒ Object
17
18
19
|
# File 'lib/squad.rb', line 17
def self.application(&block)
@app = new(&block)
end
|
.call(env) ⇒ Object
21
22
23
|
# File 'lib/squad.rb', line 21
def self.call(env)
@app.call(env)
end
|
.redis ⇒ Object
14
|
# File 'lib/squad.rb', line 14
def self.redis; @redis = settings[:redis] || Redic.new end
|
.routes ⇒ Object
25
|
# File 'lib/squad.rb', line 25
def self.routes; @routes ||= {} end
|
.settings ⇒ Object
15
|
# File 'lib/squad.rb', line 15
def self.settings; @settings ||= {} end
|
Instance Method Details
#call(env) ⇒ Object
31
|
# File 'lib/squad.rb', line 31
def call(env); dup.call!(env) end
|
#call!(env) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/squad.rb', line 33
def call!(env)
request = Rack::Request.new(env)
seg = Seg.new(request.path_info)
inbox = {}
seg.capture(:segment, inbox)
segment = inbox[:segment].to_sym
raise BadRequestError unless klass = self.class.routes[segment]
resource = klass.new(segment)
resource.run(seg)
resource.render(request)
[resource.status, resource., [resource.to_json]]
rescue Error => e
[e.status, e., [JSON.dump(e.body)]]
rescue Exception => e
puts "message: #{e.message}, b: #{e.backtrace}"
error = Error.new
[error.status, error., [JSON.dump(error.body)]]
end
|
#resources(name, &block) ⇒ Object
27
28
29
|
# File 'lib/squad.rb', line 27
def resources(name, &block)
self.class.routes[name] = Resource.factor(&block)
end
|