class Rangefinder::SST

Constants

DEFAULT_ENDPOINT
DEFAULT_PORT
DEFAULT_SCRIPT
VERSION

Attributes

endpoint[RW]
port[RW]
script[RW]
server_id[R]
site_id[RW]
site_key[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/rangefinder/sst.rb, line 22
def initialize(opts = {})
  @site_key = opts[:site_key] || ''
  @site_id  = opts[:site_id]  || 0
  @server_id = 0
  @socket = nil
  
  @script = opts[:script] || DEFAULT_SCRIPT
  @endpoint = opts[:endpoint] || DEFAULT_ENDPOINT
  @port = opts[:port] || DEFAULT_PORT
end
rand() click to toggle source
# File lib/rangefinder/sst.rb, line 16
def rand; @@rand; end
rand=(r) click to toggle source

Change the random server ID generator. eg. Rangefinder::SST.rand = Proc.new { MySpecial.rand }

# File lib/rangefinder/sst.rb, line 19
def rand=(r) @@rand = r; end

Public Instance Methods

code() click to toggle source
# File lib/rangefinder/sst.rb, line 59
    def code
      <<eos
<script type="text/javascript">
  var _rangefinder_queue = _rangefinder_queue || [];
  _rangefinder_queue.push(['track', #{@site_id.to_s}, #{@server_id.to_s}]);
  document.write(unescape("%3Cscript src='//#{@script}' type='text/javascript'%3E%3C/script%3E"));
</script>
eos
    end
connect() click to toggle source
# File lib/rangefinder/sst.rb, line 54
def connect
  @socket = UDPSocket.new
  @socket.connect(@endpoint, @port)
end
track(visit) click to toggle source
# File lib/rangefinder/sst.rb, line 33
def track(visit)
  connect if @socket.nil?
  
  @id = self.class.rand.call
  packet = 'track:'+Addressable::URI.form_encode(
    :key => @site_key,
    :page => visit[:page],
    :referrer => visit[:referrer],
    :user_agent => visit[:user_agent],
    :ipv4 => visit[:ipv4],
    :time => Time.now.utc.to_i,
    :id => @id
  )+"\n"
  
  if packet.length < 1499
    @socket.send packet, 0
  else
    @id = -2 # Too long
  end
  return @id
end