fluent-plugin-weather 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ /vendor/bundles
2
+ /.bundle
3
+ /Gemfile.lock
4
+ /pkg
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - ruby-head
5
+
6
+ script: bundle exec rake test
@@ -0,0 +1,2 @@
1
+ 0.0.1 - 2012/10/23
2
+ This initial plugin is released.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'mocha', require: false
7
+ end
8
+ group :development, :test do
9
+ gem 'pry', require: false
10
+ end
@@ -0,0 +1,31 @@
1
+ = Weather output plugin for Fluent
2
+
3
+ fluent-plugin-weather is a fluent plugin to copy output with adding weather field.
4
+ Without 'latitude' and 'longitude' fields, this add null for weather fields.
5
+
6
+ == Installation
7
+
8
+ What you have to do is only installing like this:
9
+
10
+ gem install fluent-plugin-weather
11
+
12
+ Then fluent automatically loads the plugin installed.
13
+
14
+ == Configuratin
15
+
16
+ <match weather.**>
17
+ type weather
18
+ api_key #YOUR_WUNDERGROUND_API_KEY
19
+ <weather_types> # write regexp by type(ignore case)
20
+ 1 sunny|clear
21
+ 2 cloud|overcast|haze|fog
22
+ 3 rain|sleet|snow|storms
23
+ </weather_types>
24
+
25
+ <store>
26
+ # ...
27
+ </store>
28
+ <store>
29
+ # ...
30
+ </store>
31
+ </match>
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.test_files = FileList['test/plugin/*.rb']
9
+ test.verbose = true
10
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "fluent-plugin-weather"
6
+ gem.description = "Weather output plugin for Fluent"
7
+ gem.homepage = "https://github.com/ToQoz/fluent-plugin-weather"
8
+ gem.summary = "fluent-plugin-weather"
9
+ gem.version = File.read("VERSION").strip
10
+ gem.authors = ["Takatoshi Matsumoto"]
11
+ gem.email = "toqoz403@gmail.com"
12
+ gem.has_rdoc = false
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ gem.require_paths = ['lib']
17
+
18
+ gem.add_dependency "fluentd", "~> 0.10.7"
19
+ gem.add_dependency "wunderground"
20
+ gem.add_development_dependency "rake", ">= 0.9.2"
21
+ end
@@ -0,0 +1,45 @@
1
+ require 'fluent/plugin/out_copy'
2
+ require 'wunderground'
3
+
4
+ module Fluent
5
+ class WeatherOutput < CopyOutput
6
+ Plugin.register_output('weather', self)
7
+ config_param :api_key, :string
8
+ config_param :weather_types, :string
9
+
10
+ def configure(conf)
11
+ super
12
+ @api_key = conf["api_key"]
13
+ @weather_types = {}
14
+ conf.elements.each { |e| e.name == 'weather_types' }.each do |e|
15
+ e.keys.each { |w_key| @weather_types[w_key] = e[w_key] }
16
+ end
17
+ end
18
+
19
+ # most of method `emit` from fluent/plugin/out_copy
20
+ def emit(tag, es, chain)
21
+ # insert weather to record
22
+ es.each {|time, record|
23
+ weather = weather(record['latitude'], record['longitude'])
24
+ record["weather"] = weather_types.select { |k, v| /#{v}/i.match(weather) }.keys[0]
25
+ }
26
+ unless es.repeatable?
27
+ m = MultiEventStream.new
28
+ es.each {|time, record| m.add(time, record) }
29
+ es = m
30
+ end
31
+ chain = OutputChain.new(@outputs, tag, es, chain)
32
+ chain.next
33
+ end
34
+
35
+ def wunderground
36
+ @wunderground ||= Wunderground.new(api_key)
37
+ end
38
+
39
+ def weather(lat, lon)
40
+ if lat && lon
41
+ wunderground.forecast_and_conditions_for("#{lat},#{lon}")['current_observation']['weather'] rescue nil
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,67 @@
1
+ require 'test_helper'
2
+ require 'fluent/plugin/out_weather'
3
+
4
+ class WeatherOutputTest < Test::Unit::TestCase
5
+ def setup
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ # http://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary
10
+ CONFIG = %[
11
+ api_key foo
12
+ <weather_types>
13
+ 1 sunny|clear
14
+ 2 cloud|overcast|haze|fog
15
+ 3 rain|sleet|snow|storms
16
+ </weather_types>
17
+ <store>
18
+ type test
19
+ name c0
20
+ </store>
21
+ <store>
22
+ type test
23
+ name c1
24
+ </store>
25
+ <store>
26
+ type test
27
+ name c2
28
+ </store>
29
+ ]
30
+
31
+ def create_driver(conf = CONFIG)
32
+ Fluent::Test::OutputTestDriver.new(Fluent::WeatherOutput).configure(conf)
33
+ end
34
+
35
+ def test_configure
36
+ d = create_driver
37
+ assert_equal("foo", d.instance.api_key)
38
+ assert_equal("sunny|clear", d.instance.weather_types["1"])
39
+ assert_equal("cloud|overcast|haze|fog", d.instance.weather_types["2"])
40
+ assert_equal("rain|sleet|snow|storms", d.instance.weather_types["3"])
41
+ end
42
+
43
+ def test_emit
44
+ d = create_driver
45
+ expecting = [
46
+ { latlon: [ 35, 135 ], emit: { "a" => 1, "latitude" => 35, "longitude" => 135 }, weather: "Mostly Sunny", weather_types: "1" },
47
+ { latlon: [ 30, 135 ], emit: { "a" => 2, "latitude" => 30, "longitude" => 135 }, weather: "Cloudy", weather_types: "2" },
48
+ { latlon: [ 30, 135 ], emit: { "a" => 2, "latitude" => 30, "longitude" => 135 }, weather: "Sunny", weather_types: "1" },
49
+ { latlon: [ 30, 135 ], emit: { "a" => 2, "latitude" => 30, "longitude" => 135 }, weather: "Rain", weather_types: "3" },
50
+ { latlon: [ 30, 135 ], emit: { "a" => 2, "latitude" => 30, "longitude" => 135 }, weather: "UnkownWeather", weather_types: nil },
51
+ { latlon: [ 30, 135 ], emit: { "a" => 2, "latitude" => 30, "longitude" => 135 }, weather: "Partly Cloudy", weather_types: "2" }
52
+ ].map do |e|
53
+ Fluent::WeatherOutput.any_instance.
54
+ stubs(:weather).
55
+ with(*e[:latlon]).
56
+ returns(e[:weather])
57
+ time = Time.parse("2011-01-02 13:14:15 UTC").to_i
58
+ d.emit(e[:emit], time)
59
+
60
+ [ time, e[:emit].merge("weather" => e[:weather_types]) ]
61
+ end
62
+
63
+ d.instance.outputs.each {|o|
64
+ assert_equal(expecting, o.events)
65
+ }
66
+ end
67
+ end
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require 'fluent/test'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-weather
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Takatoshi Matsumoto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fluentd
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.10.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.10.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: wunderground
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.2
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2
62
+ description: Weather output plugin for Fluent
63
+ email: toqoz403@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - .travis.yml
70
+ - Changelog
71
+ - Gemfile
72
+ - README.rdoc
73
+ - Rakefile
74
+ - VERSION
75
+ - fluent-plugin-weather.gemspec
76
+ - lib/fluent/plugin/out_weather.rb
77
+ - test/plugin/out_weather.rb
78
+ - test/test_helper.rb
79
+ homepage: https://github.com/ToQoz/fluent-plugin-weather
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.23
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: fluent-plugin-weather
103
+ test_files:
104
+ - test/plugin/out_weather.rb
105
+ - test/test_helper.rb