Skip to content

Commit ba7f899

Browse files
committed
Write documentation
1 parent a59b777 commit ba7f899

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,55 @@ And then execute:
1616
$ bundle
1717
```
1818

19+
## Configuration
20+
21+
By default Bifrost::Client will look for environment variables:
22+
23+
```sh
24+
JWT_SECRET=9c3ed1aa0ba8405effdb363839caa3cc
25+
BIFROST_URL=https://bifrost-server.myapp.com
26+
```
27+
28+
And you can create a Bifrost client like so:
29+
30+
```ruby
31+
client = Bifrost::Client.new
32+
```
33+
34+
Or you can pass in configuration directly:
35+
36+
```ruby
37+
client = Bifrost::Client.new(
38+
jwt_secret: "9c3ed1aa0ba8405effdb363839caa3cc",
39+
bifrost_server_url: "https://bifrost-server.myapp.com"
40+
)
41+
```
42+
1943
## Usage
2044

21-
TODO: Write usage instructions here
45+
### Token helper
46+
47+
This helps you generate a JWT to give an end user permission to connect to a channel called `user:thor`
48+
49+
```ruby
50+
token = client.token_for(channels: %w[user:thor]) # => "eyJhbGciOiJIUzUxMiJ9.eyJjaGFubmVscyI6WyJ1c2VyOnRob3IiXX0.SBgGNE-n7S8gVtYQ3wg7_WG2TqOGNFf-jy0GW57_YV-B-xjekm4KCQwTZIqxL_TXoqbWW3tThT9YTt4GLD4Y7A"
51+
```
52+
53+
Once you have this token [follow the instructions on the bifrost repo](https://github.com/alternatelabs/bifrost#1-create-an-api-endpoint-in-your-application-that-can-give-users-a-realtime-token) to provide that token to client side JS and connect using ReconnectingWebsocket.
54+
55+
### Broadcast events
56+
57+
Use `client#broadcast` when you want to send an event out to all members of a channel. Payload must be a string, we recommend you encode it to JSON and can safely `JSON.parse(payload)` in your client side code upon receiving the message.
58+
59+
```ruby
60+
begin
61+
payload = JSON.dump({ name: "Hela" })
62+
channel = "user:thor"
63+
client.broadcast(channel, event: "invader_detected", data: payload) # => { deliveries: 1 }
64+
rescue Bifrost::ServerError => e
65+
# Do something with the error
66+
end
67+
```
2268

2369
## Development
2470

@@ -28,7 +74,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
2874

2975
## Contributing
3076

31-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bifrost-client.
77+
Bug reports and pull requests are welcome on GitHub at https://github.com/alternatelabs/bifrost-ruby-client.
3278

3379
## License
3480

lib/bifrost/client.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def broadcast(channel, event:, data: nil)
3434
end
3535

3636
if Integer(res.code) > 206
37-
false
37+
raise ServerError.new("Bifrost server responded with #{res.code}: #{res.body}")
3838
else
39-
true
39+
JSON.parse(res.body, symbolize_names: true)
4040
end
4141
end
4242

@@ -45,4 +45,6 @@ def token_for(channels:)
4545
JWT.encode(payload, jwt_secret, "HS512")
4646
end
4747
end
48+
49+
class ServerError < StandardError; end
4850
end

spec/bifrost/client_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
result = subject.broadcast(channel, event: "event_name", data: "test")
4343

44-
expect(result).to eq(true)
44+
expect(result).to eq({message: "Success", deliveries: 1})
4545

4646
expect(WebMock).to(have_requested(:post, "#{server_url}/broadcast").with { |req|
4747
payload = JSON.parse(req.body)["token"]
@@ -62,9 +62,9 @@
6262
stub_request(:post, %r{#{server_url}})
6363
.to_return(body: bifrost_resp, status: 400, headers: resp_headers)
6464

65-
result = subject.broadcast(channel, event: "event_name", data: "test")
66-
67-
expect(result).to eq(false)
65+
expect do
66+
subject.broadcast(channel, event: "event_name", data: "test")
67+
end.to raise_error(Bifrost::ServerError)
6868
end
6969
end
7070
end

0 commit comments

Comments
 (0)