rake_notification 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c6a3378a9677b2056d89ebd7c7901b8612e82e67
4
- data.tar.gz: 7e36ff689aa4d62697e3e6a2b9b2f8044d09e154
3
+ metadata.gz: 2a8517f309e1bea9d58fe584474dbaa145fa1aaa
4
+ data.tar.gz: 21f6dbca969770b2e71b63d32ea1083235f706b5
5
5
  SHA512:
6
- metadata.gz: 38e812351b33202687047f77557370ffdbd3ec24fc2fe526c1761f86b483db0857777049e3ec23061b1ae2f23d634303b21ef869f0b729cc7299b6a942f67382
7
- data.tar.gz: 780d0d7119135c8d1408670ef8a843866bffed2bd76472798ae9a0cdbe11e4bdc237d935bbe2072ed40d1401c7154cfb0b3bc844fcf73c0ba14b7bbded8aa0a1
6
+ metadata.gz: 8c53716a47671ea36cfcc23bed3a6969afd150f5f67b5ac83e51bd23339644440879834637ab24f67132387f2b30b9b12109628cf03e326edbeece360d28f236
7
+ data.tar.gz: c67b702b0f57212873bc258d5240b973a82f6791280a8f06a60281232f089126b945c9c30deef69c19ae3fd479c4fe0a12f471a0dc0c638d2ac6cb2d7c555098
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.1.3
5
+
6
+ before_install:
7
+ - gem update bundler
8
+
9
+ script:
10
+ - bundle exec rspec
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # RakeNotification
2
2
 
