Class: Syntropy::RPCAPI
- Inherits:
-
Object
- Object
- Syntropy::RPCAPI
- Defined in:
- lib/syntropy/rpc_api.rb
Constant Summary collapse
- INTERNAL_SERVER_ERROR =
Qeweney::Status::INTERNAL_SERVER_ERROR
Instance Method Summary collapse
- #call(req) ⇒ Object
- #error_response(err) ⇒ Object
-
#initialize(env) ⇒ RPCAPI
constructor
A new instance of RPCAPI.
- #invoke(req) ⇒ Object
Constructor Details
#initialize(env) ⇒ RPCAPI
Returns a new instance of RPCAPI.
9 10 11 |
# File 'lib/syntropy/rpc_api.rb', line 9 def initialize(env) @env = env end |
Instance Method Details
#call(req) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/syntropy/rpc_api.rb', line 13 def call(req) response, status = invoke(req) req.respond( response.to_json, ':status' => status, 'Content-Type' => 'application/json' ) end |
#error_response(err) ⇒ Object
43 44 45 46 47 |
# File 'lib/syntropy/rpc_api.rb', line 43 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. }, http_status] end |
#invoke(req) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/syntropy/rpc_api.rb', line 22 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 |