Class: Grenache::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/grenache/link.rb

Overview

Implement Grape connection helpers

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Link

Initialize passing configuration



6
7
8
# File 'lib/grenache/link.rb', line 6

def initialize(config)
  @config = config
end

Instance Method Details

#connectObject

Connect to grape



11
12
13
14
15
# File 'lib/grenache/link.rb', line 11

def connect
  unless connected?
    ws_connect
  end
end

#connected?Boolean

Return true if it’s connected to Grape

Returns:

  • (Boolean)


18
19
20
# File 'lib/grenache/link.rb', line 18

def connected?
  @connected
end

#disconnectObject

Disconnect from grape



23
24
25
26
# File 'lib/grenache/link.rb', line 23

def disconnect
  @ws.close
  @ws = nil
end

#send(type, payload, opts = {}, &block) ⇒ Object

Send a message to grape



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

def send(type, payload, opts = {}, &block)
  if http?
    res = http_send type, Oj.dump({"rid" => uuid, "data" => payload})
    block.call(res) if block
    res
  else
    m = GrapeMessage.new(type,payload,opts, &block)
    messages[m.rid] = m
    ws_send m.to_json
  end
end