Class: Fluent::GeoBlipperOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_geoblipper.rb

Instance Method Summary collapse

Constructor Details

#initializeGeoBlipperOutput

Returns a new instance of GeoBlipperOutput.



4
5
6
7
8
# File 'lib/fluent/plugin/out_geoblipper.rb', line 4

def initialize
  super
  require 'geoip'
  require 'pubnub'
end

Instance Method Details

#format(tag, time, record) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/out_geoblipper.rb', line 23

def format(tag, time, record)
  address = record.delete(@ip_key)
  loc = @geodata.city(address)
  extra = {}
  if loc
    record.merge({latitude: loc.latitude, longitude: loc.longitude}).to_json + "\n"
  else
     # Fluent::Engine.emit('debug.livemap', Time.now, {message: "geodata not found for #{address}"})
     ''
  end
end

#startObject



17
18
19
20
21
# File 'lib/fluent/plugin/out_geoblipper.rb', line 17

def start
  super
  @geodata = GeoIP.new(@geodata_location)
  @pubnub = Pubnub.new( publish_key: @pubnub_publish_key, subscribe_key: @pubnub_subscribe_key, logger: Logger.new(STDOUT) )
end

#write(chunk) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin/out_geoblipper.rb', line 35

def write(chunk)
  chunk.open do |io|
    items = io.read.split("\n")
    entries = items.slice(0..@max_entries).map {|item| JSON.parse(item) }
    unless entries.empty?
      @pubnub.publish(http_sync: true, message: entries, channel: @pubnub_channel)
    end
  end
end