class Metybur::Method

Public Class Methods

new(name, websocket) click to toggle source
# 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

Public Instance Methods

call(params, &block) click to toggle source
# 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

Private Instance Methods

handle_message(attributes) click to toggle source
# File lib/metybur/method.rb, line 55
def handle_message(attributes)
  id = attributes[:id]
  result = Result.new(attributes, @callbacks[id])
  result.publish
end