Class: Metybur::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/metybur/method.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, websocket) ⇒ Method

Returns a new instance of Method.



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/metybur/method.rb', line 2

def initialize(name, websocket)
  require 'securerandom'

  @name = name
  @websocket = websocket
  @callbacks = {}

  @websocket.on(:message) do |event|
    attributes = JSON.parse(event.data, symbolize_names: true)
    handle_message(attributes) if attributes[:msg] == 'result'
  end
end

Instance Method Details

#call(params, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/metybur/method.rb', line 15

def call(params, &block)
  id = SecureRandom.uuid
  message = {
    msg: 'method',
    id: id,
    method: @name,
    params: params
  }.to_json
  @websocket.send message
  @callbacks[id] = block
end