2012-08-12 17:47:27 +02:00
|
|
|
require "socket"
|
|
|
|
|
|
|
|
class Countinual
|
|
|
|
cattr_accessor :API_KEY
|
|
|
|
|
|
|
|
# this needs to be overridden in config/initializers/production.rb
|
|
|
|
@@API_KEY = nil
|
|
|
|
|
2012-09-03 10:22:30 +02:00
|
|
|
COUNTINUAL_HOST = "207.158.15.115"
|
2012-08-12 17:47:27 +02:00
|
|
|
COUNTINUAL_PORT = 1025
|
|
|
|
|
|
|
|
def self.count!(counter, value, time = nil)
|
|
|
|
if !@@API_KEY
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if time
|
|
|
|
time = time.to_i
|
|
|
|
else
|
|
|
|
time = Time.now.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
line = "#{@@API_KEY} #{counter} #{value} #{time}\n"
|
|
|
|
|
|
|
|
begin
|
|
|
|
sock = UDPSocket.open
|
|
|
|
sock.send(line, 0, COUNTINUAL_HOST, COUNTINUAL_PORT)
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.info "Countinual error: #{e.message} (#{line.inspect})"
|
2012-08-15 23:45:28 +02:00
|
|
|
ensure
|
|
|
|
sock.close
|
2012-08-12 17:47:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|