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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da792a06cf218e905c4c4cd558b1624edfd17ef7
4
- data.tar.gz: 07a24306c369d4a1e509522121b4fb93354f7a9f
3
+ metadata.gz: 92dadfe64e680ca4a8e87efc3bbf8f9b323ebf52
4
+ data.tar.gz: 27e1d86d7da77c90fbe766e43a365387c752ad6f
5
5
  SHA512:
6
- metadata.gz: 302fe93bd63fc4598a98e3c3014632e3f506a28ecd5bcedcab29077addb522842c54fccd71e792171508b91f3e7d0fde12314ffd8ec48ce53d166d75f2a5f094
7
- data.tar.gz: 245d04b36d85a0d50735a10526afc33cc1f446ae97aaef8127c912cbf117e835c92a6fc51a1292686fbf8a9ec187484aaa7196656c2423e67e5f3678232b5db6
6
+ metadata.gz: b2a992ef089e0250747811676135662d1b1f9f5645611a817716b9769d5bd9651ea0c02c3ae93807988d34e1f2c8cda23051d649ec2ca1d4318dd8ebd53feba0
7
+ data.tar.gz: 5385967062ce610246d25cc3296f2f0656a278ba653103684c291419551310121a92c01875062728bdfce2f2f97a42b1fe15c97449353a3d7b3d822c43c2bfad
data/lib/ircparser.rb CHANGED
@@ -16,7 +16,7 @@
16
16
  module IRCParser
17
17
 
18
18
  # Public: The version of IRCParser in use.
19
- VERSION = '0.3.2'
19
+ VERSION = '0.4.0'
20
20
  end
21
21
 
22
22
  require_relative 'ircparser/error'
@@ -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.2 message tags.
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
- tokens = line.split ' '
63
- current_index = 0
62
+ buffer = line.clone
63
+ current_token = __get_token buffer
64
64
  components = Hash.new
65
65
 
66
- # Have we encountered IRCv3.2 message tags?
66
+ # Have we encountered IRCv3 message tags?
67
67
  components[:tags] = Hash.new
68
- if current_index < tokens.size && tokens[current_index][0] == '@'
69
- components[:tags] = __parse_tags tokens[current_index]
70
- current_index += 1
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 current_index < tokens.size && tokens[current_index][0] == ':'
75
- components[:source] = IRCParser::Source.new tokens[current_index][1..-1]
76
- current_index += 1
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 current_index < tokens.size
81
- components[:command] = tokens[current_index]
82
- current_index += 1
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] = __parse_parameters tokens[current_index..-1]
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: Parse parameters from network form to an Array.
128
+ # Internal: Retrieves a space delimited token from the buffer.
124
129
  #
125
- # token - A list of tags in network form.
126
- def self.__parse_parameters tokens
127
- parameters = Array.new
128
- tokens.each_with_index do |token, index|
129
- if token[0] == ':'
130
- last_token = tokens[index..-1].join ' '
131
- parameters << last_token[1..-1]
132
- break
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
- return parameters
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.include? ' '
187
+ if trailing || parameter.empty?
164
188
  buffer += ':'
165
- buffer += parameters[index..-1].join ' '
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.3.2
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-01-09 00:00:00.000000000 Z
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.2 message format.
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.8
93
+ rubygems_version: 2.6.12
94
94
  signing_key:
95
95
  specification_version: 4
96
- summary: An IRCv3.2 message parser.
96
+ summary: An IRCv3 message parser.
97
97
  test_files: []