lib/jpc/invoker.rb in jpc-1.2.0 vs lib/jpc/invoker.rb in jpc-1.2.1

- old
+ new

@@ -1,53 +1,55 @@ -class JPC::Invoker - include JPC::Helpers +module JPC + class Invoker + include JPC::Helpers - def initialize(handler) - @handler = handler - end + def initialize(handler) + @handler = handler + end - def invoke(json) - request = Oj.load(json) - response = execute(request) - end + def invoke(json) + request = Oj.load(json) + execute(request) + end - private + private - def execute(request) - method = request['method'] + def execute(request) + method = request['method'] - fail "Method #{method} not allowed" unless method_allowed?(method) + raise "Method #{method} not allowed" unless method_allowed?(method) - @handler.token = request['token'] if request['token'] + @handler.token = request['token'] if request['token'] - if @handler.respond_to?(:authorized?) - fail JPC::Errors::UnauthorizedError unless @handler.authorized?(method) - end + if @handler.respond_to?(:authorized?) + raise JPC::Errors::UnauthorizedError unless @handler.authorized?(method) + end - if request['params'].is_a?(Array) - result = @handler.public_send(method, *request['params']) - elsif %w(Hash String Integer).include?(request['params'].class.name) - result = @handler.public_send(method, request['params']) - else - result = @handler.public_send(method) - end + result = if request['params'].is_a?(Array) + @handler.public_send(method, *request['params']) + elsif %w(Hash String Integer).include?(request['params'].class.name) + @handler.public_send(method, request['params']) + else + @handler.public_send(method) + end - make_result(request['id'], result) - rescue => e - case e.class.name - when 'JPC::Errors::UnauthorizedError' - code = 32001 - else - code = 32000 + make_result(request['id'], result) + rescue => e + code = case e.class.name + when 'JPC::Errors::UnauthorizedError' + 32001 + else + 32000 + end + + make_error( + request['id'], + code, + "Method #{method}: #{e.message}. See #{e.backtrace[0]}" + ) end - make_error( - request['id'], - code, - "Method #{method}: #{e.message}. See #{e.backtrace[0]}" - ) - end - - def method_allowed?(name) - @handler.send(:allowed_methods).include?(name.to_s) + def method_allowed?(name) + @handler.send(:allowed_methods).include?(name.to_s) + end end end