Class: Syntropy::RPCAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/syntropy/rpc_api.rb

Constant Summary collapse

INTERNAL_SERVER_ERROR =
Qeweney::Status::INTERNAL_SERVER_ERROR

Instance Method Summary collapse

Instance Method Details

#call(req) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/syntropy/rpc_api.rb', line 9

def call(req)
  response, status = invoke(req)
  req.respond(
    response.to_json,
    ':status'       => status,
    'Content-Type'  => 'application/json'
  )
end

#error_response(err) ⇒ Object



39
40
41
42
43
# File 'lib/syntropy/rpc_api.rb', line 39

def error_response(err)
  http_status = err.respond_to?(:http_status) ? err.http_status : INTERNAL_SERVER_ERROR
  error_name = err.class.name
  [{ status: error_name, message: err.message }, http_status]
end

#invoke(req) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/syntropy/rpc_api.rb', line 18

def invoke(req)
  q = req.validate_param(:q, String)
  response = case req.method
  when 'get'
    send(q.to_sym, req)
  when 'post'
    send(:"#{q}!", req)
  else
    raise Syntropy::Error.new(Qeweney::Status::METHOD_NOT_ALLOWED)
  end
  [{ status: 'OK', response: response }, Qeweney::Status::OK]
rescue => e
  if !e.is_a?(Syntropy::Error)
    p e
    p e.backtrace
  end
  error_response(e)
end