Class: Opi::API
- Inherits:
-
Object
- Object
- Opi::API
- Defined in:
- lib/opi/api.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
- .after(method) ⇒ Object
- .after_filters ⇒ Object
- .before(method) ⇒ Object
- .before_filters ⇒ Object
- .delete(path, options = {}, &block) ⇒ Object
- .get(path, options = {}, &block) ⇒ Object
- .helpers(&block) ⇒ Object
- .post(path, options = {}, &block) ⇒ Object
- .put(path, options = {}, &block) ⇒ Object
- .route(method, path, options = {}, block) ⇒ Object
- .router ⇒ Object
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
Constructor Details
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
55 56 57 |
# File 'lib/opi/api.rb', line 55 def logger @logger end |
Class Method Details
.after(method) ⇒ Object
31 32 33 |
# File 'lib/opi/api.rb', line 31 def after(method) after_filters << method end |
.after_filters ⇒ Object
39 40 41 |
# File 'lib/opi/api.rb', line 39 def after_filters @after_filters ||= [] end |
.before(method) ⇒ Object
27 28 29 |
# File 'lib/opi/api.rb', line 27 def before(method) before_filters << method end |
.before_filters ⇒ Object
35 36 37 |
# File 'lib/opi/api.rb', line 35 def before_filters @before_filters ||= [] end |
.delete(path, options = {}, &block) ⇒ Object
18 19 20 |
# File 'lib/opi/api.rb', line 18 def delete(path, ={}, &block) route 'DELETE', path, , block end |
.get(path, options = {}, &block) ⇒ Object
6 7 8 |
# File 'lib/opi/api.rb', line 6 def get(path, ={}, &block) route 'GET', path, , block end |
.helpers(&block) ⇒ Object
47 48 49 50 51 |
# File 'lib/opi/api.rb', line 47 def helpers(&block) mod = Module.new mod.class_eval &block Context.send :include, mod end |
.post(path, options = {}, &block) ⇒ Object
10 11 12 |
# File 'lib/opi/api.rb', line 10 def post(path, ={}, &block) route 'POST', path, , block end |
.put(path, options = {}, &block) ⇒ Object
14 15 16 |
# File 'lib/opi/api.rb', line 14 def put(path, ={}, &block) route 'PUT', path, , block end |
.route(method, path, options = {}, block) ⇒ Object
22 23 24 25 |
# File 'lib/opi/api.rb', line 22 def route(method, path, ={}, block) # TODO: remove&replace existing routes (on reload) router.routes.unshift({:method => method, :path => path, :options => , :block => block}) end |
Instance Method Details
#call(env) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/opi/api.rb', line 63 def call(env) logger.debug env.inspect.cyan request = Request.new(env) response = Response.new begin Loader.reload! route, params = self.class.router.route(request.method, request.path) request.params.merge!(params) if params and params.is_a? Hash request.params.merge!('splat' => params.join(',')) if params and params.is_a? Array start_time = Time.now logger.info " Started #{request.method} \"#{request.path}\" for #{request.ip} at #{start_time.to_s(:short)}" logger.info " Parameters: #{request.params}" if route logger.debug "#{request.method} #{request.path} => route #{route.inspect}".green context = Context.new(env, logger, route, request, response, self.class.before_filters, self.class.after_filters) response = context.run else logger.debug "#{request.method} #{request.path} => route not found".red response.not_found! end rescue Exception => e logger.error "#{e..red}\n #{e.backtrace[0..9].join("\n ").yellow}" response.internal_server_error!(e) end logger.info " Completed #{response.status} in #{((Time.now - start_time)*1000).round(2)}ms" [response.status, response.header, response.body] end |