Class: Norikra::WebUI::API

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/norikra/webui/api.rb

Overview

mounted on ‘/api’

Constant Summary collapse

@@shut_off_mode =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.engine=(engine) ⇒ Object



17
18
19
# File 'lib/norikra/webui/api.rb', line 17

def self.engine=(engine)
  @@engine = engine
end

.shut_off=(mode) ⇒ Object



23
24
25
# File 'lib/norikra/webui/api.rb', line 23

def self.shut_off=(mode)
  @@shut_off_mode = mode
end

Instance Method Details

#engineObject



66
# File 'lib/norikra/webui/api.rb', line 66

def engine; @@engine; end

#loggerObject



15
# File 'lib/norikra/webui/api.rb', line 15

def logger ; Norikra::Log.logger ; end

#logging(type, handler, args = [], opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/norikra/webui/api.rb', line 37

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.message}"
    if opts[:on_error_hook]
      opts[:on_error_hook].call(e.class, e.message)
    else
      halt 400, {'Content-Type' => 'application/json'}, {error: e.class.to_s, message: e.message}.to_json
    end
  rescue => e
    logger.error "WebAPI #{e.class}: #{e.message}"
    e.backtrace.each do |t|
      logger.error "  " + t
    end
    if opts[:on_error_hook]
      opts[:on_error_hook].call(e.class, e.message)
    else
      halt 500, {'Content-Type' => 'application/json'}, {error: e.class.to_s, message: e.message}.to_json
    end
  end
end

#parse_args(param_names, request) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/norikra/webui/api.rb', line 70

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

#shut_off_modeObject



68
# File 'lib/norikra/webui/api.rb', line 68

def shut_off_mode; @@shut_off_mode; end