Class: Clapton::ClaptonChannel

Inherits:
ApplicationCable::Channel
  • Object
show all
Defined in:
app/channels/clapton/clapton_channel.rb

Instance Method Summary collapse

Instance Method Details

#action(data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/channels/clapton/clapton_channel.rb', line 7

def action(data)
  state = data["data"]["state"]["name"].constantize.new(JSON.parse(data["data"]["state"]["attributes"].to_json, symbolize_names: true))

  if state.respond_to?(data["data"]["state"]["action"])
    state.public_send(data["data"]["state"]["action"], JSON.parse(data["data"]["params"].to_json, symbolize_names: true)) do |options = {}|
      ActionCable.server.broadcast("clapton_channel", {
        status: "success",
        continue: options[:continue],
        stream: true,
        data: {
          component: {
            name: data["data"]["component"]["name"],
            id: data["data"]["component"]["id"],
          },
          state: state.to_h,
          focus: data["data"]["focus"] 
        }
      })
    end
    if state.errors.any?
      ActionCable.server.broadcast("clapton_channel", {
        status: "error",
        errors: state.errors,
        data: {
          component: {
            name: data["data"]["component"]["name"],
            id: data["data"]["component"]["id"],
          },
          state: state.to_h,
          focus: data["data"]["focus"]
        }
      })
      return
    end

    ActionCable.server.broadcast("clapton_channel", {
      status: "success",
      stream: false,
      continue: false,
      data: {
        component: {
          name: data["data"]["component"]["name"],
          id: data["data"]["component"]["id"],
        },
        state: state.to_h,
        focus: data["data"]["focus"]
      }
    })
  else
    ActionCable.server.broadcast("clapton_channel", {
      status: "error",
      message: "Invalid action",
      data: {
        component: {
          name: data["data"]["component"]["name"],
          id: data["data"]["component"]["id"],
        },
        state: state.to_h,
        focus: data["data"]["focus"]
      }
    })
  end
end

#subscribedObject



3
4
5
# File 'app/channels/clapton/clapton_channel.rb', line 3

def subscribed
  stream_from "clapton_channel"
end