Module: Rfid

Extended by:
Rfid
Included in:
Rfid
Defined in:
lib/rfid.rb,
lib/rfid/cli.rb,
lib/rfid/event.rb,
lib/rfid/config.rb,
lib/rfid/version.rb,
lib/rfid/websocket.rb,
lib/rfid/translator.rb,
lib/rfid/application.rb,
lib/rfid/client_message.rb

Defined Under Namespace

Modules: Translator, WebSocket Classes: Application, CLI, ClientMessage, Config, Event

Constant Summary collapse

VERSION =
"0.0.3".freeze

Instance Method Summary collapse

Instance Method Details

#configObject



19
20
21
# File 'lib/rfid.rb', line 19

def config
  @config ||= Config.new(config_path)
end

#config_pathObject



23
24
25
# File 'lib/rfid.rb', line 23

def config_path
  @config_path ||= File.expand_path("../../config/rfid.yml", __FILE__)
end

#initialize!Object



68
69
70
71
# File 'lib/rfid.rb', line 68

def initialize!
  Cocaine::CommandLine.path = ["/opt/bin", "/usr/local/bin", "/usr/bin"]
  Cocaine::CommandLine.logger = logger
end

#keysObject

Returns an array of all known Resque keys in Redis. Redis’ KEYS operation is O(N) for the keyspace, so be careful - this can be slow for big databases.



55
56
57
# File 'lib/rfid.rb', line 55

def keys
  redis.keys("*")
end

#log(*args) ⇒ Object



63
64
65
66
# File 'lib/rfid.rb', line 63

def log(*args)
  timestamp = "[#{Time.now.strftime('%d.%m.%Y %H:%M')}]"
  logger.info([timestamp, args.compact].flatten.join(' '))
end

#loggerObject



59
60
61
# File 'lib/rfid.rb', line 59

def logger
  @logger ||= create_logger(self.config.log_path)
end

#push_event(event) ⇒ Object



48
49
50
51
# File 'lib/rfid.rb', line 48

def push_event(event)
  log("Redis push event", event.inspect)
  redis.rpush(self.config.redis_key, event.to_json)
end

#redisObject



27
28
29
# File 'lib/rfid.rb', line 27

def redis
  @redis ||= Redis.new(self.redis_options)
end

#redis_idObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/rfid.rb', line 37

def redis_id
  # support 1.x versions of redis-rb
  if redis.respond_to?(:server)
    redis.server
  elsif redis.respond_to?(:nodes) # distributed
    redis.nodes.map { |n| n.id }.join(', ')
  else
    redis.client.id
  end
end

#redis_optionsObject



31
32
33
34
35
# File 'lib/rfid.rb', line 31

def redis_options
  url = URI(self.config.redis_url)
  db = (url.path[1..-1] || 10).to_i
  { :host => url.host, :port => url.port, :db => db, :password => url.password }
end