fluent-plugin-jabber 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1 +1,2 @@
1
+ source :rubygems
1
2
  gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 todesking
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -29,6 +29,10 @@ See source for details.
29
29
  # Need 'jid' and 'password' field.
30
30
  pit_id jabber
31
31
 
32
+ # Or, put JID/password directly(exclusive for pit_id)
33
+ jid name@example.com
34
+ password pa55w0rd
35
+
32
36
  # Output target JID. Currently multi user chat only.
33
37
  room test@conference.localhost/test
34
38
  </match>
@@ -49,6 +53,15 @@ If 'body' field not set, the plugin raises error.
49
53
 
50
54
  ## Changes
51
55
 
56
+ ### 0.1.0
57
+
58
+ * Add license
59
+ * Updated Gemfile
60
+ * Change file location to fluent plugin standard.
61
+ * Add format option
62
+ * Add test
63
+ * Add jid/password option
64
+
52
65
  ### 0.0.1
53
66
 
54
- Initial release.
67
+ * Initial release.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "fluent-plugin-jabber"
3
- gem.version = "0.0.1"
3
+ gem.version = "0.1.0"
4
4
  gem.authors = ["todesking"]
5
5
  gem.email = ["discommunicative@gmail.com"]
6
6
  gem.summary = %q{Fluentd output plugin for XMPP(Jabber) protocol}
@@ -11,8 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
12
  gem.require_paths = ["lib"]
13
13
 
14
- gem.add_development_dependency "rake"
15
- gem.add_development_dependency "fluentd"
14
+ gem.add_development_dependency "rspec"
16
15
 
17
16
  gem.add_runtime_dependency "xmpp4r"
18
17
  gem.add_runtime_dependency "fluentd"
@@ -0,0 +1,70 @@
1
+ require 'fluent/plugin'
2
+ require 'fluent/config'
3
+ require 'fluent/output'
4
+
5
+ require 'xmpp4r'
6
+ require 'xmpp4r/muc'
7
+
8
+ require 'pit'
9
+
10
+ class Fluent::JabberOutput < Fluent::Output
11
+ Fluent::Plugin.register_output('jabber', self)
12
+
13
+ config_param :pit_id, :string, default: nil
14
+ config_param :jid, :string, default: nil
15
+ config_param :password, :string, default: nil
16
+
17
+ # Currently, output target is group chat only.
18
+ config_param :room, :string
19
+
20
+ config_param :format, :string
21
+
22
+ attr_reader :jid
23
+ attr_reader :password
24
+
25
+ def configure(conf)
26
+ super
27
+
28
+ raise Fluent::ConfigError, "jid/password and pit_id is exclusive!!" if (@jid || @password) && @pit_id
29
+
30
+ if @pit_id
31
+ user_info = Pit.get(@pit_id, require: {
32
+ 'jid' => 'jid',
33
+ 'password' => 'password',
34
+ })
35
+ @jid = user_info['jid']
36
+ @password = user_info['password']
37
+ end
38
+ end
39
+
40
+ def start
41
+ jid = Jabber::JID.new(@jid)
42
+ @client = Jabber::Client.new(jid)
43
+ @client.connect
44
+ @client.auth(@password)
45
+
46
+ @muc_client = Jabber::MUC::MUCClient.new(@client)
47
+ @muc_client.join(@room)
48
+
49
+ $log.info("out_jabber plugin initialized(jid: #{self.jid}, room: #{self.room})")
50
+ end
51
+
52
+ def shutdown
53
+ @client.close
54
+ end
55
+
56
+ def emit(tag, es, chain)
57
+ es.each do|time, record|
58
+ send_message create_message(time, record)
59
+ end
60
+ chain.next
61
+ end
62
+
63
+ def create_message(time, record)
64
+ message = @format.gsub(/\\n/, "\n").gsub(/\${([\w.]+)}/) { $1.split('.').inject(record) {|r,k| (r||{})[k]} }
65
+ end
66
+
67
+ def send_message(message)
68
+ @muc_client.send Jabber::Message.new(@room, message)
69
+ end
70
+ end
@@ -0,0 +1,117 @@
1
+ require 'fluent/log'
2
+ $log = Fluent::Log.new(STDERR, Fluent::Log::LEVEL_FATAL)
3
+
4
+ require 'fluent/plugin/out_jabber'
5
+
6
+ def create_fluent_config(hash, root = Fluent::Config.new)
7
+ hash.each do|k,v|
8
+ case v
9
+ when String
10
+ root[k.to_s] = v
11
+ when Hash
12
+ child = root.add_element(k.to_s, v[:__arg__] || nil)
13
+ create_fluent_config(v.dup.delete(:__arg__), child)
14
+ else
15
+ raise "Invalid value: #{v.inspect}"
16
+ end
17
+ end
18
+ root
19
+ end
20
+
21
+ describe Fluent::JabberOutput do
22
+ let(:default_config) {
23
+ {
24
+ pit_id: 'jabber',
25
+ room: 'the_room@conference.example.com',
26
+ format: 'message: ${message}',
27
+ }
28
+ }
29
+
30
+ let(:chain) { double('chain') }
31
+
32
+ context 'configuring(with pit)' do
33
+ before :each do
34
+ config = create_fluent_config(default_config)
35
+
36
+ Pit.stub(:get).with('jabber', anything).and_return('jid' => 'jabber@example.com', 'password' => 'pa55w0rd')
37
+
38
+ subject.configure(config)
39
+ end
40
+
41
+ its(:jid) { should == 'jabber@example.com' }
42
+ its(:password) { should == 'pa55w0rd' }
43
+ its(:room) { should == 'the_room@conference.example.com' }
44
+ end
45
+
46
+ context 'configureing(with jid/password)' do
47
+ before :each do
48
+ config_hash = default_config.merge(jid: 'jabber@example.com', password: 'pa55w0rd')
49
+ config_hash.delete :pit_id
50
+ config = create_fluent_config(config_hash)
51
+
52
+ subject.configure(config)
53
+ end
54
+
55
+ its(:jid) { should == 'jabber@example.com' }
56
+ its(:password) { should == 'pa55w0rd' }
57
+ its(:room) { should == 'the_room@conference.example.com' }
58
+ end
59
+
60
+ context 'config contains jid and pit_id' do
61
+ it 'should error' do
62
+ config_hash = default_config.merge(jid: 'jabber@example.com', password: 'pa55w0rd')
63
+ config = create_fluent_config(config_hash)
64
+
65
+ expect { subject.configure config }.to raise_error Fluent::ConfigError
66
+ end
67
+ end
68
+
69
+ context 'connecting' do
70
+ it 'should connect with configured parameters' do
71
+ Pit.stub(:get).with('jabber', anything).and_return('jid' => 'jabber@example.com', 'password' => 'pa55w0rd')
72
+
73
+ jabber_client = double('jabber_client')
74
+ muc_client = double('muc_client')
75
+
76
+ Jabber::Client.stub(:new).with(Jabber::JID.new('jabber@example.com')).ordered.and_return(jabber_client)
77
+ jabber_client.should_receive(:connect).ordered
78
+ jabber_client.should_receive(:auth).with('pa55w0rd').ordered
79
+
80
+ Jabber::MUC::MUCClient.stub(:new).with(jabber_client).ordered.and_return(muc_client)
81
+ muc_client.should_receive(:join).with('the_room@conference.example.com')
82
+
83
+ subject.configure create_fluent_config(default_config)
84
+ subject.start
85
+ end
86
+ end
87
+
88
+ context 'emitting' do
89
+ let(:format) { 'hello!' }
90
+ before :each do
91
+ config = create_fluent_config(default_config.merge format: format)
92
+
93
+ Pit.stub(:get).with('jabber', anything).and_return('jid' => 'jabber@example.com', 'password' => 'pa55w0rd')
94
+
95
+ subject.configure(config)
96
+ end
97
+
98
+ it 'should send message to jabber conference room' do
99
+ subject.should_receive(:send_message).with('hello!').twice
100
+ chain.should_receive(:next).once
101
+
102
+ subject.emit('tag', [[0, {}], [1, {'a'=>'b'}]], chain)
103
+ end
104
+
105
+ context :format do
106
+ let(:format) { 'a=${a}, x.y=${x.y}' }
107
+
108
+ it 'should format message with received record' do
109
+ subject.should_receive(:send_message).with('a=1, x.y=2').ordered
110
+ subject.should_receive(:send_message).with('a=1, x.y=').ordered
111
+ chain.should_receive(:next).once
112
+
113
+ subject.emit('tag', [[0, {'a'=>1, 'x'=>{'y'=>2}}], [1, {'a'=>1}]], chain)
114
+ end
115
+ end
116
+ end
117
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-jabber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,26 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-15 00:00:00.000000000 Z
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rake
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: fluentd
15
+ name: rspec
32
16
  requirement: !ruby/object:Gem::Requirement