3
+ [![Build Status](https://img.shields.io/travis/mizoR/rake_notification/master.svg?style=flat)](https://travis-ci.org/mizoR/rake_notification)
4
+
3
5
  Notification of status for rake
4
6
 
5
7
  ## Installation
@@ -14,7 +16,43 @@ And then execute:
14
16
 
15
17
  ### Execution
16
18
 
17
- $ bundle exec rake_notify your_task
19
+ $ bundle exec rake_notify awesome_task
20
+
21
+ ### Usage
22
+
23
+ #### Ikachan Notifier
24
+
25
+ ```rb
26
+ # config/rake_notification.rb
27
+
28
+ endpoint = 'https://irc.example.com:4979/'
29
+ channel = '#rake_notification'
30
+ ikachan = RakeNotifier::Ikachan.new(endpoint, channel)
31
+
32
+ Rake.application.register_interceptor ikachan
33
+ Rake.application.register_observer ikachan
34
+ ```
35
+
36
+ #### Custom Notifier
37
+
38
+ ```rb
39
+ # config/rake_notification.rb
40
+
41
+ notifier = Object.new.tap do |o|
42
+ def o.started_task(task)
43
+ CustomNotifier.started(task).deliver
44
+ end
45
+
46
+ def o.completed_task(task, system_exit)
47
+ if !system_exit.success?
48
+ CustomNotifier.failed(task, system_exit)
49
+ end
50
+ end
51
+ end
52
+
53
+ Rake.application.register_interceptor notifier
54
+ Rake.application.register_observer notifier
55
+ ```
18
56
 
19
57
  ## Contributing
20
58
 
data/bin/rake_notify CHANGED
@@ -9,10 +9,13 @@ end
9
9
  require 'rake'
10
10
  require 'rake_notification'
11
11
 
12
- using RakeNotification
12
+ class Rake::Application
13
+ prepend RakeNotification
14
+ end
13
15
 
14
- if File.exist?(RakeNotification.config_path)
16
+ begin
15
17
  require RakeNotification.config_path
18
+ rescue LoadError
16
19
  end
17
20
 
18
21
  Rake.application.run
@@ -1,3 +1,3 @@
1
1
  module RakeNotification
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,57 +1,56 @@
1
1
  require 'rake'
2
+ require 'rake_notifier'
2
3
 
3
4
  module RakeNotification
4
5
  def self.config_path
5
- './config/rake_notification.rb'
6
+ './config/rake_notification'
6
7
  end
7
8
 
8
- refine Rake::Application do
9
- def init(app_name='rake')
10
- super
11
- end
12
-
13
- def reconstructed_command_line
14
- @reconstructed_command_line ||= "#{File.basename($0)} #{ARGV.join(' ')}"
15
- end
9
+ def reconstructed_command_line
10
+ @reconstructed_command_line ||= "#{File.basename($0)} #{ARGV.join(' ')}"
11
+ end
16
12
 
17
- def register_observer(observer)
18
- notification_observers << observer
19
- end
13
+ def register_observer(observer)
14
+ notification_observers << observer
15
+ end
20
16
 
21
- def register_interceptor(interceptor)
22
- notification_interceptors << interceptor
23
- end
17
+ def register_interceptor(interceptor)
18
+ notification_interceptors << interceptor
19
+ end
24
20
 
25
- def run
26
- inform_interceptors
27
- super
28
- rescue SystemExit => e
29
- inform_observers(e)
30
- raise e
31
- else
32
- inform_observers
33
- end
21
+ def top_level
22
+ inform_interceptors rescue nil
23
+
24
+ super
25
+ rescue SystemExit => original_error
26
+ inform_observers(original_error) rescue nil
27
+ raise original_error
28
+ rescue Exception => original_error
29
+ inform_observers(SystemExit.new(1, original_error.message)) rescue nil
30
+ raise original_error
31
+ else
32
+ inform_observers rescue nil
33
+ end
34
34
 
35
- private
35
+ private
36
36
 
37
- def inform_interceptors
38
- notification_interceptors.each do |interceptor|
39
- interceptor.started_task(self)
40
- end
37
+ def inform_interceptors
38
+ notification_interceptors.each do |interceptor|
39
+ interceptor.started_task(self)
41
40
  end
41
+ end
42
42
 
43
- def inform_observers(system_exit=SystemExit.new(0))
44
- notification_observers.each do |observer|
45
- observer.completed_task(self, system_exit)
46
- end
43
+ def inform_observers(system_exit=SystemExit.new(0))
44
+ notification_observers.each do |observer|
45
+ observer.completed_task(self, system_exit)
47
46
  end
47
+ end
48
48
 
49
- def notification_interceptors
50
- @notification_interceptors ||= []
51
- end
49
+ def notification_interceptors
50
+ @notification_interceptors ||= []
51
+ end
52
52
 
53
- def notification_observers
54
- @notification_observers ||= []
55
- end
53
+ def notification_observers
54
+ @notification_observers ||= []
56
55
  end
57
56
  end
@@ -0,0 +1,76 @@
1
+ require 'active_support/core_ext/string/strip'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'pathname'
5
+
6
+ module RakeNotifier
7
+ class Ikachan < Base
8
+
9
+ START_LABEL = "\x02\x0307[START]\x0f"
10
+ SUCCESS_LABEL = "\x02\x0303[SUCCESS]\x0f"
11
+ FAILED_LABEL = "\x02\x0304[FAILED]\x0f"
12
+
13
+ def initialize(url, channel)
14
+ @client = Client.new(url, channel)
15
+ end
16
+
17
+ def started_task(task)
18
+ notice <<-EOS.strip_heredoc
19
+ #{START_LABEL} $ #{task.reconstructed_command_line}
20
+ (from #{hostname} at #{Time.now} RAILS_ENV=#{rails_env})
21
+ EOS
22
+ end
23
+
24
+ def completed_task(task, system_exit)
25
+ label = system_exit.success? ? SUCCESS_LABEL : FAILED_LABEL
26
+ notice <<-EOS.strip_heredoc
27
+ #{label} $ #{task.reconstructed_command_line}
28
+ (exit #{system_exit.status} from #{hostname} at #{Time.now} RAILS_ENV=#{rails_env})
29
+ EOS
30
+ end
31
+
32
+ private
33
+
34
+ def notice(message)
35
+ message.each_line {|m| @client.notice m }
36
+ end
37
+
38
+ class Client
39
+ def initialize(url, channel)
40
+ @url = url
41
+ @channel = channel
42
+ end
43
+
44
+ def join
45
+ request('/join', {channel: @channel})
46
+ end
47
+
48
+ def notice(message)
49
+ join
50
+ request('/notice', {channel: @channel, message: message})
51
+ end
52
+
53
+ def uri_for(path = nil)
54
+ uri = URI.parse("#{@url}/#{path}")
55
+ uri.path = Pathname.new(uri.path).cleanpath.to_s
56
+ uri
57
+ end
58
+
59
+ def request(path, params)
60
+ begin
61
+ uri = uri_for(path)
62
+
63
+ http = Net::HTTP.new(uri.host, uri.port)
64
+ http.open_timeout = http.read_timeout = 10
65
+
66
+ req = Net::HTTP::Post.new(uri.path)
67
+ req.form_data = params
68
+
69
+ http.request(req)
70
+ rescue StandardError, TimeoutError => e
71
+ $stderr.puts "#{e.class} #{e.message}"
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,17 @@
1
+ require 'socket'
2
+
3
+ module RakeNotifier
4
+ autoload :Ikachan, File.join(__dir__, 'rake_notifier', 'ikachan')
5
+
6
+ class Base
7
+ private
8
+
9
+ def hostname
10
+ Socket.gethostname rescue 'Anonymous'
11
+ end
12
+
13
+ def rails_env
14
+ ENV['RAILS_ENV'] || 'development'
15
+ end
16
+ end
17
+ end
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "pry"
25
25
  spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "rspec-its"
26
27
  end
@@ -1,8 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe RakeNotification do
4
- using RakeNotification
5
-
6
4
  subject { app }
7
5
 
8
6
  let(:app) { Rake::Application.new }
@@ -10,48 +8,55 @@ describe RakeNotification do
10
8
  let(:notifier) { double('notifier') }
11
9
 
12
10
  before {
11
+ class Rake::Application; prepend RakeNotification; end
12
+
13
13
  allow(app).to receive(:invoke_task).and_return(true)
14
14
  }
15
15
 
16
16
  it { expect(described_class.config_path).to be_a String }
17
17
 
18
- it { should be_respond_to :run }
18
+ it { is_expected.to be_respond_to :run }
19
19
 
20
20
  describe "#reconstruct_command_line" do
21
- subject { app.reconstruct_command_line }
21
+ subject { app.reconstructed_command_line }
22
+
22
23
  it {
23
24
  app.init
24
- expect(app.reconstructed_command_line).to be_a String
25
+ is_expected.to be_a String
25
26
  }
26
27
  end
27
28
 
28
29
  describe '#register_interceptor' do
30
+ subject { notifier }
31
+
29
32
  before { app.register_interceptor notifier }
30
33
 
31
- it '#started_task が実行されること' do
32
- expect(notifier).to receive(:started_task).with(app)
33
- expect(notifier).not_to receive(:completed_task)
34
+ it 'should receive started_task' do
35
+ is_expected.to receive(:started_task).with(app)
36
+ is_expected.not_to receive(:completed_task)
34
37
 
35
38
  app.run
36
39
  end
37
40
  end
38
41
 
39
42
  describe '#register_observer' do
43
+ subject { notifier }
44
+
40
45
  before { app.register_observer notifier }
41
46
 
42
- it '#completed_task が実行されること' do
43
- expect(notifier).not_to receive(:started_task)
44
- expect(notifier).to receive(:completed_task)
47
+ it 'is_expected.to receive completed_task' do
48
+ is_expected.not_to receive(:started_task)
49
+ is_expected.to receive(:completed_task)
45
50
 
46
51
  app.run
47
52
  end
48
53
 
49
- context 'タスクの実行中に例外が発生' do
54
+ context 'raise error on invoking task' do
50
55
  before { app.stub(:invoke_task).and_raise(StandardError.new('Rake Error')) }
51
56
 
52
- it '#completed_task が実行されること' do
53
- expect(notifier).not_to receive(:started_task)
54
- expect(notifier).to receive(:completed_task).with(app, kind_of(SystemExit))
57
+ it 'should receive completed_task' do
58
+ is_expected.not_to receive(:started_task)
59
+ is_expected.to receive(:completed_task).with(app, kind_of(SystemExit))
55
60
  begin
56
61
  $stderr.reopen('/dev/null', 'w')
57
62
  app.run
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe RakeNotifier::Ikachan::Client do
4
+ subject { client }
5
+
6
+ let(:client) { described_class.new(url, channel) }
7
+ let(:url) { 'https://irc.example.com:4649' }
8
+ let(:channel) { '#rake_notification' }
9
+
10
+ describe '#uri_for' do
11
+ subject { client.uri_for('/notice') }
12
+
13
+ let(:path) { '/notice' }
14
+
15
+ its(:to_s) { is_expected.to eq "#{url}#{path}" }
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe RakeNotifier::Ikachan do
4
+ subject { described_class.new(url, channel) }
5
+
6
+ let(:url) { 'https://irc.example.com:4979' }
7
+ let(:channel) { '#rake_notification' }
8
+
9
+ it { is_expected.to respond_to :started_task }
10
+ it { is_expected.to respond_to :completed_task }
11
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe RakeNotifier::Base do
4
+ subject { anonymous_class.new }
5
+
6
+ let(:anonymous_class) { Class.new(described_class) }
7
+
8
+ its(:hostname) { is_expected.to be_a String }
9
+ its(:rails_env) { is_expected.to be_a String }
10
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  require 'rspec'
2
+ require 'rspec/its'
2
3
  require 'rake'
3
4
  require 'rake_notification'
5
+ require 'rake_notifier'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake_notification
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mizokami
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-15 00:00:00.000000000 Z
11
+ date: 2014-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - suzunatsu@yahoo.com
@@ -89,6 +103,7 @@ extensions: []
89
103
  extra_rdoc_files: []
90
104
  files:
91
105
  - ".gitignore"
106
+ - ".travis.yml"
92
107
  - Gemfile
93
108
  - LICENSE.txt
94
109
  - README.md
@@ -96,9 +111,13 @@ files:
96
111
  - bin/rake_notify
97
112
  - lib/rake_notification.rb
98
113
  - lib/rake_notification/version.rb
99
- - lib/rake_notification/whenever.rb
114
+ - lib/rake_notifier.rb
115
+ - lib/rake_notifier/ikachan.rb
100
116
  - rake_notification.gemspec
101
117
  - spec/lib/rake_notification_spec.rb
118
+ - spec/lib/rake_notifier/ikachan/client_spec.rb
119
+ - spec/lib/rake_notifier/ikachan_spec.rb
120
+ - spec/lib/rake_notifier_spec.rb
102
121
  - spec/spec_helper.rb
103
122
  homepage: ''
104
123
  licenses:
@@ -126,4 +145,7 @@ specification_version: 4
126
145
  summary: Rake notification
127
146
  test_files:
128
147
  - spec/lib/rake_notification_spec.rb
148
+ - spec/lib/rake_notifier/ikachan/client_spec.rb
149
+ - spec/lib/rake_notifier/ikachan_spec.rb
150
+ - spec/lib/rake_notifier_spec.rb
129
151
  - spec/spec_helper.rb
@@ -1 +0,0 @@
1
- job_type :rake_notify, "cd :path && :environment_variable=:environment :bundle_command rake_notify :task --silent :output"