Class: Metybur::Method

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

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(name, websocket) ⇒ Method

Returns a new instance of Method.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/metybur/method.rb', line 28

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/metybur/method.rb', line 41

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