balotelli 0.4.1 → 0.4.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9bfeedb4c09e7cdac32022e2cc033310c9254c48
4
- data.tar.gz: 9f0af9c1914dc98ae0f61b6487a1ff40f71d3568
3
+ metadata.gz: 7addde5a832912208f7790a6559c172d818a555a
4
+ data.tar.gz: 0a86ccc08b3e928152d024d9e8e154aa36cdbf46
5
5
  SHA512:
6
- metadata.gz: d580a3e378aee126aa667b675d6dec41d78d6c6e018f266bfbcdd7d36bc6cc9103ae33839f2400181b105386f50c312e820e77be570124edc489e34ac60405b9
7
- data.tar.gz: 1687cc8f9f82a8d532fe2706f3812099c785c60e460208054e674ce75be6070de0e5763673b1f08a08cea45d4db4e3bd7376239cc599c89003fb250806092274
6
+ metadata.gz: a252b2a40886641e5912b5818416a53d014b3c2b1a026f9a688153c1a5397d162be1da2d4b755a36250ee27487cbe5526940eba3a77108c90a59b11982d18168
7
+ data.tar.gz: 9f37a6595af818d752e9cfcdd7814ad183ccf09364b6b36f6445ca8137e3b685bfdacd64cb9c1b809ec0307eccfc1f5910bdab42dfba919e1f59dfaa245d6450
data/.travis.yml CHANGED
@@ -8,3 +8,12 @@ rvm:
8
8
  - 2.0
9
9
  - jruby-9
10
10
  - rbx