33
17
  none: false
34
18
  requirements:
@@ -100,9 +84,11 @@ extra_rdoc_files: []
100
84
  files:
101
85
  - .gitignore
102
86
  - Gemfile
87
+ - LICENSE
103
88
  - README.md
104
89
  - fluent-plugin-jabber.gemspec
105
- - lib/fluent/plugin/jabber.rb
90
+ - lib/fluent/plugin/out_jabber.rb
91
+ - spec/out_jabber_spec.rb
106
92
  homepage: https://github.com/todesking/fluent-plugin-jabber
107
93
  licenses: []
108
94
  post_install_message:
@@ -127,4 +113,5 @@ rubygems_version: 1.8.24
127
113
  signing_key:
128
114
  specification_version: 3
129
115
  summary: Fluentd output plugin for XMPP(Jabber) protocol
130
- test_files: []
116
+ test_files:
117
+ - spec/out_jabber_spec.rb
@@ -1,54 +0,0 @@
1
- require 'fluent/plugin'
2
- require 'fluent/config'
3
- require 'fluent/output'
4
-
5
- require 'xmpp4r'
6
- require 'xmpp4r/muc'
7
-
8
- require 'pit'
9
-
10
- class Fluent::JabberOutput < Fluent::Output
11
- Fluent::Plugin.register_output('jabber', self)
12
-
13
- config_param :pit_id, :string
14
-
15
- # Currently, output target is group chat only.
16
- config_param :room, :string
17
-
18
- def configure(conf)
19
- super
20
- end
21
-
22
- def start
23
- user_info = Pit.get(@pit_id, require: {
24
- 'jid' => 'jid',
25
- 'password' => 'password',
26
- })
27
-
28
- jid = Jabber::JID.new(user_info['jid'])
29
- @client = Jabber::Client.new(jid)
30
- @client.connect
31
- @client.auth(user_info['password'])
32
-
33
- @muc_client = Jabber::MUC::MUCClient.new(@client)
34
- @muc_client.join(@room)
35
-
36
- $log.info("out_jabber plugin initialized(jid: #{user_info['jid']}, room: #{@room})")
37
- end
38
-
39
- def shutdown
40
- @client.close
41
- end
42
-
43
- def emit(tag, es, chain)
44
- es.each do|time, record|
45
- @muc_client.send create_message(time, record)
46
- end
47
- chain.next
48
- end
49
-
50
- def create_message(time, record)
51
- raise "record['body'] is empty" unless record['body']
52
- Jabber::Message.new(@room, record['body'])
53
- end
54
- end