Module: Hornet
Constant Summary collapse
- EVENTS =
[ "hornet:events:connect", "hornet:events:disconnect" ]
Instance Method Summary collapse
-
#create_access_token(*args) ⇒ Object
tokens are the base62 version of the concatenation (string) of : - unique id on any number of digits.
- #disconnect_tokens(tokens) ⇒ Object
- #publish(*args) ⇒ Object
- #redis ⇒ Object
- #redis_options(redis_option = {}) ⇒ Object
- #subscribe ⇒ Object
- #token_TTL(token_TTL = nil) ⇒ Object
Instance Method Details
#create_access_token(*args) ⇒ Object
tokens are the base62 version of the concatenation (string) of :
-
unique id on any number of digits
-
current timestamp (in seconds) left padded to be on 10 digits. i.e. it means any timestamp will look like : 0001234953. 10 digits are enough to go to Sat Nov 20 18:46:39 +0100 2286 (9999999999)
-
a random number on 5 digits, again, left padded with 0. This last part is just to increase the complexity of the token.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hornet.rb', line 30 def create_access_token( *args ) token_id = redis.incr "hornet:tokens_id" token = (token_id.to_s + generate_token_suffix).to_i.alphadecimal key = "hornet:token:" + token if args[0].is_a? Hash opts = args[0] if opts[:channels] opts[:channels].each do |channel| redis.sadd key, channel end elsif opts[:channel] redis.sadd key, opts[:channel] end else puts '*** DEPRECATED : Please use :channel => "#{args[0]}" instead of "#{args[0]}" ***' redis.sadd key, args[0] end redis.expire key, token_TTL return token end |
#disconnect_tokens(tokens) ⇒ Object
60 61 62 63 64 |
# File 'lib/hornet.rb', line 60 def disconnect_tokens(tokens) disconnectMsg = "token:" + tokens.to_json publish("hornet", "disconnect_tokens", disconnectMsg) end |
#publish(*args) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hornet.rb', line 67 def publish( *args ) if args[0].is_a? Hash opts = args[0] opts[:options] ||= {} if opts[:channels] opts[:channels].each do |channel| redis.publish("hornet:channel:" + channel.to_s, opts[:message].merge( :type => opts[:type], :channel => channel.to_s ).merge(opts[:options]).to_json ) end elsif opts[:channel] redis.publish("hornet:channel:" + opts[:channel].to_s, opts[:message].merge( :type => opts[:type], :channel => opts[:channel].to_s ).merge(opts[:options]).to_json ) end else puts '*** DEPRECATED : Please use :type , :channel(s) :message :options instead of direct args ***' args[3] ||= {} redis.publish("hornet:channel:" + args[0].to_s, args[2].merge( :type => args[1].to_s, :channel => args[0].to_s ).merge( args[3] ).to_json) end end |
#redis ⇒ Object
99 100 101 |
# File 'lib/hornet.rb', line 99 def redis @redis ||= Redis.new() end |
#redis_options(redis_option = {}) ⇒ Object
17 18 19 |
# File 'lib/hornet.rb', line 17 def ( redis_option = {} ) @redis_options ||= redis_option end |
#subscribe ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/hornet.rb', line 90 def subscribe Redis.new(:host => ['redis_host'], :port => ['redis_port']).subscribe(*EVENTS) do |on| on. do |type, msg| yield(type.gsub(/^hornet:events:/, "").to_sym, JSON.parse(msg)) end end end |
#token_TTL(token_TTL = nil) ⇒ Object
11 12 13 14 15 |
# File 'lib/hornet.rb', line 11 def token_TTL( token_TTL = nil ) @token_TTL ||= token_TTL @token_TTL || 120 end |