Class: Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/danta/dispatcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dataObject (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

#actionObject



82
83
84
# File 'lib/danta/dispatcher.rb', line 82

def action
  @action ||= data['action']
end

#dataObject



78
79
80
# File 'lib/danta/dispatcher.rb', line 78

def data
  @data ||= JSON.parse(raw_data)
end

#dispatchObject



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_actionObject



49
50
51
52
# File 'lib/danta/dispatcher.rb', line 49

def exec_action
  Pecari::Player.send(params['command'])
  exec_response
end

#exec_responseObject



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.message
  }.to_json
end

#launch_actionObject



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_responseObject



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

#paramsObject



86
87
88
# File 'lib/danta/dispatcher.rb', line 86

def params
  @params ||= data['params']
end

#unrecognized_responseObject



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_responseObject



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