Class: Brief::Server::Socket
- Inherits:
-
Object
- Object
- Brief::Server::Socket
- Defined in:
- lib/brief/server/socket.rb
Instance Attribute Summary collapse
-
#briefcases ⇒ Object
readonly
Returns the value of attribute briefcases.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#websocket ⇒ Object
readonly
Returns the value of attribute websocket.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Socket
constructor
A new instance of Socket.
- #process(message = {}) ⇒ Object
- #respond_to(message_id, &block) ⇒ Object
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(={}) @root = Pathname(.fetch(:root)) @websocket = .fetch(:websocket) @briefcases = {}.to_mash = .fetch(:briefcase_options, {}) load_briefcases setup_interface end |
Instance Attribute Details
#briefcases ⇒ Object (readonly)
Returns the value of attribute briefcases.
3 4 5 |
# File 'lib/brief/server/socket.rb', line 3 def briefcases @briefcases end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
3 4 5 |
# File 'lib/brief/server/socket.rb', line 3 def root @root end |
#websocket ⇒ Object (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 |
# File 'lib/brief/server/socket.rb', line 14 def process(={}) .symbolize_keys! action = .fetch(:action) = .fetch(:message_id) case when action == "brief:info" respond_to() do { environment: Brief.environment_info, root: root.to_s, briefcases: briefcases.keys } end when action == "list:briefcases" presenter = .fetch(:presenter, 'default') = .fetch(:options, {}) respond_to() do briefcases.values.map do |bc| bc.present(presenter, ) end end when action == "view:briefcase" presenter = .fetch(:presenter, 'default') = .fetch(:options, {}) briefcase = briefcases.fetch([:briefcase]) respond_to() do briefcase.present(presenter.to_sym, ) end when action == "view:document" = .fetch(:options, {}) path = .fetch(:path) briefcase = briefcases.fetch([:briefcase]) respond_to() do document = briefcase.document_at(path) document.to_model.as_json() end when action == "query" params = .fetch(:params, {}) = .fetch(:model_options, {}) briefcase = briefcases.fetch([:briefcase]) respond_to() do briefcase.where(params).all.map do |model| model.as_json() end end else respond_to() do {error:"Invalid Action: #{ action }"} end end rescue nil end |
#respond_to(message_id, &block) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/brief/server/socket.rb', line 80 def respond_to(, &block) body = block.call() payload = { action: "response", message_id: , body: body } websocket.send(payload.to_json) end |