hato 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c911cd9411f589c2ca7db3e534ed77b3536942b8
4
- data.tar.gz: 7957d0bf047be3f829a98a9a8ff637088623586a
3
+ metadata.gz: 10fe52f50d0d3e9135999a5f4c52a2b7d419266f
4
+ data.tar.gz: 82c0ad38232cd128e4e3a834ee1f310fd2f66dcd
5
5
  SHA512:
6
- metadata.gz: eba45a0d953bd29dc0d3a3bca7195b48f5ed45a1b3a4a885a67c569d6827dfb3a586a06550d6673a9f9e7477d8059f09fe30eeb3b93bbaaa298497f97ea44baf
7
- data.tar.gz: f31c44905201b10fc991e91745c6c1cbf734bd0ba8023342ea1b73c218abfbb07a312632c27415e6eee7635375a8d885cf5df78aada0b6e3c2d00caafee0743c
6
+ metadata.gz: b6b226505e06bcea7aa9c77e1e7e4f64d21ad562db087c898667b3e41bc9c4fb0575fdf19326d03e74924606e70853ca3f1ea62e25030366d99c5657a9c50929
7
+ data.tar.gz: d9cec8a303fa12785af2c13a62199c30eae69e0792810edd03620870201a1474b4c38a1b1ce7ac89ff765e7cfa4c1ade0bd02b1683bc0efd884d30efb0532dd5
data/README.md CHANGED
@@ -28,38 +28,20 @@ Hato::Config.define do
28
28
  host '0.0.0.0'
29
29
  port 9699
30
30
 
31
+ # test by exact string mathing
31
32
  tag 'test' do
32
- plugin 'Ikachan' do
33
- scheme 'http'
34
- host 'irc.example.com'
35
- port 4979
36
- channel 'hato'
33
+ plugin 'Plugin1' do
34
+ key1 'value1'
35
+ key2 'value2'
36
+ key3 'value3'
37
37
  end
38
+ end
38
39
 
39
- plugin 'Mail' do
40
- smtp address: 'smtp.example.com',
41
- port: 587,
42
- domain: 'example',
43
- user_name: 'hato',
44
- password: 'password',
45
- enable_ssl: true
46
-
47
- subject_template = <<EOS
48
- [<%= args[:tag] %>] Notification
49
- EOS
50
- body_template = <<EOS
51
- You've got a message:
52
-
53
- <%= args[:message] %>
54
- EOS
55
-
56
- message from: 'hato@example.com',
57
- to: [
58
- 'foo@example.com',
59
- 'bar@example.com',
60
- ],
61
- subject_template: subject_template,
62
- body_template: body_template
40
+ # test by regexp matching
41
+ tag /^test2\.([^\.]+)\.([^\.]+)$/ do |matched1, matched2|
42
+ plugin 'Plugin2' do
43
+ key1 matched1
44
+ key2 matched2
63
45
  end
64
46
  end
65
47
  end
@@ -67,10 +49,11 @@ end
67
49
 
68
50
  ## Plugin Architecture
69
51
 
70
- There have alread been some plugins:
52
+ There have already been some plugins:
71
53
 
