Class: ApiInputActor
- 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
- #close_server ⇒ Object
-
#initialize ⇒ ApiInputActor
constructor
A new instance of ApiInputActor.
- #process_input(input) ⇒ Object
- #respond_client(_, message, extras = {}) ⇒ Object
- #send_message(msg, arg) ⇒ Object
- #watch_input ⇒ Object
Methods included from BasicActor
Constructor Details
#initialize ⇒ ApiInputActor
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_server ⇒ Object
59 60 61 62 63 |
# File 'lib/api_input_actor.rb', line 59 def close_server "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) "input: #{input.inspect}" parsed = JSON.parse(input) = parsed["message"] args = parsed["args"] = parsed["options"] "message: #{.inspect}\nargs: #{args.inspect}\noptions: #{.inspect}" unless PERMITTED_MESSAGES_LIST.include?() "Message not permitted, ignoring." return end publish *[, args, ].compact end |
#respond_client(_, message, extras = {}) ⇒ Object
65 66 67 68 69 |
# File 'lib/api_input_actor.rb', line 65 def respond_client(_, , extras={}) payload = { message: }.merge(extras) "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 (msg, arg) "Sending #{msg}, #{arg}" @client.puts [msg, arg].join("|") if @client end |
#watch_input ⇒ Object
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 |