ircparser 0.3.2 → 0.4.0
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 +4 -4
- data/lib/ircparser.rb +1 -1
- data/lib/ircparser/message.rb +52 -28
- data/test/test_message.rb +37 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92dadfe64e680ca4a8e87efc3bbf8f9b323ebf52
|
4
|
+
data.tar.gz: 27e1d86d7da77c90fbe766e43a365387c752ad6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2a992ef089e0250747811676135662d1b1f9f5645611a817716b9769d5bd9651ea0c02c3ae93807988d34e1f2c8cda23051d649ec2ca1d4318dd8ebd53feba0
|
7
|
+
data.tar.gz: 5385967062ce610246d25cc3296f2f0656a278ba653103684c291419551310121a92c01875062728bdfce2f2f97a42b1fe15c97449353a3d7b3d822c43c2bfad
|
data/lib/ircparser.rb
CHANGED
data/lib/ircparser/message.rb
CHANGED
@@ -39,7 +39,7 @@ module IRCParser
|
|
39
39
|
# Public: The source of this command or nil if unsourced.
|
40
40
|
attr_reader :source
|
41
41
|
|
42
|
-
# Public: A hash of IRCv3
|
42
|
+
# Public: A hash of IRCv3 message tags.
|
43
43
|
attr_reader :tags
|
44
44
|
|
45
45
|
# Public: Initialize a new message.
|
@@ -59,33 +59,38 @@ module IRCParser
|
|
59
59
|
raise IRCParser::Error.new(line), "line is not a string" unless line.is_a? String
|
60
60
|
|
61
61
|
# Split the message up into an array of tokens.
|
62
|
-
|
63
|
-
|
62
|
+
buffer = line.clone
|
63
|
+
current_token = __get_token buffer
|
64
64
|
components = Hash.new
|
65
65
|
|
66
|
-
# Have we encountered IRCv3
|
66
|
+
# Have we encountered IRCv3 message tags?
|
67
67
|
components[:tags] = Hash.new
|
68
|
-
if
|
69
|
-
components[:tags] = __parse_tags
|
70
|
-
|
68
|
+
if current_token != nil && current_token[0] == '@'
|
69
|
+
components[:tags] = __parse_tags current_token
|
70
|
+
current_token = __get_token buffer
|
71
71
|
end
|
72
72
|
|
73
73
|
# Have we encountered the source of this message?
|
74
|
-
if
|
75
|
-
components[:source] = IRCParser::Source.new
|
76
|
-
|
74
|
+
if current_token != nil && current_token[0] == ':'
|
75
|
+
components[:source] = IRCParser::Source.new current_token[1..-1]
|
76
|
+
current_token = __get_token buffer
|
77
77
|
end
|
78
78
|
|
79
79
|
# The command parameter is mandatory.
|
80
|
-
if
|
81
|
-
components[:command] =
|
82
|
-
|
80
|
+
if current_token != nil
|
81
|
+
components[:command] = current_token.upcase
|
82
|
+
current_token = __get_final_token buffer
|
83
83
|
else
|
84
84
|
raise IRCParser::Error.new(line), 'message is missing the command name'
|
85
85
|
end
|
86
86
|
|
87
87
|
# Try to parse all of the remaining parameters.
|
88
|
-
components[:parameters] =
|
88
|
+
components[:parameters] = Array.new
|
89
|
+
while current_token != nil
|
90
|
+
components[:parameters] << current_token
|
91
|
+
current_token = __get_final_token buffer
|
92
|
+
end
|
93
|
+
|
89
94
|
return IRCParser::Message.new components
|
90
95
|
end
|
91
96
|
|
@@ -120,20 +125,34 @@ module IRCParser
|
|
120
125
|
|
121
126
|
private
|
122
127
|
|
123
|
-
# Internal:
|
128
|
+
# Internal: Retrieves a space delimited token from the buffer.
|
124
129
|
#
|
125
|
-
#
|
126
|
-
def self.
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
end
|
134
|
-
parameters << token
|
130
|
+
# data - The buffer to retrieve the token from.
|
131
|
+
def self.__get_token buffer
|
132
|
+
return nil if buffer.empty?
|
133
|
+
position = buffer.index ' '
|
134
|
+
if position == nil
|
135
|
+
token = buffer.clone
|
136
|
+
buffer.clear
|
137
|
+
return token
|
135
138
|
end
|
136
|
-
|
139
|
+
token = buffer.slice! 0...position
|
140
|
+
position = buffer.index /\S+/
|
141
|
+
buffer.slice! 0...position
|
142
|
+
return token
|
143
|
+
end
|
144
|
+
|
145
|
+
# Internal: Retrieves a space delimited token that may be a <trailing> parameter.
|
146
|
+
#
|
147
|
+
# data - The buffer to retrieve the token from.
|
148
|
+
def self.__get_final_token buffer
|
149
|
+
return nil if buffer.empty?
|
150
|
+
if buffer[0] == ':'
|
151
|
+
token = buffer[1..-1]
|
152
|
+
buffer.clear
|
153
|
+
return token
|
154
|
+
end
|
155
|
+
return __get_token buffer
|
137
156
|
end
|
138
157
|
|
139
158
|
# Internal: Parse tags from network form to a Hash.
|
@@ -159,10 +178,15 @@ module IRCParser
|
|
159
178
|
def __serialize_parameters
|
160
179
|
buffer = String.new
|
161
180
|
@parameters.each_with_index do |parameter, index|
|
181
|
+
trailing = parameter.include? ' '
|
182
|
+
if trailing && index != @parameters.size-1
|
183
|
+
raise IRCParser::Error.new(parameter), "only the last parameter may contain spaces"
|
184
|
+
end
|
185
|
+
|
162
186
|
buffer += ' '
|
163
|
-
if parameter.
|
187
|
+
if trailing || parameter.empty?
|
164
188
|
buffer += ':'
|
165
|
-
buffer +=
|
189
|
+
buffer += parameter
|
166
190
|
break
|
167
191
|
end
|
168
192
|
buffer += parameter
|
data/test/test_message.rb
CHANGED
@@ -130,4 +130,41 @@ describe IRCParser::Message do
|
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
|
+
describe 'when checking we can handle multiple consecutive spaces in a <trailing> parameter' do
|
134
|
+
before do
|
135
|
+
@text = 'COMMAND :param1 param1 '
|
136
|
+
@message = IRCParser::Message.parse @text
|
137
|
+
end
|
138
|
+
it 'should parse the trailing parameter properly' do
|
139
|
+
@message.parameters.size.must_equal 1
|
140
|
+
@message.parameters[0].must_equal 'param1 param1 '
|
141
|
+
end
|
142
|
+
it 'should serialize the trailing parameter properly' do
|
143
|
+
@message.to_s.must_equal @text
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe 'when checking we can handle an empty <trailing> parameter' do
|
148
|
+
before do
|
149
|
+
@text = 'COMMAND :'
|
150
|
+
@message = IRCParser::Message.parse @text
|
151
|
+
end
|
152
|
+
it 'should parse the trailing parameter properly' do
|
153
|
+
@message.parameters.size.must_equal 1
|
154
|
+
@message.parameters[0].must_equal ''
|
155
|
+
end
|
156
|
+
it 'should serialize the trailing parameter properly' do
|
157
|
+
@message.to_s.must_equal @text
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe 'when checking we can handle serialising malformed parameters' do
|
162
|
+
it 'should throw an IRCParser::Error when a non <trailing> parameter contains spaces' do
|
163
|
+
proc {
|
164
|
+
message = IRCParser::Message.new command: 'COMMAND', parameters: [ 'param1 param1', 'param2' ]
|
165
|
+
message.to_s
|
166
|
+
}.must_raise IRCParser::Error
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
133
170
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ircparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter "SaberUK" Powell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: 0.2.5
|
61
|
-
description: A standards compliant parser for the IRCv3
|
61
|
+
description: A standards compliant parser for the IRCv3 message format.
|
62
62
|
email: petpow@saberuk.com
|
63
63
|
executables: []
|
64
64
|
extensions: []
|
@@ -90,8 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.6.
|
93
|
+
rubygems_version: 2.6.12
|
94
94
|
signing_key:
|
95
95
|
specification_version: 4
|
96
|
-
summary: An IRCv3
|
96
|
+
summary: An IRCv3 message parser.
|
97
97
|
test_files: []
|