class ApiInputActor

Constants

PERMITTED_MESSAGES_LIST

Since the inputs are translated into internal actor messages, we can have a little extra security by only permitting certain messages, because some messages are really for internal use only.

Public Class Methods

new() click to toggle source
# File lib/api_input_actor.rb, line 25
def initialize
  subscribe "respond_client", :respond_client
  @server = UNIXServer.new $SOCKET_PATH
  async.watch_input
end

Public Instance Methods

close_server() click to toggle source
# File lib/api_input_actor.rb, line 59
def close_server
  debug_message "closing server" if @server
  @server.close if @server
  File.delete($SOCKET_PATH)
end
process_input(input) click to toggle source
# File lib/api_input_actor.rb, line 41
def process_input(input)
  debug_message "input: #{input.inspect}"

  parsed = JSON.parse(input)
  message = parsed["message"]
  args    = parsed["args"]
  options = parsed["options"]

  debug_message "message: #{message.inspect}\nargs: #{args.inspect}\noptions: #{options.inspect}"

  unless PERMITTED_MESSAGES_LIST.include?(message)
    debug_message "Message not permitted, ignoring."
    return
  end

  publish *[message, args, options].compact
end
respond_client(_, message, extras={}) click to toggle source
# File lib/api_input_actor.rb, line 65
def respond_client(_, message, extras={})
  payload = { message: message }.merge(extras)
  debug_message "Responding #{payload.inspect}"
  @client.puts payload.to_json if @client
end
send_message(msg, arg) click to toggle source
# File lib/api_input_actor.rb, line 71
def send_message(msg, arg)
  debug_message "Sending #{msg}, #{arg}"
  @client.puts [msg, arg].join("|") if @client
end
watch_input() click to toggle source
# File lib/api_input_actor.rb, line 31
def watch_input
  loop do
    @client = @server.accept
    while input = @client.gets
      process_input(input.chomp)
    end
    @client.close
  end
end