bifrost-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +49 -0
- data/LICENSE.txt +21 -0
- data/README.md +81 -0
- data/Rakefile +6 -0
- data/bifrost-client.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/bifrost/client.rb +50 -0
- data/lib/bifrost/client/version.rb +5 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3354b44b58bfe9235b57e656d7f3aef5899ef15e9b9c8aaaf6cf85383a48701f
|
4
|
+
data.tar.gz: 300fcfaf94f4f8c0499efd7cfd16ce7c1548da46e2d63ec2e59772dcdc457cae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6967087f3458d75605e1b85c002bedaf3712513ac7d2c79017c7b059f26bb318999f982c549d6bfa8cd7682903e496b9cf4a6ff5e31589fbbfb19f1cba08251b
|
7
|
+
data.tar.gz: 696cd1fb5dfddedd6e4848c66d666b5873cbf66ff1f5fd8a6ec9b2eb7926d54d72303de1f8208ce333c53760bc17ec27b9eeef72b7bf0ded74669f116e8b196e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
bifrost-client (0.1.0)
|
5
|
+
jwt (>= 2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.5.2)
|
11
|
+
public_suffix (>= 2.0.2, < 4.0)
|
12
|
+
crack (0.4.3)
|
13
|
+
safe_yaml (~> 1.0.0)
|
14
|
+
diff-lcs (1.3)
|
15
|
+
hashdiff (0.3.7)
|
16
|
+
jwt (2.1.0)
|
17
|
+
public_suffix (3.0.2)
|
18
|
+
rake (10.5.0)
|
19
|
+
rspec (3.7.0)
|
20
|
+
rspec-core (~> 3.7.0)
|
21
|
+
rspec-expectations (~> 3.7.0)
|
22
|
+
rspec-mocks (~> 3.7.0)
|
23
|
+
rspec-core (3.7.1)
|
24
|
+
rspec-support (~> 3.7.0)
|
25
|
+
rspec-expectations (3.7.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.7.0)
|
28
|
+
rspec-mocks (3.7.0)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.7.0)
|
31
|
+
rspec-support (3.7.1)
|
32
|
+
safe_yaml (1.0.4)
|
33
|
+
webmock (3.3.0)
|
34
|
+
addressable (>= 2.3.6)
|
35
|
+
crack (>= 0.3.2)
|
36
|
+
hashdiff
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
bifrost-client!
|
43
|
+
bundler (~> 1.16)
|
44
|
+
rake (~> 10.0)
|
45
|
+
rspec (~> 3.0)
|
46
|
+
webmock
|
47
|
+
|
48
|
+
BUNDLED WITH
|
49
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Alternate Labs Ltd
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Bifrost Ruby Client
|
2
|
+
|
3
|
+
A ruby client helper for the [bifrost crystal](https://github.com/alternatelabs/bifrost) websockets server.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'bifrost-client'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
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
|
+
|
43
|
+
## Usage
|
44
|
+
|
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
|
+
```
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
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.
|
72
|
+
|
73
|
+
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).
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/alternatelabs/bifrost-ruby-client.
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "bifrost/client/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bifrost-client"
|
8
|
+
spec.version = Bifrost::Client::VERSION
|
9
|
+
spec.authors = ["Pete Hawkins"]
|
10
|
+
spec.email = ["pete@alternatelabs.co"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby client library for the bifrost crystal websocket server}
|
13
|
+
spec.description = %q{Bifrost is a crystal websocket server, this is a client library to simplify broadcasting messages to it.}
|
14
|
+
spec.homepage = "https://github.com/alternatelabs/bifrost"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "jwt", ">= 2.0"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "webmock"
|
29
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bifrost/client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "bifrost/client/version"
|
2
|
+
require "net/http"
|
3
|
+
require "json"
|
4
|
+
require "jwt"
|
5
|
+
|
6
|
+
module Bifrost
|
7
|
+
class Client
|
8
|
+
attr_reader :jwt_secret, :bifrost_server_url
|
9
|
+
|
10
|
+
def initialize(jwt_secret: ENV["JWT_SECRET"], bifrost_server_url: ENV["BIFROST_URL"])
|
11
|
+
@jwt_secret = jwt_secret
|
12
|
+
@bifrost_server_url = bifrost_server_url
|
13
|
+
end
|
14
|
+
|
15
|
+
def broadcast(channel, event:, data: nil)
|
16
|
+
data = {
|
17
|
+
channel: channel,
|
18
|
+
message: {
|
19
|
+
event: event,
|
20
|
+
data: data
|
21
|
+
},
|
22
|
+
exp: Time.now.to_i + 3600
|
23
|
+
}
|
24
|
+
jwt = JWT.encode(data, jwt_secret, "HS512")
|
25
|
+
uri = URI.parse(bifrost_server_url)
|
26
|
+
uri.path = "/broadcast"
|
27
|
+
ssl = uri.is_a?(URI::HTTPS)
|
28
|
+
|
29
|
+
res = Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http|
|
30
|
+
request = Net::HTTP::Post.new(uri.path)
|
31
|
+
request.body = JSON.dump(token: jwt)
|
32
|
+
request["Content-Type"] = "application/json"
|
33
|
+
http.request(request)
|
34
|
+
end
|
35
|
+
|
36
|
+
if Integer(res.code) > 206
|
37
|
+
raise ServerError.new("Bifrost server responded with #{res.code}: #{res.body}")
|
38
|
+
else
|
39
|
+
JSON.parse(res.body, symbolize_names: true)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def token_for(channels:)
|
44
|
+
payload = { channels: channels }
|
45
|
+
JWT.encode(payload, jwt_secret, "HS512")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class ServerError < StandardError; end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bifrost-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pete Hawkins
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jwt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
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.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
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
|
+
description: Bifrost is a crystal websocket server, this is a client library to simplify
|
84
|
+
broadcasting messages to it.
|
85
|
+
email:
|
86
|
+
- pete@alternatelabs.co
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bifrost-client.gemspec
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/bifrost/client.rb
|
103
|
+
- lib/bifrost/client/version.rb
|
104
|
+
homepage: https://github.com/alternatelabs/bifrost
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.7.6
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Ruby client library for the bifrost crystal websocket server
|
128
|
+
test_files: []
|