Class: ClientAuth

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key) ⇒ ClientAuth

Returns a new instance of ClientAuth.



3
4
5
# File 'lib/client_auth.rb', line 3

def initialize api_key, secret_key
	@headers = self.class.build_headers api_key, secret_key
end

Class Method Details

.build_headers(api_key, secret_key) ⇒ Object



26
27
28
29
30
# File 'lib/client_auth.rb', line 26

def self.build_headers api_key, secret_key
	time = Time.now.utc.to_i
	hash = Digest::SHA1.hexdigest("#{api_key}#{secret_key}#{time}").downcase
	{ 'api_key' => api_key, 'hash' => hash, 'time' => time.to_s }
end

Instance Method Details

#outgoing(message, callback) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/client_auth.rb', line 7

def outgoing(message, callback)
 
	# Again, leave non-subscribe messages alone
	if message['channel'] != '/meta/subscribe'
		return callback.call(message)
	end

	# Add ext field if it's not present	
	message['ext'] ||= {}

	# Set the tokens
	message['ext']['api_key'] = @headers['api_key']
	message['ext']['hash'] = @headers['hash']
	message['ext']['time'] = @headers['time']

	# Carry on and send the message to the server
	callback.call(message)
end