Class: Norikra::WebUI::API
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Norikra::WebUI::API
- Defined in:
- lib/norikra/webui/api.rb
Overview
mounted on ‘/api’
Class Method Summary collapse
Instance Method Summary collapse
- #engine ⇒ Object
- #logger ⇒ Object
- #logging(type, handler, args = [], opts = {}) ⇒ Object
- #parse_args(param_names, request) ⇒ Object
Class Method Details
.engine=(engine) ⇒ Object
17 18 19 |
# File 'lib/norikra/webui/api.rb', line 17 def self.engine=(engine) @@engine = engine end |
Instance Method Details
#engine ⇒ Object
60 |
# File 'lib/norikra/webui/api.rb', line 60 def engine; @@engine; end |
#logger ⇒ Object
15 |
# File 'lib/norikra/webui/api.rb', line 15 def logger ; Norikra::Log.logger ; end |
#logging(type, handler, args = [], opts = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/norikra/webui/api.rb', line 31 def logging(type, handler, args=[], opts={}) if type == :manage debug "WebAPI", :handler => handler.to_s, :args => args else trace "WebAPI", :handler => handler.to_s, :args => args end begin yield rescue Norikra::ClientError => e logger.info "WebAPI #{e.class}: #{e.}" if opts[:on_error_hook] opts[:on_error_hook].call(e.class, e.) else halt 400, {'Content-Type' => 'application/json'}, {error: e.class.to_s, message: e.}.to_json end rescue => e logger.error "WebAPI #{e.class}: #{e.}" e.backtrace.each do |t| logger.error " " + t end if opts[:on_error_hook] opts[:on_error_hook].call(e.class, e.) else halt 500, {'Content-Type' => 'application/json'}, {error: e.class.to_s, message: e.}.to_json end end end |
#parse_args(param_names, request) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/norikra/webui/api.rb', line 62 def parse_args(param_names, request) body = request.body.read parsed = begin JSON.parse(body) rescue JSON::ParserError => e info "JSON content body parse error" {} end return parsed if parsed.is_a?(Array) param_names.map{|name| parsed[name] } end |