Class: Fluent::WeatherOutput

Inherits:
CopyOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_weather.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/fluent/plugin/out_weather.rb', line 10

def configure(conf)
  super
  @api_key = conf["api_key"]
  @weather_types = {}
  conf.elements.each { |e| e.name == 'weather_types' }.each do |e|
    e.keys.each { |w_key| @weather_types[w_key] = e[w_key] }
  end
end

#emit(tag, es, chain) ⇒ Object

most of method ‘emit` from fluent/plugin/out_copy



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

def emit(tag, es, chain)
  # insert weather to record
  es.each {|time, record|
    weather = weather(record['latitude'], record['longitude'])
    record["weather"] = weather_types.select { |k, v| /#{v}/i.match(weather) }.keys[0]
  }
  unless es.repeatable?
    m = MultiEventStream.new
    es.each {|time, record| m.add(time, record) }
    es = m
  end
  chain = OutputChain.new(@outputs, tag, es, chain)
  chain.next
end

#weather(lat, lon) ⇒ Object



39
40
41
42
43
# File 'lib/fluent/plugin/out_weather.rb', line 39

def weather(lat, lon)
  if lat && lon
    wunderground.forecast_and_conditions_for("#{lat},#{lon}")['current_observation']['weather'] rescue nil
  end
end

#wundergroundObject



35
36
37
# File 'lib/fluent/plugin/out_weather.rb', line 35

def wunderground
  @wunderground ||= Wunderground.new(api_key)
end