event_stream_parser 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -9
- data/lib/event_stream_parser/version.rb +1 -1
- data/lib/event_stream_parser.rb +16 -18
- metadata +6 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6c523a863200d11f4277cd2def00738e53d40adcae96a8bf5e0dc8e4e734457
|
4
|
+
data.tar.gz: 684c33aaf90439ce864e2b9c063f7ad487866b7749e62e5edb3f0bcbb20b777b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4527ea1befd90840ca9640b19f37f1c0232aae9ce9eab2a68612e6ff2db1d3842fdf0041bb6f6e2778950ffb86c3bd0c428d5867456e6aeb32ba01def56a154
|
7
|
+
data.tar.gz: 4b9a408e6f8eb325a52d35756af38087dcac36cf82b43ea06bd1af6f4287c45a9125d0e1d894382c2513e57e3c4239071aa7f915a7fbe1a6c9bd74dc735e0939
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# event_stream_parser
|
2
2
|
|
3
|
+
[![Gem (including prereleases)](https://img.shields.io/gem/v/event_stream_parser)](https://rubygems.org/gems/event_stream_parser)
|
4
|
+
[![CI](https://github.com/Shopify/event_stream_parser/actions/workflows/ci.yml/badge.svg)](https://github.com/Shopify/event_stream_parser/actions/workflows/ci.yml)
|
5
|
+
|
3
6
|
A lightweight, fully spec-compliant parser for the
|
4
7
|
[event stream](https://www.w3.org/TR/eventsource/) format.
|
5
8
|
|
@@ -35,23 +38,23 @@ gem install event_stream_parser
|
|
35
38
|
|
36
39
|
## Usage
|
37
40
|
|
38
|
-
Create a new Parser
|
41
|
+
Create a new Parser:
|
39
42
|
|
40
43
|
```rb
|
41
44
|
parser = EventStreamParser::Parser.new
|
42
|
-
|
43
|
-
parser.feed do |type, data, id, reconnection_time|
|
44
|
-
puts "Event type: #{type}"
|
45
|
-
puts "Event data: #{data}"
|
46
|
-
puts "Event id: #{id}"
|
47
|
-
puts "Reconnection time: #{reconnection_time}"
|
48
|
-
end
|
49
45
|
```
|
50
46
|
|
51
47
|
Then, feed it chunks as they come in:
|
52
48
|
|
53
49
|
```rb
|
54
|
-
do_something_that_yields_chunks do
|
50
|
+
do_something_that_yields_chunks do |chunk|
|
51
|
+
parser.feed(chunk) do |type, data, id, reconnection_time|
|
52
|
+
puts "Event type: #{type}"
|
53
|
+
puts "Event data: #{data}"
|
54
|
+
puts "Event id: #{id}"
|
55
|
+
puts "Reconnection time: #{reconnection_time}"
|
56
|
+
end
|
57
|
+
end
|
55
58
|
```
|
56
59
|
|
57
60
|
Or use the `stream` method to generate a proc that you can pass to a chunk
|
data/lib/event_stream_parser.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'event_stream_parser/version'
|
4
4
|
|
5
5
|
module EventStreamParser
|
6
6
|
##
|
@@ -18,12 +18,12 @@ module EventStreamParser
|
|
18
18
|
# event ID buffer must be associated with it. They must be initialized to
|
19
19
|
# the empty string.
|
20
20
|
#
|
21
|
-
@data_buffer = +
|
22
|
-
@event_type_buffer = +
|
23
|
-
@last_event_id_buffer = +
|
21
|
+
@data_buffer = +''
|
22
|
+
@event_type_buffer = +''
|
23
|
+
@last_event_id_buffer = +''
|
24
24
|
|
25
25
|
@reconnection_time = nil
|
26
|
-
@buffer = +
|
26
|
+
@buffer = +''
|
27
27
|
@last_delimiter = nil
|
28
28
|
end
|
29
29
|
|
@@ -38,9 +38,7 @@ module EventStreamParser
|
|
38
38
|
# followed by a U+000A LINE FEED (LF) character being the ways in which a
|
39
39
|
# line can end.
|
40
40
|
#
|
41
|
-
if @last_delimiter == "\r"
|
42
|
-
@buffer.delete_prefix!("\n")
|
43
|
-
end
|
41
|
+
@buffer.delete_prefix!("\n") if @last_delimiter == "\r"
|
44
42
|
|
45
43
|
while (line = @buffer.slice!(/.*?(?<delim>\r\n|\r|\n)/))
|
46
44
|
line.chomp!
|
@@ -68,7 +66,7 @@ module EventStreamParser
|
|
68
66
|
##
|
69
67
|
# If the line is empty (a blank line)
|
70
68
|
#
|
71
|
-
when
|
69
|
+
when ''
|
72
70
|
##
|
73
71
|
# Dispatch the event, as defined below.
|
74
72
|
#
|
@@ -106,7 +104,7 @@ module EventStreamParser
|
|
106
104
|
# Process the field using the steps described below, using the whole line
|
107
105
|
# as the field name, and the empty string as the field value.
|
108
106
|
#
|
109
|
-
process_field(line,
|
107
|
+
process_field(line, '')
|
110
108
|
end
|
111
109
|
end
|
112
110
|
|
@@ -120,7 +118,7 @@ module EventStreamParser
|
|
120
118
|
##
|
121
119
|
# If the field name is "event"
|
122
120
|
#
|
123
|
-
when
|
121
|
+
when 'event'
|
124
122
|
##
|
125
123
|
# Set the event type buffer to field value.
|
126
124
|
#
|
@@ -128,7 +126,7 @@ module EventStreamParser
|
|
128
126
|
##
|
129
127
|
# If the field name is "data"
|
130
128
|
#
|
131
|
-
when
|
129
|
+
when 'data'
|
132
130
|
##
|
133
131
|
# Append the field value to the data buffer, then append a single U+000A
|
134
132
|
# LINE FEED (LF) character to the data buffer.
|
@@ -137,7 +135,7 @@ module EventStreamParser
|
|
137
135
|
##
|
138
136
|
# If the field name is "id"
|
139
137
|
#
|
140
|
-
when
|
138
|
+
when 'id'
|
141
139
|
##
|
142
140
|
# If the field value does not contain U+0000 NULL, then set the last event
|
143
141
|
# ID buffer to the field value. Otherwise, ignore the field.
|
@@ -146,7 +144,7 @@ module EventStreamParser
|
|
146
144
|
##
|
147
145
|
# If the field name is "retry"
|
148
146
|
#
|
149
|
-
when
|
147
|
+
when 'retry'
|
150
148
|
##
|
151
149
|
# If the field value consists of only ASCII digits, then interpret the
|
152
150
|
# field value as an integer in base ten, and set the event stream's
|
@@ -188,8 +186,8 @@ module EventStreamParser
|
|
188
186
|
# event type buffer to the empty string and return.
|
189
187
|
#
|
190
188
|
if @data_buffer.empty?
|
191
|
-
@data_buffer = +
|
192
|
-
@event_type_buffer = +
|
189
|
+
@data_buffer = +''
|
190
|
+
@event_type_buffer = +''
|
193
191
|
return
|
194
192
|
end
|
195
193
|
##
|
@@ -213,8 +211,8 @@ module EventStreamParser
|
|
213
211
|
##
|
214
212
|
# 7. Set the data buffer and the event type buffer to the empty string.
|
215
213
|
#
|
216
|
-
@data_buffer = +
|
217
|
-
@event_type_buffer = +
|
214
|
+
@data_buffer = +''
|
215
|
+
@event_type_buffer = +''
|
218
216
|
|
219
217
|
yield type, data, id, @reconnection_time
|
220
218
|
end
|
metadata
CHANGED
@@ -1,43 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: event_stream_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ates Goral
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: minitest
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '5.0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '5.0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '13.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '13.0'
|
11
|
+
date: 2023-11-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
41
13
|
description:
|
42
14
|
email:
|
43
15
|
- ates.goral@shopify.com
|
@@ -58,6 +30,7 @@ metadata:
|
|
58
30
|
changelog_uri: https://github.com/Shopify/event_stream_parser/blob/main/CHANGELOG.md
|
59
31
|
source_code_uri: https://github.com/Shopify/event_stream_parser
|
60
32
|
allowed_push_host: https://rubygems.org
|
33
|
+
rubygems_mfa_required: 'true'
|
61
34
|
post_install_message:
|
62
35
|
rdoc_options: []
|
63
36
|
require_paths:
|
@@ -66,14 +39,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
39
|
requirements:
|
67
40
|
- - ">="
|
68
41
|
- !ruby/object:Gem::Version
|
69
|
-
version: 2.
|
42
|
+
version: 2.6.0
|
70
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
44
|
requirements:
|
72
45
|
- - ">="
|
73
46
|
- !ruby/object:Gem::Version
|
74
47
|
version: 1.3.7
|
75
48
|
requirements: []
|
76
|
-
rubygems_version: 3.4.
|
49
|
+
rubygems_version: 3.4.21
|
77
50
|
signing_key:
|
78
51
|
specification_version: 4
|
79
52
|
summary: A spec-compliant event stream parser
|