Skip to content

Commit 8c84a4a

Browse files
authored
docs: Some minor updates and corrections to the README and examples (#94)
Signed-off-by: Daniel Azuma <[email protected]>
1 parent 17a3592 commit 8c84a4a

File tree

8 files changed

+82
-24
lines changed

8 files changed

+82
-24
lines changed

.toys/cucumber.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def run
2020
end
2121

2222
def setup_features
23-
remote_features = git_cache.find "https://github.com/cloudevents/conformance", path: "features"
24-
local_features = File.join context_directory, "features", "conformance"
25-
rm_rf local_features
26-
cp_r remote_features, local_features
23+
remote_features = git_cache.find("https://github.com/cloudevents/conformance", path: "features")
24+
local_features = File.join(context_directory, "features", "conformance")
25+
rm_rf(local_features)
26+
cp_r(remote_features, local_features)
2727
end

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ source "https://rubygems.org"
44
gemspec
55

66
gem "cucumber", "~> 9.2"
7+
gem "logger", "~> 1.4"
78
gem "minitest", "~> 5.25"
89
gem "minitest-focus", "~> 1.4"
910
gem "minitest-rg", "~> 5.3"
1011
gem "rack", "~> 3.2"
1112
gem "redcarpet", "~> 3.6" unless ::RUBY_PLATFORM == "java"
1213
gem "rubocop", "~> 1.81"
14+
gem "toys-core", "~> 0.17"
1315
gem "webrick", "~> 1.9"
1416
gem "yard", "~> 0.9.37"

README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ Features:
1313
JSON Batch Format.
1414
* Support for sending and receiving CloudEvents via HTTP Bindings.
1515
* Supports the [CloudEvent 0.3](https://github.com/cloudevents/spec/tree/v0.3)
16-
and [CloudEvents 1.0](https://github.com/cloudevents/spec/tree/v1.0.1)
16+
and [CloudEvents 1.0](https://github.com/cloudevents/spec/tree/v1.0.2)
1717
specifications.
1818
* Extensible to additional formats and protocol bindings, and future
1919
specification versions.
20-
* Compatible with Ruby 2.5 or later, or JRuby 9.2.x or later. No runtime gem
20+
* Compatible with Ruby 2.7 or later, or JRuby 9.2.x or later. No runtime gem
2121
dependencies.
2222

2323
## Quickstart
@@ -35,8 +35,10 @@ A simple [Sinatra](https://sinatrarb.com) app that receives CloudEvents:
3535
```ruby
3636
# examples/server/Gemfile
3737
source "https://rubygems.org"
38-
gem "cloud_events", "~> 0.6"
39-
gem "sinatra", "~> 2.0"
38+
gem "cloud_events", "~> 0.8"
39+
gem "sinatra", "~> 4.0"
40+
gem "rackup"
41+
gem "puma"
4042
```
4143

4244
```ruby
@@ -46,9 +48,9 @@ require "cloud_events"
4648

4749
cloud_events_http = CloudEvents::HttpBinding.default
4850

49-
post "/" do
50-
event = cloud_events_http.decode_event request.env
51-
logger.info "Received CloudEvent: #{event.to_h}"
51+
post("/") do
52+
event = cloud_events_http.decode_event(request.env)
53+
logger.info("Received CloudEvent: #{event.to_h}")
5254
end
5355
```
5456

@@ -59,7 +61,7 @@ A simple Ruby script that sends a CloudEvent:
5961
```ruby
6062
# examples/client/Gemfile
6163
source "https://rubygems.org"
62-
gem "cloud_events", "~> 0.6"
64+
gem "cloud_events", "~> 0.8"
6365
```
6466

6567
```ruby
@@ -69,17 +71,18 @@ require "net/http"
6971
require "uri"
7072

7173
data = { message: "Hello, CloudEvents!" }
72-
event = CloudEvents::Event.create \
74+
event = CloudEvents::Event.create(
7375
spec_version: "1.0",
7476
id: "1234-1234-1234",
7577
source: "/mycontext",
7678
type: "com.example.someevent",
7779
data_content_type: "application/json",
7880
data: data
81+
)
7982

8083
cloud_events_http = CloudEvents::HttpBinding.default
81-
headers, body = cloud_events_http.encode_event event
82-
Net::HTTP.post URI("http://localhost:4567"), body, headers
84+
headers, body = cloud_events_http.encode_event(event)
85+
Net::HTTP.post(URI("http://localhost:4567"), body, headers)
8386
```
8487

8588
### Putting it together
@@ -108,7 +111,8 @@ Hit `CTRL+C` to stop the server.
108111

109112
## Contributing
110113

111-
Bug reports and pull requests are welcome on GitHub at https://github.com/cloudevents/sdk-ruby.
114+
Bug reports and pull requests are welcome on GitHub at
115+
https://github.com/cloudevents/sdk-ruby.
112116

113117
### Development
114118

@@ -145,11 +149,15 @@ toys test --help
145149

146150
### Code style
147151

148-
Ruby code in this library generally follows the
149-
[Google Ruby Style Guide](https://github.com/googleapis/ruby-style), which is
150-
based on "Seattle Style" Ruby.
152+
Ruby code style is enforced by Rubocop rules. We've left the configuration
153+
largely on the Rubocop defaults, with a few exceptions, notably:
151154

152-
Style is enforced by Rubocop rules. You can run rubocop directly using the
155+
* We prefer double-quoted strings rather than single-quoted strings.
156+
* We prefer trailing commas in multi-line array and hash literals.
157+
* Line length limit is 120
158+
* We've loosened a few additional checks that we felt were not helpful.
159+
160+
You can run rubocop directly using the
153161
`rubocop` binary:
154162

155163
```sh

RELEASING.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ locally. See `toys release request --help` and `toys release perform --help`
2222
for more information.
2323

2424
If a release fails, you may need to delete the release tag before retrying.
25-

examples/client/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
22

33
source "https://rubygems.org"
4+
45
gem "cloud_events", path: "../.."

examples/server/Gemfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
source "https://rubygems.org"
4+
45
gem "cloud_events", path: "../.."
5-
gem "sinatra", "~> 2.0"
6-
gem "webrick"
6+
gem "puma"
7+
gem "rackup"
8+
gem "sinatra", "~> 4.0"

examples/server/app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
cloud_events_http = CloudEvents::HttpBinding.default
77

8-
post "/" do
8+
post("/") do
99
event = cloud_events_http.decode_event(request.env)
1010
logger.info("Received CloudEvent: #{event.to_h}")
1111
end

test/test_examples.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "helper"
4+
5+
require "io/wait"
6+
7+
require "toys/compat"
8+
require "toys/utils/exec"
9+
10+
describe "examples" do
11+
let(:exec_util) { Toys::Utils::Exec.new }
12+
let(:examples_dir) { File.join(File.dirname(__dir__), "examples") }
13+
let(:client_dir) { File.join(examples_dir, "client") }
14+
let(:server_dir) { File.join(examples_dir, "server") }
15+
16+
def expect_read(io, content, timeout)
17+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
18+
received = String.new
19+
loop do
20+
time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
21+
assert(time < deadline, "Did not receive content in time: #{content}")
22+
next unless io.wait_readable((deadline - time).ceil)
23+
received.concat(io.readpartial(1024))
24+
return if received.include?(content)
25+
end
26+
end
27+
28+
it "sends and receives an event" do
29+
skip if Toys::Compat.jruby?
30+
skip if Toys::Compat.truffleruby?
31+
skip if Toys::Compat.windows?
32+
Bundler.with_unbundled_env do
33+
assert(exec_util.exec(["bundle", "install"], out: :null, chdir: server_dir).success?, "server bundle failed")
34+
assert(exec_util.exec(["bundle", "install"], out: :null, chdir: client_dir).success?, "client bundle failed")
35+
exec_util.exec(["bundle", "exec", "ruby", "app.rb"], chdir: server_dir,
36+
in: :controller, out: :controller, err: :controller) do |server_control|
37+
expect_read(server_control.out, "* Listening on http", 5)
38+
client_result = exec_util.exec(["bundle", "exec", "ruby", "send.rb"], chdir: client_dir)
39+
assert(client_result.success?)
40+
expect_read(server_control.err, "Hello, CloudEvents!", 5)
41+
ensure
42+
server_control.kill("TERM")
43+
end
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)