Class: ApiInputActor

Inherits:
Object show all
Includes:
BasicActor, Celluloid::IO
Defined in:
lib/api_input_actor.rb

Constant Summary collapse

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.

%w(
  search_changed
  select_next_item
  select_prev_item
  select_next_file
  select_prev_file
  tell_selected_item
  tell_selected_item_content
  show_match_lines_toggled
  refresh
)

Instance Method Summary collapse

Methods included from BasicActor

included

Constructor Details

#initializeApiInputActor

Returns a new instance of ApiInputActor.



25
26
27
28
29
# File 'lib/api_input_actor.rb', line 25

def initialize
  subscribe "respond_client", :respond_client
  @server = UNIXServer.new $SOCKET_PATH
  async.watch_input
end

Instance Method Details

#close_serverObject



59
60
61
62
63
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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 = {}) ⇒ Object



65
66
67
68
69
# 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) ⇒ Object



71
72
73
74
# 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_inputObject



31
32
33
34
35
36
37
38
39
# 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