11
+ - 1.9.3
12
+ - ruby-head
13
+ - jruby-1.7
14
+ matrix:
15
+ allow_failures:
16
+ - rvm: rbx
17
+ - rvm: 1.9.3
18
+ - rvm: ruby-head
19
+ - rvm: jruby-1.7
data/Gemfile.lock CHANGED
@@ -1,16 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- balotelli (0.4.0)
4
+ balotelli (0.4.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- minitest (5.8.0)
9
+ minitest (5.8.5)
10
10
  rake (10.4.2)
11
11
 
12
12
  PLATFORMS
13
- java
14
13
  ruby
15
14
 
16
15
  DEPENDENCIES
@@ -9,19 +9,20 @@ require 'balotelli/config'
9
9
 
10
10
  module Balotelli
11
11
  class Base
12
- def initialize(str, route, block, message, command)
13
- @str = str
12
+ extend Core::Utils::ClassVariableGetter
13
+ class_variable_get :cache, :cvs, :mutex
14
+
15
+ def initialize(block, message, matchdata)
14
16
  @message = message
15
17
  @block = block
16
- @route = route
17
- @command = command
18
- @matchdata = @command.match(@route)
18
+ @matchdata = matchdata
19
19
  end
20
20
 
21
- def execute
21
+ def execute!
22
22
  instance_exec(@message, @matchdata, &@block)
23
23
  end
24
- private :execute
24
+
25
+ alias execute execute!
25
26
 
26
27
  def out(message_string, destination = nil)
27
28
  self.class.privmsg((destination || @message.responder), message_string)
@@ -35,19 +36,20 @@ module Balotelli
35
36
  execute_in_mutex(channel) do
36
37
  self.class.names(channel)
37
38
  end
38
- __CACHE__[:user_list][channel]
39
+ cache[:user_list][channel]
39
40
  end
40
41
 
41
42
  def execute_in_mutex(channel)
42
43
  cv = ConditionVariable.new
43
- if __CVS__.key? channel
44
- __CVS__[channel] << cv
44
+ if cvs.key? channel
45
+ cvs[channel] << cv
45
46
  else
46
- __CVS__[channel] = [cv]
47
+ cvs[channel] = [cv]
47
48
  end
48
49
  yield
49
- __MUTEX__.synchronize { cv.wait(__MUTEX__) }
50
+ mutex.synchronize { cv.wait(mutex) }
50
51
  end
52
+ private :execute_in_mutex
51
53
 
52
54
  def method_missing(method, *args, **params, &block)
53
55
  if method =~ /__([[[:upper:]]_]+)__/
@@ -93,6 +95,8 @@ module Balotelli
93
95
  def mod_match(str, priv = false)
94
96
  if str =~ /\A[^a-zA-Z0-9\s]?(\S+) ?(.*)/
95
97
  if (mod = @cache[:modules][Regexp.last_match(1).downcase])
98
+ str.slice!(0..Regexp.last_match(1).length)
99
+ str.lstrip!
96
100
  mod.match(Regexp.last_match(2), priv)
97
101
  end
98
102
  elsif str == :join
@@ -128,40 +132,52 @@ module Balotelli
128
132
 
129
133
  def process_message(str)
130
134
  if str =~ Core::Utils::PRIV_MSG_REGEX
131
- metadata = { user: Regexp.last_match(1), channel: Regexp.last_match(2), message: Regexp.last_match(3) }
132
- priv = !(Regexp.last_match(2) =~ /#.+/)
133
- if (match = match(metadata[:message], priv)) ||
134
- (match = mod_match(metadata[:message], priv))
135
- new_response(str, match, metadata, Core::PrivMsg, priv)
136
- end
135
+ process_private_message(str, *Regexp.last_match[1..3])
137
136
  elsif str =~ Core::Utils::JOIN_REGEX
138
- metadata = { user: Regexp.last_match(1), channel: Regexp.last_match(2) }
139
- if (match = match(:join)) || (match = mod_match(:join))
140
- new_response(str, match, metadata, Core::Join)
141
- end
137
+ process_join(str, *Regexp.last_match[1..2])
142
138
  end
143
139
  end
144
140
 
145
- def new_response(str, match, metadata, klass, priv = nil)
146
- route, block, mod, command = match.flatten
147
- message = klass.new(*[metadata, priv].compact)
148
- new(str, route, block, message, command).tap do |response|
149
- response.extend(mod) if mod.class.to_s == 'Module'
150
- end.send(:execute)
141
+ def process_private_message(str, user, channel, message)
142
+ privacy = !(channel =~ /#.+/)
143
+ if (route = match(message, privacy)) ||
144
+ (route = mod_match(message, privacy))
145
+ new_response(user, channel, message, route, Core::PrivMsg, privacy)
146
+ end
147
+ end
148
+
149
+ def process_join(str, user, channel)
150
+ if (route = match(:join)) || (route = mod_match(:join))
151
+ new_response(user, channel, nil, route, Core::Join)
152
+ end
153
+ end
154
+
155
+
156
+ def new_response(user, channel, message, route_match, klass, priv = nil)
157
+ route_pattern, block, module_name = route_match.flatten
158
+ message = klass.new(*[user, channel, message, priv].compact)
159
+ matchdata = message.content.match(route_pattern) if route_pattern.is_a?(Regexp)
160
+ new(block, message, matchdata).tap do |response|
161
+ response.extend(module_name) if module_name.class.to_s == 'Module'
162
+ end.execute!
151
163
  end
152
164
 
153
165
  def process_table(string)
154
166
  if @cvs.any? && string =~ (regex = Core::Utils.table_regex(@nick))
155
- metadata = { channel: Regexp.last_match(2), message: Regexp.last_match(3) }
156
- users = @cache[:user_list][metadata[:channel]] = metadata[:message]
167
+ channel = Regexp.last_match(2)
168
+ users = Regexp.last_match(3)
157
169
  until (str = sgets) =~ %r{End of \/NAME}i
158
170
  users << " #{str.match(regex).captures.first}"
159
171
  end
160
- @cache[:user_list][metadata[:channel]] = users.split(' ')
161
- @cvs[metadata[:channel]].shift.signal
162
- @cvs.delete(metadata[:channel]) if @cvs[metadata[:channel]].empty?
172
+ @cache[:user_list][channel] = users.split(' ')
173
+ signal_cvs(channel)
163
174
  end
164
175
  end
176
+
177
+ def signal_cvs(channel)
178
+ @cvs[channel].shift.signal
179
+ @cvs.delete(channel) if @cvs[channel].empty?
180
+ end
165
181
  end
166
182
  end
167
183
  end
@@ -23,37 +23,19 @@ module Balotelli
23
23
  def included(des)
24
24
  super
25
25
 
26
- define_log_method
27
-
28
26
  des.instance_variable_set(:@log_file, log_file)
29
27
  des.instance_variable_set(:@logger, self)
28
+
29
+ define_log_method
30
+
30
31
  class << des
31
32
  alias_method :orig_sputs, :sputs
32
33
  private :orig_sputs
33
34
 
34
- def sputs(str)
35
- orig_sputs(str)
36
- to_log = ">>#{str.inspect}\n"
37
- @log_file.info(to_log)
38
- if str =~ /\A:?\S+ (#\S+) .*/
39
- @logger.channel_log(Regexp.last_match(1).downcase, to_log)
40
- end
41
- str
42
- end
43
-
44
35
  alias_method :orig_sgets, :sgets
45
36
  private :orig_sgets
46
-
47
- def sgets
48
- str = orig_sgets
49
- to_log = "<<#{str.inspect}\n"
50
- @log_file.info(to_log)
51
- if str =~ /\A:?\S+ \S+ (#\S+) .*/
52
- @logger.channel_log(Regexp.last_match(1).downcase, to_log)
53
- end
54
- str
55
- end
56
37
  end
38
+ des.extend(Methods)
57
39
  end
58
40
 
59
41
  def channel_log(channel, to_log)
@@ -62,16 +44,42 @@ module Balotelli
62
44
  end
63
45
  end
64
46
 
65
- private
47
+ module Methods
48
+ def sgets
49
+ str = orig_sgets
50
+ to_log = "<<#{str.inspect}\n"
51
+ @log_file.info(to_log)
52
+ if str =~ /\A:?\S+ \S+ (#\S+) .*/
53
+ @logger.channel_log(Regexp.last_match(1).downcase, to_log)
54
+ end
55
+ str
56
+ end
57
+
58
+ def sputs(str)
59
+ orig_sputs(str)
60
+ to_log = ">>#{str.inspect}\n"
61
+ @log_file.info(to_log)
62
+ if str =~ /\A:?\S+ (#\S+) .*/
63
+ @logger.channel_log(Regexp.last_match(1).downcase, to_log)
64
+ end
65
+ str
66
+ end
67
+ end
66
68
 
67
69
  def define_log_method
70
+ define_method :log_file do |str|
71
+ class_variable_get(:@log_file)
72
+ end
73
+
68
74
  define_method :log do |str|
69
75
  log_string = "LOG: #{str.inspect}\n"
70
- __LOG_FILE__.info log_string
76
+ log_file.info log_string
71
77
  puts log_string
72
78
  end
73
79
  end
74
80
 
81
+ private
82
+
75
83
  def channel_log_file(channel)
76
84
  "#{channel.tr('#', '')}_logs.log"
77
85
  end
@@ -1,12 +1,15 @@
1
1
  module Balotelli
2
2
  module Core
3
3
  class Join
4
- attr_reader :user, :channel, :user_nick, :responder
5
- def initialize(metadata)
6
- @user = metadata[:user]
4
+ attr_reader :user, :channel, :user_nick
5
+ def initialize(user, channel)
6
+ @user = user
7
7
  @user_nick = Core::Utils.nick_from_user(@user)
8
- @channel = metadata[:channel]
9
- @responder = @channel
8
+ @channel = channel
9
+ end
10
+
11
+ def responder
12
+ @channel
10
13
  end
11
14
  end
12
15
  end
@@ -2,16 +2,12 @@ module Balotelli
2
2
  module Core
3
3
  class PrivMsg
4
4
  attr_reader :user, :channel, :content, :user_nick, :responder
5
- def initialize(metadata, privacy = false)
6
- @user = metadata[:user]
5
+ def initialize(user, channel, message, privacy = false)
6
+ @user = user
7
7
  @user_nick = @user.match(/\A(\S+)!.*/)[1]
8
- @channel = metadata[:channel]
9
- @content = metadata[:message]
10
- @responder = if privacy
11
- @user_nick
12
- else
13
- @channel
14
- end
8
+ @channel = channel
9
+ @content = message
10
+ @responder = privacy ? @user_nick : @channel
15
11
  end
16
12
  end
17
13
  end
@@ -38,7 +38,7 @@ module Balotelli
38
38
 
39
39
  def match(str, priv = false)
40
40
  if (m = @routes.detect { |k, v| match?(str, k) && v[priv] })
41
- [m[0], m[1][priv], str]
41
+ [m[0], m[1][priv]]
42
42
  end
43
43
  end
44
44
  end
@@ -14,6 +14,14 @@ module Balotelli
14
14
  def table_regex(nick)
15
15
  /\A:?(\S+) \d+ #{nick} \S (#\S+) :?(.*)/
16
16
  end
17
+
18
+ module ClassVariableGetter
19
+ def class_variable_get(*args)
20
+ args.each do |s|
21
+ define_method(s) { self.class.instance_variable_get("@#{s}") }
22
+ end
23
+ end
24
+ end
17
25
  end
18
26
  end
19
27
  end
@@ -1,3 +1,3 @@
1
1
  module Balotelli
2
- VERSION = '0.4.1'.freeze
2
+ VERSION = '0.4.2.1'.freeze
3
3
  end
@@ -20,7 +20,7 @@ class RouterTest < Minitest::Test
20
20
  def test_it_mathes_routes
21
21
  lam = -> { puts 'kk' }
22
22
  @route.on %r{ono}i, private: true, &lam
23
- assert @route.match('onOmatopeja', true) == [%r{ono}i, [lam, @route], 'onOmatopeja']
23
+ assert @route.match('onOmatopeja', true) == [%r{ono}i, [lam, @route]]
24
24
  refute @route.match('onOmatopeja', false)
25
25
  @route.on 'asdf', &lam
26
26
  assert @route.match('asdf')
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class SampleClass
4
+ extend Balotelli::Core::Utils::ClassVariableGetter
5
+ class_variable_get :one, :string
6
+
7
+ def initialize
8
+ end
9
+ end
10
+
11
+ class ClassVariableGetterTest < Minitest::Test
12
+ def setup
13
+ @instance = SampleClass.new
14
+ SampleClass.instance_variable_set(:@one, 1)
15
+ SampleClass.instance_variable_set(:@string, 'string')
16
+ end
17
+
18
+ def test_it_defines_methods
19
+ assert @instance.one == 1
20
+ assert @instance.string == 'string'
21
+ end
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: balotelli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Henryk Bartkowiak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2016-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,6 +86,7 @@ files:
86
86
  - test/lib/balotelli/config_test.rb
87
87
  - test/lib/balotelli/core/irc_test.rb
88
88
  - test/lib/balotelli/core/router_test.rb
89
+ - test/lib/balotelli/core/utils_test.rb
89
90
  - test/lib/balotelli/special_one.rb
90
91
  - test/lib/balotelli/xyz.rb
91
92
  - test/test_helper.rb
@@ -120,6 +121,7 @@ test_files:
120
121
  - test/lib/balotelli/config_test.rb
121
122
  - test/lib/balotelli/core/irc_test.rb
122
123
  - test/lib/balotelli/core/router_test.rb
124
+ - test/lib/balotelli/core/utils_test.rb
123
125
  - test/lib/balotelli/special_one.rb
124
126
  - test/lib/balotelli/xyz.rb
125
127
  - test/test_helper.rb