Class: NewlineHw::StreamCommandHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/newline_hw/stream_command_handler.rb

Overview

A json message handler to trigger actions from chrome native messaging

Constant Summary collapse

EVENTS =
[
  :heartbeat,
  :clone_and_open_submission,
  :check_if_cloneable
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ StreamCommandHandler

Returns a new instance of StreamCommandHandler.



14
15
16
17
18
# File 'lib/newline_hw/stream_command_handler.rb', line 14

def initialize(event)
  @event = event["event"].to_sym
  @data = event["data"]
  @message_at = event["message_at"].to_i
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/newline_hw/stream_command_handler.rb', line 7

def data
  @data
end

#eventObject (readonly)

Returns the value of attribute event.



7
8
9
# File 'lib/newline_hw/stream_command_handler.rb', line 7

def event
  @event
end

#message_atObject (readonly)

Returns the value of attribute message_at.



7
8
9
# File 'lib/newline_hw/stream_command_handler.rb', line 7

def message_at
  @message_at
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
# File 'lib/newline_hw/stream_command_handler.rb', line 24

def call
  return handle_unknown unless known_event?
  send(event)

rescue StandardError => e
  handle_fail(e)
end

#check_if_cloneableObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/newline_hw/stream_command_handler.rb', line 46

def check_if_cloneable
  setup = Shell::Setup.new(data["id"], Config.new)
  {
    status: :ok,
    message_at: message_at,
    data: {
      cloneable: setup.cloneable?,
      submission_info: setup.submission_info
    }
  }
end

#clone_and_open_submissionObject



58
59
60
61
62
63
64
# File 'lib/newline_hw/stream_command_handler.rb', line 58

def clone_and_open_submission
  {
    status: :ok,
    message_at: message_at,
    data: GuiTrigger.new(data, Config.new).call
  }
end

#handle_fail(e) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/newline_hw/stream_command_handler.rb', line 75

def handle_fail(e)
  {
    status: :fail,
    event: @event,
    message_at: message_at,
    message: e.message
  }
end

#handle_unknownObject



66
67
68
69
70
71
72
73
# File 'lib/newline_hw/stream_command_handler.rb', line 66

def handle_unknown
  {
    status: :fail,
    message_at: message_at,
    message: \
      "no event handler found in Stream Command Handler for #{@event}"
  }
end

#heartbeatObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/newline_hw/stream_command_handler.rb', line 32

def heartbeat
  {
    status: :ok,
    message_at: message_at,
    data: {
      newline_hw_version: NewlineHw::VERSION,
      ruby_version: RUBY_VERSION,
      newline_hw_config_path: Config::CONFIG_PATH,
      newline_hw_path: NewlineHw.root_path,
      newline_hw_config: Config.new.config
    }
  }
end

#known_event?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/newline_hw/stream_command_handler.rb', line 20

def known_event?
  EVENTS.include?(event)
end