servent 0.0.0 → 0.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.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/.rspec +3 -0
- data/.rubocop.yml +27 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +56 -30
- data/Gemfile +3 -2
- data/Gemfile.lock +74 -0
- data/LICENSE.txt +1 -1
- data/README.md +45 -16
- data/Rakefile +10 -1
- data/bin/console +1 -1
- data/hypotesis/consumer.rb +38 -0
- data/hypotesis/emitter.ru +58 -0
- data/lib/servent.rb +7 -1
- data/lib/servent/event.rb +67 -0
- data/lib/servent/event_source.rb +106 -0
- data/lib/servent/stream.rb +56 -0
- data/lib/servent/version.rb +1 -1
- data/servent.gemspec +18 -15
- metadata +78 -15
- data/CHANGELOG.md +0 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: afcf04d7de0fbed51a568743c7dad4af879042d5
|
|
4
|
+
data.tar.gz: 95b1d000e7171f9df857952d6e4698b6073aca09
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca999d77e3c6f6236091167bee4e8f815c05af640ff16a990eee5d237187f31f33207811d72bae5b26c68e38e70d63b734fb3bf6386cd68a916e4e756cf90e2a
|
|
7
|
+
data.tar.gz: 1d0b871339cb5f9f281de6522a5bf699f7f2489d33b2feb968c94db512de8b3ca43c45b08326c1c0f079921da7f63a6dfd2f821abe9831ad95e27c8cc09e8f36
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
Exclude:
|
|
3
|
+
- 'bin/**'
|
|
4
|
+
- 'Gemfile'
|
|
5
|
+
- 'Rakefile'
|
|
6
|
+
- 'vendor/**/*'
|
|
7
|
+
|
|
8
|
+
Metrics/LineLength:
|
|
9
|
+
Max: 80
|
|
10
|
+
|
|
11
|
+
Metrics/ClassLength:
|
|
12
|
+
Max: 120
|
|
13
|
+
|
|
14
|
+
Metrics/MethodLength:
|
|
15
|
+
Max: 10
|
|
16
|
+
|
|
17
|
+
Metrics/BlockLength:
|
|
18
|
+
ExcludedMethods: ["describe", "context"]
|
|
19
|
+
|
|
20
|
+
Style/StringLiterals:
|
|
21
|
+
EnforcedStyle: double_quotes
|
|
22
|
+
|
|
23
|
+
Style/BlockDelimiters:
|
|
24
|
+
Enabled: false
|
|
25
|
+
|
|
26
|
+
Style/FrozenStringLiteralComment:
|
|
27
|
+
Enabled: false
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.4.2
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
CHANGED
|
@@ -1,24 +1,41 @@
|
|
|
1
|
-
# Contributor Code of Conduct
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
fostering an open and welcoming community, we pledge to respect all people who
|
|
5
|
-
contribute through reporting issues, posting feature requests, updating
|
|
6
|
-
documentation, submitting pull requests or patches, and other activities.
|
|
3
|
+
## Our Pledge
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
12
22
|
|
|
13
23
|
Examples of unacceptable behavior by participants include:
|
|
14
24
|
|
|
15
|
-
* The use of sexualized language or imagery
|
|
16
|
-
|
|
17
|
-
* Trolling
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
18
28
|
* Public or private harassment
|
|
19
|
-
* Publishing
|
|
20
|
-
|
|
21
|
-
* Other
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
22
39
|
|
|
23
40
|
Project maintainers have the right and responsibility to remove, edit, or
|
|
24
41
|
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
@@ -26,24 +43,33 @@ that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
|
26
43
|
permanently any contributor for other behaviors that they deem inappropriate,
|
|
27
44
|
threatening, offensive, or harmful.
|
|
28
45
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
33
54
|
|
|
34
|
-
|
|
35
|
-
when an individual is representing the project or its community.
|
|
55
|
+
## Enforcement
|
|
36
56
|
|
|
37
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
38
|
-
reported by contacting
|
|
39
|
-
complaints will be reviewed and investigated and will result in a response
|
|
40
|
-
is deemed necessary and appropriate to the circumstances.
|
|
41
|
-
obligated to maintain confidentiality with regard to the reporter of an
|
|
42
|
-
incident.
|
|
58
|
+
reported by contacting the project team at mister.sourcerer@gmail.com.
|
|
59
|
+
All complaints will be reviewed and investigated and will result in a response
|
|
60
|
+
that is deemed necessary and appropriate to the circumstances. The project team
|
|
61
|
+
is obligated to maintain confidentiality with regard to the reporter of an
|
|
62
|
+
incident. Further details of specific enforcement policies may be posted
|
|
63
|
+
separately.
|
|
64
|
+
|
|
65
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
66
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
67
|
+
members of the project's leadership.
|
|
68
|
+
|
|
69
|
+
## Attribution
|
|
43
70
|
|
|
44
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
45
|
-
|
|
46
|
-
[http://contributor-covenant.org/version/1/3/0/][version]
|
|
71
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
72
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
47
73
|
|
|
48
74
|
[homepage]: http://contributor-covenant.org
|
|
49
|
-
[version]: http://contributor-covenant.org/version/1/
|
|
75
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
servent (0.0.1)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
addressable (2.5.2)
|
|
10
|
+
public_suffix (>= 2.0.2, < 4.0)
|
|
11
|
+
ast (2.3.0)
|
|
12
|
+
byebug (9.1.0)
|
|
13
|
+
coderay (1.1.2)
|
|
14
|
+
crack (0.4.3)
|
|
15
|
+
safe_yaml (~> 1.0.0)
|
|
16
|
+
diff-lcs (1.3)
|
|
17
|
+
hashdiff (0.3.7)
|
|
18
|
+
method_source (0.9.0)
|
|
19
|
+
parallel (1.12.0)
|
|
20
|
+
parser (2.4.0.0)
|
|
21
|
+
ast (~> 2.2)
|
|
22
|
+
powerpack (0.1.1)
|
|
23
|
+
pry (0.11.2)
|
|
24
|
+
coderay (~> 1.1.0)
|
|
25
|
+
method_source (~> 0.9.0)
|
|
26
|
+
pry-byebug (3.5.0)
|
|
27
|
+
byebug (~> 9.1)
|
|
28
|
+
pry (~> 0.10)
|
|
29
|
+
public_suffix (3.0.1)
|
|
30
|
+
rainbow (2.2.2)
|
|
31
|
+
rake
|
|
32
|
+
rake (10.5.0)
|
|
33
|
+
rspec (3.7.0)
|
|
34
|
+
rspec-core (~> 3.7.0)
|
|
35
|
+
rspec-expectations (~> 3.7.0)
|
|
36
|
+
rspec-mocks (~> 3.7.0)
|
|
37
|
+
rspec-core (3.7.0)
|
|
38
|
+
rspec-support (~> 3.7.0)
|
|
39
|
+
rspec-expectations (3.7.0)
|
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
41
|
+
rspec-support (~> 3.7.0)
|
|
42
|
+
rspec-mocks (3.7.0)
|
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
44
|
+
rspec-support (~> 3.7.0)
|
|
45
|
+
rspec-support (3.7.0)
|
|
46
|
+
rubocop (0.51.0)
|
|
47
|
+
parallel (~> 1.10)
|
|
48
|
+
parser (>= 2.3.3.1, < 3.0)
|
|
49
|
+
powerpack (~> 0.1)
|
|
50
|
+
rainbow (>= 2.2.2, < 3.0)
|
|
51
|
+
ruby-progressbar (~> 1.7)
|
|
52
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
|
53
|
+
ruby-progressbar (1.9.0)
|
|
54
|
+
safe_yaml (1.0.4)
|
|
55
|
+
unicode-display_width (1.3.0)
|
|
56
|
+
webmock (3.1.0)
|
|
57
|
+
addressable (>= 2.3.6)
|
|
58
|
+
crack (>= 0.3.2)
|
|
59
|
+
hashdiff
|
|
60
|
+
|
|
61
|
+
PLATFORMS
|
|
62
|
+
ruby
|
|
63
|
+
|
|
64
|
+
DEPENDENCIES
|
|
65
|
+
bundler (~> 1.16)
|
|
66
|
+
pry-byebug
|
|
67
|
+
rake (~> 10.0)
|
|
68
|
+
rspec (~> 3.7)
|
|
69
|
+
rubocop
|
|
70
|
+
servent!
|
|
71
|
+
webmock
|
|
72
|
+
|
|
73
|
+
BUNDLED WITH
|
|
74
|
+
1.16.0
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
# Servent
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[<img src="https://travis-ci.com/mistersourcerer/servent.svg?token=aMwiRm3UQ11zdWwMxGgZ&branch=master" />](https://travis-ci.com/mistersourcerer/servent)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Ruby _Server-Sent Events_ client.
|
|
6
|
+
A _EventSource_ Ruby implementation based on the [W3C specification](https://www.w3.org/TR/eventsource).
|
|
7
|
+
|
|
8
|
+
## Early Development [15/11/2017]
|
|
9
|
+
|
|
10
|
+
This is just a first public draft,
|
|
11
|
+
a bunch of changes and lack of documentation
|
|
12
|
+
is to be expected.
|
|
6
13
|
|
|
7
14
|
## Installation
|
|
8
15
|
|
|
@@ -12,30 +19,52 @@ Add this line to your application's Gemfile:
|
|
|
12
19
|
gem 'servent'
|
|
13
20
|
```
|
|
14
21
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
$ bundle
|
|
18
|
-
|
|
19
|
-
Or install it yourself as:
|
|
20
|
-
|
|
21
|
-
$ gem install servent
|
|
22
|
+
## Usage
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
```ruby
|
|
25
|
+
# given that the http://example.org/event-source
|
|
26
|
+
# generates an event like the one below:
|
|
27
|
+
#
|
|
28
|
+
# event: hello_world
|
|
29
|
+
# id: 42
|
|
30
|
+
# data: Omg! Hello World.
|
|
31
|
+
|
|
32
|
+
events = Queue.new
|
|
33
|
+
|
|
34
|
+
event_source = Servent::EventSource.new("http://example.org/event-source")
|
|
35
|
+
event_source.on_message do |message|
|
|
36
|
+
events.push message
|
|
37
|
+
end
|
|
38
|
+
event_source.start
|
|
39
|
+
|
|
40
|
+
while (event = events.pop)
|
|
41
|
+
puts "Event type: #{event.type}"
|
|
42
|
+
puts "Event body: #{event.body}"
|
|
43
|
+
|
|
44
|
+
# Will print:
|
|
45
|
+
#
|
|
46
|
+
# ```
|
|
47
|
+
# Event type: hello_world
|
|
48
|
+
# Event body: Omg! Hello World.
|
|
49
|
+
# ```
|
|
50
|
+
# And wait for the next event to arrive.
|
|
51
|
+
end
|
|
52
|
+
```
|
|
26
53
|
|
|
27
54
|
## Development
|
|
28
55
|
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
57
|
|
|
31
58
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
59
|
|
|
33
60
|
## Contributing
|
|
34
61
|
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
36
|
-
|
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ricardovaleriano/servent. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
37
63
|
|
|
38
64
|
## License
|
|
39
65
|
|
|
40
|
-
The gem is available as open source under the terms of the [MIT License](
|
|
66
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
67
|
+
|
|
68
|
+
## Code of Conduct
|
|
41
69
|
|
|
70
|
+
Everyone interacting in the Servent project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ricardovaleriano/servent/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
1
|
require "bundler/gem_tasks"
|
|
2
|
-
|
|
2
|
+
require "rspec/core/rake_task"
|
|
3
|
+
require "rubocop/rake_task"
|
|
4
|
+
|
|
5
|
+
RuboCop::RakeTask.new do |task|
|
|
6
|
+
task.fail_on_error = false
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
10
|
+
|
|
11
|
+
task default: [:rubocop, :spec]
|
data/bin/console
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
|
3
|
+
require "servent"
|
|
4
|
+
require "pp"
|
|
5
|
+
|
|
6
|
+
#q = Queue.new
|
|
7
|
+
#
|
|
8
|
+
#trap :INT do
|
|
9
|
+
# q << nil
|
|
10
|
+
#end
|
|
11
|
+
|
|
12
|
+
#Thread.new do
|
|
13
|
+
# uri = URI("http://localhost:9292/omg")
|
|
14
|
+
#
|
|
15
|
+
# Net::HTTP.start(uri.host, uri.port, read_timeout: 600) do |http|
|
|
16
|
+
# get = Net::HTTP::Get.new uri
|
|
17
|
+
# get["Accept"] = "text/event-stream"
|
|
18
|
+
# http.request(get) do |response|
|
|
19
|
+
# response.read_body do |chunk|
|
|
20
|
+
# q.push chunk
|
|
21
|
+
# end
|
|
22
|
+
# end
|
|
23
|
+
# q.push nil
|
|
24
|
+
# end
|
|
25
|
+
#end
|
|
26
|
+
|
|
27
|
+
event_source = Servent::EventSource.new("http://localhost:9292/omg")
|
|
28
|
+
event_source.on_message do |message|
|
|
29
|
+
#q.push message
|
|
30
|
+
pp message
|
|
31
|
+
end
|
|
32
|
+
event_source.start.join
|
|
33
|
+
|
|
34
|
+
#while (chunk = q.pop)
|
|
35
|
+
# puts chunk
|
|
36
|
+
#end
|
|
37
|
+
|
|
38
|
+
puts "bye"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require "webrick"
|
|
2
|
+
|
|
3
|
+
class SSEEvent
|
|
4
|
+
def initialize(text, type: "message", id: Time.now.to_f)
|
|
5
|
+
@id = id
|
|
6
|
+
@text = text
|
|
7
|
+
@type = type
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def event
|
|
11
|
+
%(event: #{@type}
|
|
12
|
+
id: #{@id}
|
|
13
|
+
data: {
|
|
14
|
+
data: "id": "#{@id}",
|
|
15
|
+
data: "type": "#{@type}",
|
|
16
|
+
data: "text": "#{@text}"
|
|
17
|
+
data: }
|
|
18
|
+
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
server = WEBrick::HTTPServer.new Port: 9292
|
|
24
|
+
|
|
25
|
+
clients = []
|
|
26
|
+
|
|
27
|
+
server.mount_proc "/" do |_, res|
|
|
28
|
+
r, w = IO.pipe
|
|
29
|
+
clients << w
|
|
30
|
+
|
|
31
|
+
res.content_type = "text/event-stream"
|
|
32
|
+
res.body = r
|
|
33
|
+
res.chunked = true
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
server.mount_proc "/omg" do |_, res|
|
|
37
|
+
r, w = IO.pipe
|
|
38
|
+
clients << w
|
|
39
|
+
|
|
40
|
+
res.content_type = "text/event-stream"
|
|
41
|
+
res.body = r
|
|
42
|
+
res.chunked = true
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
server.mount_proc "/broadcast" do |_, _|
|
|
46
|
+
clients.each do |client|
|
|
47
|
+
Thread.new do
|
|
48
|
+
client << SSEEvent.new("streaming!").event
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
trap :INT do
|
|
54
|
+
clients.each(&:close)
|
|
55
|
+
server.shutdown
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
server.start
|
data/lib/servent.rb
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
module Servent
|
|
2
|
+
class Event
|
|
3
|
+
class InvalidError < StandardError
|
|
4
|
+
attr_reader :event
|
|
5
|
+
|
|
6
|
+
def initialize(event)
|
|
7
|
+
@event = event
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
attr_reader :type, :id, :retry
|
|
12
|
+
|
|
13
|
+
def initialize(event)
|
|
14
|
+
@data = []
|
|
15
|
+
StringIO.open(event) do |io|
|
|
16
|
+
io.each_line { |line| parse_line line }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
raise InvalidError.new(event) if empty?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def data
|
|
23
|
+
@_data ||= @data.join("\n")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def parse_line(line)
|
|
29
|
+
return unless line.include?(Servent::COLON)
|
|
30
|
+
field_name, data = line.split(":")
|
|
31
|
+
normalized_data = remove_first_space(data).chomp
|
|
32
|
+
process_as_field field_name, normalized_data
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def empty?
|
|
36
|
+
data.empty? && type.nil? && id.nil? && @retry.nil?
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def process_as_field(field_name, data)
|
|
40
|
+
return unless KNOWN_EVENTS.include?(field_name)
|
|
41
|
+
field_handler = method("field_#{field_name}")
|
|
42
|
+
field_handler.call data
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def remove_first_space(string)
|
|
46
|
+
return string unless string[0] == "\u{0020}"
|
|
47
|
+
string[1..(string.length - 1)]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def field_event(data)
|
|
51
|
+
@type = data
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def field_id(data)
|
|
55
|
+
@id = data
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def field_retry(data)
|
|
59
|
+
@retry = data.to_i
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def field_data(data)
|
|
63
|
+
@type = "message" if @type.nil?
|
|
64
|
+
@data << data
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
require "servent/stream"
|
|
2
|
+
require "servent/event"
|
|
3
|
+
require "net/http"
|
|
4
|
+
|
|
5
|
+
module Servent
|
|
6
|
+
class EventSource
|
|
7
|
+
attr_reader :ready_state
|
|
8
|
+
|
|
9
|
+
def initialize(url, net_http_options: { read_timeout: 600 })
|
|
10
|
+
@uri = URI(url)
|
|
11
|
+
@net_http_options = net_http_options
|
|
12
|
+
@ready_state = Servent::CONNECTING
|
|
13
|
+
|
|
14
|
+
@open_blocks = []
|
|
15
|
+
@message_blocks = []
|
|
16
|
+
@error_blocks = []
|
|
17
|
+
|
|
18
|
+
@proxy_config = ProxyConfig.new
|
|
19
|
+
yield @proxy_config if block_given?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def start(http_starter = Net::HTTP)
|
|
23
|
+
params = HTTPStartParams.new(@uri, @proxy_config, @net_http_options)
|
|
24
|
+
|
|
25
|
+
Thread.new {
|
|
26
|
+
http_starter.start(*params.parameterize) do |http|
|
|
27
|
+
get = Net::HTTP::Get.new @uri
|
|
28
|
+
headers.each { |header, value| get[header] = value }
|
|
29
|
+
yield http, get if block_given?
|
|
30
|
+
perform_request http, get
|
|
31
|
+
end
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def on_open(&open_block)
|
|
36
|
+
@open_blocks << open_block
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def on_message(&message_block)
|
|
40
|
+
@message_blocks << message_block
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def on_error(&error_block)
|
|
44
|
+
@error_blocks << error_block
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def headers
|
|
50
|
+
{ "Accept" => "text/event-stream" }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def perform_request(http, type)
|
|
54
|
+
http.request type do |response|
|
|
55
|
+
# FIXME: response CAN have more than one mime type
|
|
56
|
+
unless response["Content-Type"] == "text/event-stream"
|
|
57
|
+
@ready_state = Servent::CLOSED
|
|
58
|
+
@error_blocks.each { |block| block.call response, :wrong_mime_type }
|
|
59
|
+
return
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
handle_response response
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def handle_response(response)
|
|
67
|
+
@ready_state = Servent::OPEN
|
|
68
|
+
@open_blocks.each { |block| block.call response }
|
|
69
|
+
response.read_body do |chunk|
|
|
70
|
+
@message_blocks.each { |block| block.call chunk }
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
class ProxyConfig
|
|
76
|
+
attr_accessor :host, :user, :pass
|
|
77
|
+
attr_writer :port
|
|
78
|
+
|
|
79
|
+
def port
|
|
80
|
+
@port.to_i
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def empty?
|
|
84
|
+
@host.nil? && @port.nil? && @user.nil? && @pass.nil?
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def parameterize
|
|
88
|
+
[host, port, user, pass]
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
class HTTPStartParams
|
|
93
|
+
def initialize(uri, proxy_config, options)
|
|
94
|
+
@uri = uri
|
|
95
|
+
@proxy_config = proxy_config
|
|
96
|
+
@options = options
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def parameterize
|
|
100
|
+
params = [@uri.host, @uri.port]
|
|
101
|
+
params += @proxy_config.parameterize unless @proxy_config.empty?
|
|
102
|
+
params << @options
|
|
103
|
+
params
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module Servent
|
|
2
|
+
class Stream
|
|
3
|
+
attr_reader :last_event_id, :reconnection_time
|
|
4
|
+
|
|
5
|
+
def initialize(stream)
|
|
6
|
+
@stream = StringIO.new stream
|
|
7
|
+
@buffer = []
|
|
8
|
+
@events = []
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def parse
|
|
12
|
+
@stream.each_line { |line|
|
|
13
|
+
next if line.start_with?(Servent::COLON)
|
|
14
|
+
handle_line line
|
|
15
|
+
}
|
|
16
|
+
complete_event
|
|
17
|
+
@events
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def handle_line(line)
|
|
23
|
+
# Line is empty:
|
|
24
|
+
# - can be the end of a stream or
|
|
25
|
+
# - can be a stream with multiple events
|
|
26
|
+
if line.strip.chomp.empty?
|
|
27
|
+
complete_event line
|
|
28
|
+
else
|
|
29
|
+
buffer line
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def complete_event(line = nil)
|
|
34
|
+
return if @buffer.empty?
|
|
35
|
+
buffer line unless line.nil?
|
|
36
|
+
@events << create_event
|
|
37
|
+
@buffer = []
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def create_event
|
|
41
|
+
begin
|
|
42
|
+
event = Event.new(@buffer.join("\n"))
|
|
43
|
+
rescue Servent::Event::InvalidError
|
|
44
|
+
return
|
|
45
|
+
end
|
|
46
|
+
@last_event_id = event.id
|
|
47
|
+
@reconnection_time = event.retry
|
|
48
|
+
event
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def buffer(line)
|
|
52
|
+
# TODO: if this line defines a new type, than also #complete_event
|
|
53
|
+
@buffer << line
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
data/lib/servent/version.rb
CHANGED
data/servent.gemspec
CHANGED
|
@@ -1,30 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
require
|
|
3
|
+
require "servent/version"
|
|
5
4
|
|
|
6
5
|
Gem::Specification.new do |spec|
|
|
7
6
|
spec.name = "servent"
|
|
8
7
|
spec.version = Servent::VERSION
|
|
9
8
|
spec.authors = ["Ricardo Valeriano"]
|
|
10
|
-
spec.email = ["
|
|
9
|
+
spec.email = ["mister.sourcerer@gmail.com"]
|
|
11
10
|
|
|
12
|
-
spec.summary = %(
|
|
13
|
-
spec.description = %(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
spec.summary = %(Ruby Server-Sent Events client.)
|
|
12
|
+
spec.description = %(
|
|
13
|
+
Provides a pure Ruby client implementation
|
|
14
|
+
for Server-Sent Events
|
|
15
|
+
as specified in https://www.w3.org/TR/eventsource/.
|
|
16
|
+
)
|
|
17
|
+
spec.homepage = "https://github.com/mistersourcerer/servent"
|
|
17
18
|
spec.license = "MIT"
|
|
18
19
|
|
|
19
|
-
spec.files = `git ls-files -z`.split("\x0").reject
|
|
20
|
-
f.match(%r{^(features)/})
|
|
21
|
-
|
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
|
22
|
+
end
|
|
22
23
|
spec.bindir = "exe"
|
|
23
24
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
24
25
|
spec.require_paths = ["lib"]
|
|
25
26
|
|
|
26
|
-
spec.
|
|
27
|
-
|
|
28
|
-
spec.add_development_dependency "bundler", "~> 1.11"
|
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
28
|
+
spec.add_development_dependency "pry-byebug"
|
|
29
29
|
spec.add_development_dependency "rake", "~> 10.0"
|
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.7"
|
|
31
|
+
spec.add_development_dependency "rubocop"
|
|
32
|
+
spec.add_development_dependency "webmock"
|
|
30
33
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: servent
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ricardo Valeriano
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2017-11-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -16,14 +16,28 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '1.
|
|
19
|
+
version: '1.16'
|
|
20
20
|
type: :development
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '1.
|
|
26
|
+
version: '1.16'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: pry-byebug
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
27
41
|
- !ruby/object:Gem::Dependency
|
|
28
42
|
name: rake
|
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -38,29 +52,78 @@ dependencies:
|
|
|
38
52
|
- - "~>"
|
|
39
53
|
- !ruby/object:Gem::Version
|
|
40
54
|
version: '10.0'
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.7'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.7'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rubocop
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: webmock
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
description: "\n Provides a pure Ruby client implementation\n for Server-Sent
|
|
98
|
+
Events\n as specified in https://www.w3.org/TR/eventsource/.\n "
|
|
45
99
|
email:
|
|
46
|
-
-
|
|
100
|
+
- mister.sourcerer@gmail.com
|
|
47
101
|
executables: []
|
|
48
102
|
extensions: []
|
|
49
103
|
extra_rdoc_files: []
|
|
50
104
|
files:
|
|
51
105
|
- ".gitignore"
|
|
52
|
-
-
|
|
106
|
+
- ".rspec"
|
|
107
|
+
- ".rubocop.yml"
|
|
108
|
+
- ".ruby-version"
|
|
109
|
+
- ".travis.yml"
|
|
53
110
|
- CODE_OF_CONDUCT.md
|
|
54
111
|
- Gemfile
|
|
112
|
+
- Gemfile.lock
|
|
55
113
|
- LICENSE.txt
|
|
56
114
|
- README.md
|
|
57
115
|
- Rakefile
|
|
58
116
|
- bin/console
|
|
59
117
|
- bin/setup
|
|
118
|
+
- hypotesis/consumer.rb
|
|
119
|
+
- hypotesis/emitter.ru
|
|
60
120
|
- lib/servent.rb
|
|
121
|
+
- lib/servent/event.rb
|
|
122
|
+
- lib/servent/event_source.rb
|
|
123
|
+
- lib/servent/stream.rb
|
|
61
124
|
- lib/servent/version.rb
|
|
62
125
|
- servent.gemspec
|
|
63
|
-
homepage:
|
|
126
|
+
homepage: https://github.com/mistersourcerer/servent
|
|
64
127
|
licenses:
|
|
65
128
|
- MIT
|
|
66
129
|
metadata: {}
|
|
@@ -70,9 +133,9 @@ require_paths:
|
|
|
70
133
|
- lib
|
|
71
134
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
72
135
|
requirements:
|
|
73
|
-
- - "
|
|
136
|
+
- - ">="
|
|
74
137
|
- !ruby/object:Gem::Version
|
|
75
|
-
version: '
|
|
138
|
+
version: '0'
|
|
76
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
140
|
requirements:
|
|
78
141
|
- - ">="
|
|
@@ -80,8 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
80
143
|
version: '0'
|
|
81
144
|
requirements: []
|
|
82
145
|
rubyforge_project:
|
|
83
|
-
rubygems_version: 2.
|
|
146
|
+
rubygems_version: 2.6.13
|
|
84
147
|
signing_key:
|
|
85
148
|
specification_version: 4
|
|
86
|
-
summary:
|
|
149
|
+
summary: Ruby Server-Sent Events client.
|
|
87
150
|
test_files: []
|
data/CHANGELOG.md
DELETED