Class: Centrifuge::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/centrifuge/client.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  scheme: 'http',
  host: 'localhost',
  port: 8000,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

CONFIGURATION ##



18
19
20
21
22
23
24
# 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

Instance Attribute Details

#connect_timeout=(value) ⇒ Object (writeonly)

Sets the attribute connect_timeout

Parameters:

  • value

    the value to set the attribute connect_timeout to.



13
14
15
# File 'lib/centrifuge/client.rb', line 13

def connect_timeout=(value)
  @connect_timeout = value
end

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/centrifuge/client.rb', line 12

def host
  @host
end

#keep_alive_timeout=(value) ⇒ Object (writeonly)

Sets the attribute keep_alive_timeout

Parameters:

  • value

    the value to set the attribute keep_alive_timeout to.



13
14
15
# File 'lib/centrifuge/client.rb', line 13

def keep_alive_timeout=(value)
  @keep_alive_timeout = value
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/centrifuge/client.rb', line 12

def port
  @port
end

#receive_timeout=(value) ⇒ Object (writeonly)

Sets the attribute receive_timeout

Parameters:

  • value

    the value to set the attribute receive_timeout to.



13
14
15
# File 'lib/centrifuge/client.rb', line 13

def receive_timeout=(value)
  @receive_timeout = value
end

#schemeObject

Returns the value of attribute scheme.



12
13
14
# File 'lib/centrifuge/client.rb', line 12

def scheme
  @scheme
end

#secretObject

Returns the value of attribute secret.



12
13
14
# File 'lib/centrifuge/client.rb', line 12

def secret
  @secret
end

#send_timeout=(value) ⇒ Object (writeonly)

Sets the attribute send_timeout

Parameters:

  • value

    the value to set the attribute send_timeout to.



13
14
15
# File 'lib/centrifuge/client.rb', line 13

def send_timeout=(value)
  @send_timeout = value
end

Instance Method Details

#channelsObject



62
63
64
# File 'lib/centrifuge/client.rb', line 62

def channels()
  Centrifuge::Builder.new('channels', {}, self).process
end

#clientObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/centrifuge/client.rb', line 83

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) ⇒ Object



50
51
52
# File 'lib/centrifuge/client.rb', line 50

def disconnect(user)
  Centrifuge::Builder.new('disconnect', { user: user }, self).process
end

#generate_channel_sign(client, channel, user_info = "") ⇒ Object



74
75
76
77
# File 'lib/centrifuge/client.rb', line 74

def generate_channel_sign(client, channel, ="")
  data = "#{client}#{channel}#{}"
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, data)
end

#history(channel) ⇒ Object



58
59
60
# File 'lib/centrifuge/client.rb', line 58

def history(channel)
  Centrifuge::Builder.new('history', { channel: channel }, self).process
end

#presence(channel) ⇒ Object



54
55
56
# File 'lib/centrifuge/client.rb', line 54

def presence(channel)
  Centrifuge::Builder.new('presence', { channel: channel }, self).process
end

#publish(channel, data) ⇒ Object



42
43
44
# File 'lib/centrifuge/client.rb', line 42

def publish(channel, data)
  Centrifuge::Builder.new('publish', { channel: channel, data: data }, self).process
end

#set_default_timeoutsObject



26
27
28
29
30
31
# 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) ⇒ Object



79
80
81
# File 'lib/centrifuge/client.rb', line 79

def sign(body)
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, secret, body)
end

#statsObject



66
67
68
# File 'lib/centrifuge/client.rb', line 66

def stats()
  Centrifuge::Builder.new('stats', {}, self).process
end

#token_for(user, timestamp, user_info = "") ⇒ Object



70
71
72
# File 'lib/centrifuge/client.rb', line 70

def token_for(user, timestamp,  = "")
  sign("#{user}#{timestamp}#{}")
end

#unsubscribe(channel, user) ⇒ Object



46
47
48
# File 'lib/centrifuge/client.rb', line 46

def unsubscribe(channel, user)
  Centrifuge::Builder.new('unsubscribe', { channel: channel, user: user }, self).process
end

#url(path = nil) ⇒ Object



33
34
35
36
37
38
39
40
# 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