Class: Traim
- Inherits:
-
Object
show all
- Defined in:
- lib/traim.rb
Defined Under Namespace
Classes: Application, ArController, BadRequestError, Controller, Error, ForbiddenError, Helper, MethodNotAllowedError, NotAcceptableError, NotFoundError, NotImplementedError, Resource, UnauthorizedError
Constant Summary
collapse
{"Content-Type" => 'application/json;charset=UTF-8'}
- TRAIM_ENV =
ENV['TRAIM_ENV'] || 'development'
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(&block) ⇒ Traim
Returns a new instance of Traim.
12
13
14
15
16
17
18
19
20
|
# File 'lib/traim.rb', line 12
def initialize(&block)
if self.class.logger == nil
self.class.logger = Logger.new(STDOUT)
self.class.logger.level = Logger::INFO
end
@app = Application.new
@app.compile(&block)
end
|
Class Method Details
.application(&block) ⇒ Object
24
25
26
|
# File 'lib/traim.rb', line 24
def self.application(&block)
@instance = new(&block)
end
|
.call(env) ⇒ Object
31
32
33
|
# File 'lib/traim.rb', line 31
def self.call(env)
@instance.dup.call(env)
end
|
.config {|_self| ... } ⇒ Object
35
36
37
38
39
|
# File 'lib/traim.rb', line 35
def self.config(&block)
config_file = YAML.load_file("#{Dir.pwd}/config/database.yml")
ActiveRecord::Base.establish_connection(config_file[TRAIM_ENV])
yield self
end
|
.logger ⇒ Object
29
|
# File 'lib/traim.rb', line 29
def self.logger; @logger end
|
.logger=(logger) ⇒ Object
28
|
# File 'lib/traim.rb', line 28
def self.logger=(logger); @logger = logger end
|
.settings ⇒ Object
22
|
# File 'lib/traim.rb', line 22
def self.settings; @settings ||= {} end
|
Instance Method Details
#call(env) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/traim.rb', line 43
def call(env)
request = Rack::Request.new(env)
logger.info("#{request.request_method} #{request.path_info} from #{request.ip}")
logger.debug("Parameters: #{request.params}")
@app.route(request)
rescue Error => e
logger.error(e)
[e.status, e., [JSON.dump(e.body)]]
rescue Exception => e
logger.error(e)
error = Error.new
[error.status, error., [JSON.dump(error.body)]]
end
|
#logger ⇒ Object
41
|
# File 'lib/traim.rb', line 41
def logger; Traim.logger end
|