class Stormpack::Pusher::Client

Attributes

host[RW]
port[RW]

Public Class Methods

new(host: 'localhost', port: 1234) click to toggle source
# File lib/stormpack/pusher/client.rb, line 10
def initialize(host: 'localhost', port: 1234)
  self.host = host
  self.port = port
end

Public Instance Methods

publish(data, tags = ['all'], wait=false) click to toggle source
# File lib/stormpack/pusher/client.rb, line 22
def publish(data, tags = ['all'], wait=false)
  submit "publish #{tags.join(',')} #{data}", wait
end
register(token, tags = [], wait=false) click to toggle source
# File lib/stormpack/pusher/client.rb, line 26
def register(token, tags = [], wait=false)
  submit "register #{tags.join(',')} #{token}", wait
end
register_http(url: self.host, auth_token: 'ultravisor.core', filters: [], secret_token: '1234', destination: 'http://google.com') click to toggle source
# File lib/stormpack/pusher/client.rb, line 15
def register_http(url: self.host, auth_token: 'ultravisor.core', filters: [], secret_token: '1234', destination: 'http://google.com')
  conn = Faraday.new(:url => url)
  conn.post("subscribe/callback?token=#{auth_token}") do |req|
    req.body = { filters: filters, token: secret_token, callback: destination}.to_json
  end
end
set(token, tags = [], wait = false) click to toggle source
# File lib/stormpack/pusher/client.rb, line 34
def set(token, tags = [], wait = false)
  submit "set #{tags.join(',')} #{token}", wait
end
unregister(token, wait=false) click to toggle source
# File lib/stormpack/pusher/client.rb, line 30
def unregister(token, wait=false)
  submit "unregister #{token}", wait
end
unset(token, tags = [], wait = false) click to toggle source
# File lib/stormpack/pusher/client.rb, line 38
def unset(token, tags = [], wait = false)
  submit "unset #{tags.join(',')} #{token}", wait
end

Private Instance Methods

submit(command, wait) click to toggle source
# File lib/stormpack/pusher/client.rb, line 44
def submit(command, wait)
  command = "ensure #{command}" if wait

  with_client do |cli|
    cli.puts("#{command}\n")

    if wait
      cli.gets
    else
      ''
    end

  end
end
with_client() { |current| ... } click to toggle source
# File lib/stormpack/pusher/client.rb, line 59
def with_client
  Thread.current[:sp_client] ||= TCPSocket.new(host, port)
  if Thread.current[:sp_client].closed?
    Thread.current[:sp_client] = TCPSocket.new(host, port)
  end
  res = yield Thread.current[:sp_client]
  res
rescue
  Thread.current[:sp_client] = TCPSocket.new(host, port)
  res = yield Thread.current[:sp_client]
  res
end