cinch-cooldown 1.0.0 → 1.0.1

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.
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Cinch::Cooldown
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/cinch-cooldown.png)](http://badge.fury.io/rb/cinch-cooldown)
4
+ [![Dependency Status](https://gemnasium.com/bhaberer/cinch-cooldown.png)](https://gemnasium.com/bhaberer/cinch-cooldown)
5
+ [![Build Status](https://travis-ci.org/bhaberer/cinch-cooldown.png?branch=master)](https://travis-ci.org/bhaberer/cinch-cooldown)
6
+ [![Coverage Status](https://coveralls.io/repos/bhaberer/cinch-cooldown/badge.png?branch=master)](https://coveralls.io/r/bhaberer/cinch-cooldown?branch=master)
3
7
  [![Code Climate](https://codeclimate.com/github/bhaberer/cinch-cooldown.png)](https://codeclimate.com/github/bhaberer/cinch-cooldown)
4
8
 
5
9
  This library is used to add a global cooldown so that users are prevented from spamming the
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'cinch-cooldown/version'
4
+ require 'cinch/cooldown/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "cinch-cooldown"
@@ -17,5 +17,11 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency('time-lord', '>= 1.0.1')
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'coveralls'
23
+ gem.add_development_dependency 'cinch-test'
24
+
25
+ gem.add_dependency 'time-lord', '~> 1.0.1'
26
+ gem.add_dependency 'cinch', '~> 2.0.5'
21
27
  end
@@ -1,4 +1,4 @@
1
- require 'cinch-cooldown/version'
1
+ require 'cinch/cooldown/version'
2
2
  require 'time-lord'
3
3
 
4
4
  module Cinch
@@ -7,10 +7,6 @@ module Cinch
7
7
  def enforce_cooldown
8
8
  hook(:pre, :for => [:match], :method => lambda {|m| cooldown_finished?(m)})
9
9
  end
10
-
11
- def reset_cooldown
12
- # TODO
13
- end
14
10
  end
15
11
 
16
12
  def cooldown_finished?(m)
@@ -18,7 +14,7 @@ module Cinch
18
14
  return true unless shared[:cooldown] && shared[:cooldown][:config]
19
15
  synchronize(:cooldown) do
20
16
  # return if we don't have a channel (i.e. user is pming the bot)
21
- return false if m.channel.nil?
17
+ return true if m.channel.nil?
22
18
 
23
19
  channel = m.channel.name
24
20
  user = m.user.nick
@@ -41,9 +37,9 @@ module Cinch
41
37
  def configuration_broken?(channel)
42
38
  # return if the config doesn't smell right for this channel
43
39
  return true unless shared[:cooldown][:config].key?(channel) &&
44
- shared[:cooldown][:config][channel].key?(:global) &&
45
- shared[:cooldown][:config][channel].key?(:user)
46
-
40
+ config_for(channel).key?(:global) &&
41
+ config_for(channel).key?(:user)
42
+ false
47
43
  end
48
44
 
49
45
  def first_run?(channel, user)
@@ -70,6 +66,7 @@ module Cinch
70
66
  return true
71
67
  end
72
68
  end
69
+ return false
73
70
  end
74
71
 
75
72
  def cooldown_message(channel, user)
@@ -89,23 +86,35 @@ module Cinch
89
86
  end
90
87
 
91
88
  def cooldown_channel_expire_time(channel)
92
- shared[:cooldown][channel][:global] + shared[:cooldown][:config][channel][:global]
89
+ global_cooldown_for(channel) + config_for(channel)[:global]
93
90
  end
94
91
 
95
92
  def cooldown_user_expire_time(channel, user)
96
- shared[:cooldown][channel][user] + shared[:cooldown][:config][channel][:user]
93
+ user_cooldown_for(channel, user) + config_for(channel)[:user]
97
94
  end
98
95
 
99
96
  def user_cooldown_finished?(channel, user)
100
- cooldown = shared[:cooldown][:config][channel][:user]
101
- elapsed = Time.now - shared[:cooldown][channel][user]
102
- return cooldown < elapsed
97
+ cooldown = config_for(channel)[:user]
98
+ elapsed = Time.now - user_cooldown_for(channel, user)
99
+ return cooldown <= elapsed
103
100
  end
104
101
 
105
102
  def channel_cooldown_finished?(channel)
106
- cooldown = shared[:cooldown][:config][channel][:global]
107
- elapsed = Time.now - shared[:cooldown][channel][:global]
108
- return cooldown < elapsed
103
+ cooldown = config_for(channel)[:global]
104
+ elapsed = Time.now - global_cooldown_for(channel)
105
+ return cooldown <= elapsed
106
+ end
107
+
108
+ def config_for(chan)
109
+ shared[:cooldown][:config][chan]
110
+ end
111
+
112
+ def global_cooldown_for(chan)
113
+ shared[:cooldown][chan][:global] ||= Time.now
114
+ end
115
+
116
+ def user_cooldown_for(chan, nick)
117
+ shared[:cooldown][chan][nick] || Time.now
109
118
  end
110
119
  end
111
120
  end
@@ -1,5 +1,5 @@
1
1
  module Cinch
2
2
  module Cooldown
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ class MyPlugin
4
+ include Cinch::Plugin
5
+
6
+ enforce_cooldown
7
+
8
+ match /thing/
9
+ def execute(m)
10
+ m.reply 'OMG'
11
+ end
12
+ end
13
+
14
+ def bot_for_cooldowns(global = 10, user = 0)
15
+ @bot = Cinch::Test::MockBot.new do
16
+ configure do |c|
17
+ c.nick = 'testbot'
18
+ c.server = nil
19
+ c.channels = ['foo']
20
+ c.reconnect = false
21
+ c.plugins.plugins = [MyPlugin]
22
+ c.shared[:cooldown] = { :config => { '#foo' => { :global => global, :user => user } } }
23
+ end
24
+ end
25
+ end
26
+
27
+ describe Cinch::Cooldown do
28
+ include Cinch::Test
29
+
30
+ it 'should not trigger cooldowns on private messages' do
31
+ @bot = bot_for_cooldowns(10)
32
+ get_replies(make_message(@bot, "!thing"))
33
+ get_replies(make_message(@bot, "!thing")).first.text.
34
+ should match "OMG"
35
+ end
36
+
37
+ it 'should allow plugins to mandate a global cooldown between responses in channel' do
38
+ @bot = bot_for_cooldowns(10)
39
+ get_replies(make_message(@bot, "!thing", channel: '#foo'))
40
+ get_replies(make_message(@bot, "!thing", channel: '#foo')).first.text.
41
+ should match(/Sorry, you'll have to wait \d+ seconds from now before I can talk/)
42
+ end
43
+
44
+ it 'should allow plugins allow responses after the global cooldown' do
45
+ @bot = bot_for_cooldowns(5)
46
+ get_replies(make_message(@bot, "!thing", channel: '#foo'))
47
+ sleep 5
48
+ get_replies(make_message(@bot, "!thing", channel: '#foo')).first.text.
49
+ should == 'OMG'
50
+ end
51
+
52
+ it 'should allow plugins to mandate a minimum time between responses in channel' do
53
+ @bot = bot_for_cooldowns(5, 10)
54
+ get_replies(make_message(@bot, "!thing", channel: '#foo'))
55
+ sleep 6
56
+ get_replies(make_message(@bot, "!thing", channel: '#foo')).first.text.
57
+ should match(/Sorry, you'll have to wait \d+ seconds from now before you can use/)
58
+ end
59
+
60
+ it 'should allow plugins to mandate a minimum time between responses in channel' do
61
+ @bot = bot_for_cooldowns(5, 10)
62
+ get_replies(make_message(@bot, "!thing", channel: '#foo'))
63
+ sleep 10
64
+ get_replies(make_message(@bot, "!thing", channel: '#foo')).first.text.
65
+ should == 'OMG'
66
+ end
67
+
68
+ it 'should not trigger if the config for the current channel does not exist' do
69
+ @bot = bot_for_cooldowns(5, 10)
70
+ get_replies(make_message(@bot, "!thing", channel: '#bar'))
71
+ get_replies(make_message(@bot, "!thing", channel: '#bar')).first.text.
72
+ should == 'OMG'
73
+ end
74
+
75
+ it 'should trigger for other users if the global cooldown is not finished' do
76
+ @bot = bot_for_cooldowns(10, 20)
77
+ get_replies(make_message(@bot, "!thing", channel: '#foo', nick: 'test1')).first.text.
78
+ should == 'OMG'
79
+ get_replies(make_message(@bot, "!thing", channel: '#foo', nick: 'test2')).first.text.
80
+ should_not == 'OMG'
81
+ end
82
+ end
@@ -0,0 +1,11 @@
1
+ require 'coveralls'
2
+ require 'simplecov'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
9
+
10
+ require 'cinch/cooldown'
11
+ require 'cinch/test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-cooldown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,14 +9,78 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-24 00:00:00.000000000 Z
12
+ date: 2013-07-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: time-lord
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: rspec
16
32
  requirement: !ruby/object:Gem::Requirement
17
33
  none: false
18
34
  requirements:
19
35
  - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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: coveralls
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
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'
62
+ - !ruby/object:Gem::Dependency
63
+ name: cinch-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: time-lord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
20
84
  - !ruby/object:Gem::Version
21
85
  version: 1.0.1
22
86
  type: :runtime
@@ -24,9 +88,25 @@ dependencies:
24
88
  version_requirements: !ruby/object:Gem::Requirement
25
89
  none: false
26
90
  requirements:
27
- - - ! '>='
91
+ - - ~>
28
92
  - !ruby/object:Gem::Version
29
93
  version: 1.0.1
94
+ - !ruby/object:Gem::Dependency
95
+ name: cinch
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 2.0.5
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 2.0.5
30
110
  description: This gem allows you to set a shared timer across plugins that are configured
31
111
  to respect it.
32
112
  email:
@@ -36,13 +116,16 @@ extensions: []
36
116
  extra_rdoc_files: []
37
117
  files:
38
118
  - .gitignore
119
+ - .travis.yml
39
120
  - Gemfile
40
121
  - LICENSE.txt
41
122
  - README.md
42
123
  - Rakefile
43
124
  - cinch-cooldown.gemspec
44
- - lib/cinch-cooldown.rb
45
- - lib/cinch-cooldown/version.rb
125
+ - lib/cinch/cooldown.rb
126
+ - lib/cinch/cooldown/version.rb
127
+ - spec/cinch-cooldown_spec.rb
128
+ - spec/spec_helper.rb
46
129
  homepage: https://github.com/bhaberer/cinch-cooldown
47
130
  licenses: []
48
131
  post_install_message:
@@ -67,4 +150,7 @@ rubygems_version: 1.8.24
67
150
  signing_key:
68
151
  specification_version: 3
69
152
  summary: Global Cooldown tracker for Cinch Plugins
70
- test_files: []
153
+ test_files:
154
+ - spec/cinch-cooldown_spec.rb
155
+ - spec/spec_helper.rb
156
+ has_rdoc: