class Centrifuge::Client

Constants

DEFAULT_OPTIONS

Attributes

connect_timeout[W]
host[RW]
keep_alive_timeout[W]
port[RW]
receive_timeout[W]
scheme[RW]
secret[RW]
send_timeout[W]

Public Class Methods

new(options = {}) click to toggle source

CONFIGURATION ##

# File lib/centrifuge/client.rb, line 18
def initialize(options = {})
  options = DEFAULT_OPTIONS.merge(options)
  @scheme, @host, @port, @secret = options.values_at(
   :scheme, :host, :port, :secret
  )
  set_default_timeouts
end

Public Instance Methods

broadcast(channels, data) click to toggle source
# File lib/centrifuge/client.rb, line 42
def broadcast(channels, data)
  Centrifuge::Builder.new('broadcast', { channels: channels, data: data }, self).process
end
channels() click to toggle source
# File lib/centrifuge/client.rb, line 66
def channels()
  Centrifuge::Builder.new('channels', {}, self).process
end
client() click to toggle source
# File lib/centrifuge/client.rb, line 87
def client
  @client ||= begin
    HTTPClient.new.tap do |http|
      http.connect_timeout = @connect_timeout
      http.send_timeout = @send_timeout
      http.receive_timeout = @receive_timeout
      http.keep_alive_timeout = @keep_alive_timeout
    end
  end
end
disconnect(user) click to toggle source
# File lib/centrifuge/client.rb, line 54
def disconnect(user)
  Centrifuge::Builder.new('disconnect', { user: user }, self).process
end
generate_channel_sign(client, channel, user_info="") click to toggle source
# File lib/centrifuge/client.rb, line 78
def generate_channel_sign(client, channel, user_info="")
  data = "#{client}#{channel}#{user_info}"
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, data)
end
history(channel) click to toggle source
# File lib/centrifuge/client.rb, line 62
def history(channel)
  Centrifuge::Builder.new('history', { channel: channel }, self).process
end
presence(channel) click to toggle source
# File lib/centrifuge/client.rb, line 58
def presence(channel)
  Centrifuge::Builder.new('presence', { channel: channel }, self).process
end
publish(channel, data) click to toggle source
# File lib/centrifuge/client.rb, line 46
def publish(channel, data)
  Centrifuge::Builder.new('publish', { channel: channel, data: data }, self).process
end
set_default_timeouts() click to toggle source
# File lib/centrifuge/client.rb, line 26
def set_default_timeouts
  @connect_timeout = 5
  @send_timeout = 5
  @receive_timeout = 5
  @keep_alive_timeout = 30
end
sign(body) click to toggle source
# File lib/centrifuge/client.rb, line 83
def sign(body)
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body)
end
stats() click to toggle source
# File lib/centrifuge/client.rb, line 70
def stats()
  Centrifuge::Builder.new('stats', {}, self).process
end
token_for(user, timestamp, user_info = "") click to toggle source
# File lib/centrifuge/client.rb, line 74
def token_for(user, timestamp, user_info = "")
  sign("#{user}#{timestamp}#{user_info}")
end
unsubscribe(channel, user) click to toggle source
# File lib/centrifuge/client.rb, line 50
def unsubscribe(channel, user)
  Centrifuge::Builder.new('unsubscribe', { channel: channel, user: user }, self).process
end
url(path = nil) click to toggle source
# File lib/centrifuge/client.rb, line 33
def url(path = nil)
  URI::Generic.build({
    scheme: scheme.to_s,
    host: host,
    port: port,
    path: "/api/#{path}"
  })
end