Class: Dispatcher
- Inherits:
-
Object
- Object
- Dispatcher
- Defined in:
- lib/danta/dispatcher.rb
Instance Attribute Summary collapse
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
Instance Method Summary collapse
- #action ⇒ Object
- #data ⇒ Object
- #dispatch ⇒ Object
- #exec_action ⇒ Object
- #exec_response ⇒ Object
-
#initialize(raw_data:) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #invalid_response(exception) ⇒ Object
- #launch_action ⇒ Object
- #launch_response ⇒ Object
- #params ⇒ Object
- #unrecognized_response ⇒ Object
- #videos_response ⇒ Object
Constructor Details
#initialize(raw_data:) ⇒ Dispatcher
Returns a new instance of Dispatcher.
9 10 11 |
# File 'lib/danta/dispatcher.rb', line 9 def initialize(raw_data:) @raw_data = raw_data end |
Instance Attribute Details
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
7 8 9 |
# File 'lib/danta/dispatcher.rb', line 7 def raw_data @raw_data end |
Instance Method Details
#action ⇒ Object
82 83 84 |
# File 'lib/danta/dispatcher.rb', line 82 def action @action ||= data['action'] end |
#data ⇒ Object
78 79 80 |
# File 'lib/danta/dispatcher.rb', line 78 def data @data ||= JSON.parse(raw_data) end |
#dispatch ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/danta/dispatcher.rb', line 13 def dispatch case action when 'videos' videos_response when 'launch' launch_action when 'exec' exec_action else unrecognized_response end rescue StandardError => e invalid_response(e) end |
#exec_action ⇒ Object
49 50 51 52 |
# File 'lib/danta/dispatcher.rb', line 49 def exec_action Pecari::Player.send(params['command']) exec_response end |
#exec_response ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/danta/dispatcher.rb', line 54 def exec_response { action: action, status: 'success', data: "executing #{params['command']}" }.to_json end |
#invalid_response(exception) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/danta/dispatcher.rb', line 70 def invalid_response(exception) { action: 'invalid_action', status: 'fail', data: exception. }.to_json end |
#launch_action ⇒ Object
36 37 38 39 |
# File 'lib/danta/dispatcher.rb', line 36 def launch_action Pecari::Player.launch(CGI.unescape(params['video'])) launch_response end |
#launch_response ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/danta/dispatcher.rb', line 41 def launch_response { action: action, status: 'success', data: "playing #{params['video']}" }.to_json end |
#params ⇒ Object
86 87 88 |
# File 'lib/danta/dispatcher.rb', line 86 def params @params ||= data['params'] end |
#unrecognized_response ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/danta/dispatcher.rb', line 62 def unrecognized_response { action: unrecognized_action, status: 'fail', data: '' }.to_json end |
#videos_response ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/danta/dispatcher.rb', line 28 def videos_response { action: action, status: 'success', data: Danta::VideoLibrary.new.videos.to_json }.to_json end |