Class: Brief::Server::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/server/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Socket

Returns a new instance of Socket.



5
6
7
8
9
10
11
12
# File 'lib/brief/server/socket.rb', line 5

def initialize(options={})
  @root = Pathname(options.fetch(:root))
  @websocket = options.fetch(:websocket)
  @briefcases = {}.to_mash
  @briefcase_options = options.fetch(:briefcase_options, {})
  load_briefcases
  setup_interface
end

Instance Attribute Details

#briefcasesObject (readonly)

Returns the value of attribute briefcases.



3
4
5
# File 'lib/brief/server/socket.rb', line 3

def briefcases
  @briefcases
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/brief/server/socket.rb', line 3

def root
  @root
end

#websocketObject (readonly)

Returns the value of attribute websocket.



3
4
5
# File 'lib/brief/server/socket.rb', line 3

def websocket
  @websocket
end

Instance Method Details

#process(message = {}) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/brief/server/socket.rb', line 14

def process(message={})
  message.symbolize_keys!

  action      = message.fetch(:action)
  message_id  = message.fetch(:message_id)

  case

  when action == "brief:info"
    respond_to(message_id) do
      {
        environment: Brief.environment_info,
        root: root.to_s,
        briefcases: briefcases.keys
      }
    end

  when action == "run:command"
    command = message.fetch(:command, 'default')
    options = message.fetch(:options, {})
    briefcase = briefcases.fetch(message[:briefcase])

    respond_to(message_id) do
      briefcase.run_command(command, options)
    end
  when action == "list:briefcases"
    presenter = message.fetch(:presenter, 'default')
    options = message.fetch(:options, {})

    respond_to(message_id) do
      briefcases.values.map do |bc|
        bc.present(presenter, options)
      end
    end

  when action == "view:briefcase"
    presenter = message.fetch(:presenter, 'default')
    options = message.fetch(:options, {})
    briefcase = briefcases.fetch(message[:briefcase])

    respond_to(message_id) do
      briefcase.present(presenter.to_sym, options)
    end

  when action == "view:document"
    options = message.fetch(:options, {})
    path = message.fetch(:path)
    briefcase = briefcases.fetch(message[:briefcase])

    respond_to(message_id) do
      document = briefcase.document_at(path)
      document.to_model.as_json(options)
    end

  when action == "run:command"
    binding.pry
  when action == "query"
    params = message.fetch(:params, {})
    model_options = message.fetch(:model_options, {})
    briefcase = briefcases.fetch(message[:briefcase])

    respond_to(message_id) do
      briefcase.where(params).all.map do |model|
        model.as_json(model_options)
      end
    end

  else
    respond_to(message_id) do
      {error:"Invalid Action: #{ action }"}
    end
  end
rescue
  nil
end

#respond_to(message_id, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/brief/server/socket.rb', line 90

def respond_to(message_id, &block)
  body = block.call()

  payload = {
    action: "response",
    message_id: message_id,
    body: body
  }

  websocket.send(payload.to_json)
end