Sha256: 297f1c94da73c965d135c2f099837e409618725970a11fea74faebcee0313d81

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

class JPC::Invoker
  include JPC::Helpers

  def initialize(handler)
    @handler = handler
  end

  def invoke(json)
    request = Oj.load(json)
    response = execute(request)
  end

  private

  def execute(request)
    method = request['method']

    fail "Method #{method} not allowed" unless method_allowed?(method)

    @handler.token = request['token'] if request['token']

    if @handler.respond_to?(:authorized?)
      fail 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

    make_result(request['id'], result)
  rescue => e
    case e.class.name
    when 'JPC::Errors::UnauthorizedError'
      code = 32001
    else
      code = 32000
    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)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jpc-1.2.0 lib/jpc/invoker.rb
jpc-1.1.0 lib/jpc/invoker.rb