72
54
  * [Hato::Plugin::Ikachan](https://github.com/kentaro/hato-plugin-ikachan)
73
55
  * [Hato::Plugin::Mail](https://github.com/kentaro/hato-plugin-mail)
56
+ * [Hato::Plugin::Hipchat](https://github.com/banyan/hato-plugin-hipchat)
74
57
 
75
58
  You can easily extend Hato by creating your own plugins. See the source for detail. It's really easy.
76
59
 
data/hato.gemspec CHANGED
@@ -29,5 +29,6 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "rake"
30
30
  spec.add_development_dependency "rspec"
31
31
  spec.add_development_dependency "pry"
32
+ spec.add_development_dependency "rack-test"
32
33
  end
33
34
 
data/lib/hato/config.rb CHANGED
@@ -8,7 +8,7 @@ module Hato
8
8
 
9
9
  def initialize(name, &block)
10
10
  @name = name
11
- instance_eval(&block)
11
+ @block = block
12
12
  end
13
13
 
14
14
  def method_missing(method, *args)
@@ -19,6 +19,15 @@ module Hato
19
19
  end
20
20
  end
21
21
 
22
+ def activate!(args = nil)
23
+ if !args || args.empty?
24
+ instance_eval(&@block)
25
+ else
26
+ instance_exec(*args, &@block)
27
+ end
28
+ self
29
+ end
30
+
22
31
  private
23
32
 
24
33
  def config
@@ -51,7 +60,14 @@ module Hato
51
60
  end
52
61
 
53
62
  def self.match(tag)
54
- tags.select { |t| t.name == tag }
63
+ tags.inject([]) do |buf, t|
64
+ if t.name.kind_of?(String) && t.name == tag
65
+ buf << [t]
66
+ elsif t.name.kind_of?(Regexp) && (match = t.name.match(tag))
67
+ buf << [t, match[1..-1]]
68
+ end
69
+ buf
70
+ end
55
71
  end
56
72
 
57
73
  def self.reset
@@ -63,12 +79,22 @@ module Hato
63
79
  class Tag
64
80
  include DSL
65
81
 
82
+ def initialize(name, &block)
83
+ @name = name
84
+ @block = block
85
+ end
86
+
87
+ def activate!(args = nil)
88
+ @plugins = []
89
+ super(args)
90
+ end
91
+
66
92
  def plugins
67
93
  @plugins ||= []
68
94
  end
69
95
 
70
96
  def plugin(name, &block)
71
- self.plugins << Plugin.new(name, &block)
97
+ self.plugins << Plugin.new(name, &block).activate!
72
98
  end
73
99
  end
74
100
 
data/lib/hato/httpd.rb CHANGED
@@ -23,7 +23,7 @@ module Hato
23
23
  class App < Sinatra::Base
24
24
  enable :logging
25
25
 
26
- before '/.+' do
26
+ before /\/.+/ do
27
27
  if settings.api_key && (settings.api_key != params[:api_key])
28
28
  halt 403, JSON.dump(
29
29
  status: :error,
data/lib/hato/observer.rb CHANGED
@@ -26,8 +26,9 @@ module Hato
26
26
  def load_plugins_for(tag)
27
27
  plugins = []
28
28
 
29
- @config.match(tag).each do |matched|
30
- matched.plugins.each do |plugin|
29
+ @config.match(tag).each do |matched_tag, args|
30
+ matched_tag.activate!(args)
31
+ matched_tag.plugins.each do |plugin|
31
32
  plugin_file_name = file_name_for(plugin.name)
32
33
  require plugin_file_name
33
34
 
data/lib/hato/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hato
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,40 @@
1
+ Hato::Config.define do
2
+ api_key 'test'
3
+ host '0.0.0.0'
4
+ port 9699
5
+
6
+ tag /(.+) and (.+)/ do |match|
7
+ plugin 'Ikachan' do
8
+ scheme 'http'
9
+ host 'irc.example.com'
10
+ port 4979
11
+ channel 'hato'
12
+ end
13
+
14
+ plugin 'Mail' do
15
+ smtp address: 'smtp.example.com',
16
+ port: 587,
17
+ domain: 'example',
18
+ user_name: 'hato',
19
+ password: 'password',
20
+ enable_ssl: true
21
+
22
+ subject_template = <<EOS
23
+ [<%= args[:tag] %>] Notification
24
+ EOS
25
+ body_template = <<EOS
26
+ You've got a message:
27
+
28
+ <%= args[:message] %>
29
+ EOS
30
+
31
+ message from: 'hato@example.com',
32
+ to: [
33
+ 'foo@example.com',
34
+ 'bar@example.com',
35
+ ],
36
+ subject_template: subject_template,
37
+ body_template: body_template
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,10 @@
1
+ require 'hato/plugin'
2
+
3
+ module Hato
4
+ module Plugin
5
+ class Ikachan < Base
6
+ def notify(args)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'hato/plugin'
2
+
3
+ module Hato
4
+ module Plugin
5
+ class Mail < Base
6
+ def notify(args)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -43,6 +43,7 @@ describe Hato::Config do
43
43
  expect(described_class.tags.size).to be == 1
44
44
 
45
45
  tag = described_class.tags.first
46
+ tag.activate!
46
47
  expect(tag.plugins).to be_an_instance_of Array
47
48
  expect(tag.plugins.size).to be == 2
48
49
 
@@ -0,0 +1,40 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'spec_helper'
4
+ require 'rack/test'
5
+
6
+ describe Hato::Httpd do
7
+ include Rack::Test::Methods
8
+
9
+ def app
10
+ config = Hato::Config.load(
11
+ File.expand_path('../../../assets/config/test.rb', __FILE__)
12
+ )
13
+ observer = Hato::Observer.new(config)
14
+ Hato::Httpd::App.set(:observer, observer)
15
+ Hato::Httpd::App.set(:api_key, config.api_key)
16
+ Hato::Httpd::App.new
17
+ end
18
+
19
+ describe 'index' do
20
+ it do
21
+ get '/'
22
+ expect(last_response).to be_ok
23
+ expect(last_response.body).to eq('Hato https://github.com/kentaro/hato')
24
+ end
25
+ end
26
+
27
+ describe 'notify' do
28
+ it 'success' do
29
+ post '/notify', {message: 'test', tag: 'test', api_key: 'test'}
30
+ expect(last_response).to be_ok
31
+ expect(last_response.body).to eq('{"status":"success","message":"Successfully sent the message you notified to me."}')
32
+ end
33
+
34
+ it 'error' do
35
+ post '/notify', {message: 'test', tag: 'test', api_key: 'wrong_key'}
36
+ expect(last_response).to be_forbidden
37
+ expect(last_response.body).to eq('{"status":"error","message":"API key is wrong. Confirm your API key setting of server/client."}')
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,51 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Hato::Observer do
4
+ describe '.update' do
5
+ context "tag's name is String object in config" do
6
+ before do
7
+ Hato::Config.reset
8
+ config = Hato::Config.load(
9
+ File.expand_path('../../../assets/config/test.rb', __FILE__)
10
+ )
11
+ @observer = described_class.new(config)
12
+ end
13
+
14
+ it 'receive `activate!` with nil' do
15
+ logger = double('logger')
16
+ logger.stub(:error).and_return(nil)
17
+
18
+ Hato::Config::Tag.any_instance.should_receive(:activate!).with(nil)
19
+
20
+ @observer.update(
21
+ tag: 'test',
22
+ message: 'hello!',
23
+ logger: logger
24
+ )
25
+ end
26
+ end
27
+
28
+ context "tag's name is Regexp object in config" do
29
+ before do
30
+ Hato::Config.reset
31
+ config = Hato::Config.load(
32
+ File.expand_path('../../../assets/config/tags_name_regexp.rb', __FILE__)
33
+ )
34
+ @observer = described_class.new(config)
35
+ end
36
+
37
+ it 'receive `activate!` with matched values' do
38
+ logger = double('logger')
39
+ logger.stub(:error).and_return(nil)
40
+
41
+ Hato::Config::Tag.any_instance.should_receive(:activate!).with(['Yang', 'Reinhard'])
42
+
43
+ @observer.update(
44
+ tag: 'Yang and Reinhard',
45
+ message: 'hello!',
46
+ logger: logger
47
+ )
48
+ end
49
+ end
50
+ end
51
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../assets/lib', __FILE__)
1
2
  require_relative '../lib/hato'
2
3
 
3
4
  RSpec.configure do |config|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kentaro Kuribayashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-17 00:00:00.000000000 Z
11
+ date: 2013-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rack-test
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: A Notification Management Tools
112
126
  email:
113
127
  - kentarok@gmail.com
@@ -130,8 +144,13 @@ files:
130
144
  - lib/hato/observer.rb
131
145
  - lib/hato/plugin.rb
132
146
  - lib/hato/version.rb
147
+ - spec/assets/config/tags_name_regexp.rb
133
148
  - spec/assets/config/test.rb
149
+ - spec/assets/lib/hato/plugin/ikachan.rb
150
+ - spec/assets/lib/hato/plugin/mail.rb
134
151
  - spec/lib/hato/config_spec.rb
152
+ - spec/lib/hato/httpd_spec.rb
153
+ - spec/lib/hato/observer_spec.rb
135
154
  - spec/spec_helper.rb
136
155
  homepage: https://github.com/kentaro/hato
137
156
  licenses:
@@ -158,6 +177,11 @@ signing_key:
158
177
  specification_version: 4
159
178
  summary: A Notification Management Tools
160
179
  test_files:
180
+ - spec/assets/config/tags_name_regexp.rb
161
181
  - spec/assets/config/test.rb
182
+ - spec/assets/lib/hato/plugin/ikachan.rb
183
+ - spec/assets/lib/hato/plugin/mail.rb
162
184
  - spec/lib/hato/config_spec.rb
185
+ - spec/lib/hato/httpd_spec.rb
186
+ - spec/lib/hato/observer_spec.rb
163
187
  - spec/spec_helper.rb