Skip to content

Commit 1e80df3

Browse files
committed
Initial version 0.1.0
0 parents  commit 1e80df3

16 files changed

+796
-0
lines changed

.gitignore

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
*.gem
2+
*.rbc
3+
*.DS_Store
4+
/.config
5+
/coverage/
6+
/InstalledFiles
7+
/pkg/
8+
/spec/reports/
9+
/spec/examples.txt
10+
/test/tmp/
11+
/test/version_tmp/
12+
/tmp/
13+
14+
# Used by dotenv library to load environment variables.
15+
# .env
16+
17+
# Ignore Byebug command history file.
18+
.byebug_history
19+
20+
## Specific to RubyMotion (use of CocoaPods):
21+
#
22+
# We recommend against adding the Pods directory to your .gitignore. However
23+
# you should judge for yourself, the pros and cons are mentioned at:
24+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
25+
#
26+
# vendor/Pods/
27+
28+
## Documentation cache and generated files:
29+
/.yardoc/
30+
/_yardoc/
31+
/doc/
32+
/rdoc/
33+
34+
## Environment normalization:
35+
/.bundle/
36+
/vendor/bundle
37+
/lib/bundler/man/
38+
39+
# for a library or gem, you might want to ignore these files since the code is
40+
# intended to run in multiple environments; otherwise, check them in:
41+
# Gemfile.lock
42+
# .ruby-version
43+
# .ruby-gemset
44+
45+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
46+
.rvmrc
47+
48+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
49+
# .rubocop-https?--*

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.0.2

Gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gemspec

Gemfile.lock

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
PATH
2+
remote: .
3+
specs:
4+
knockapi (0.1.0)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
ast (2.4.2)
10+
parallel (1.20.1)
11+
parser (3.0.2.0)
12+
ast (~> 2.4.1)
13+
rainbow (3.0.0)
14+
rake (13.0.6)
15+
regexp_parser (2.1.1)
16+
rexml (3.2.5)
17+
rubocop (1.18.4)
18+
parallel (~> 1.10)
19+
parser (>= 3.0.0.0)
20+
rainbow (>= 2.2.2, < 4.0)
21+
regexp_parser (>= 1.8, < 3.0)
22+
rexml
23+
rubocop-ast (>= 1.8.0, < 2.0)
24+
ruby-progressbar (~> 1.7)
25+
unicode-display_width (>= 1.4.0, < 3.0)
26+
rubocop-ast (1.10.0)
27+
parser (>= 3.0.1.1)
28+
rubocop-performance (1.11.4)
29+
rubocop (>= 1.7.0, < 2.0)
30+
rubocop-ast (>= 0.4.0)
31+
ruby-progressbar (1.11.0)
32+
standard (1.1.7)
33+
rubocop (= 1.18.4)
34+
rubocop-performance (= 1.11.4)
35+
unicode-display_width (2.0.0)
36+
37+
PLATFORMS
38+
x86_64-darwin-19
39+
40+
DEPENDENCIES
41+
bundler (>= 2.0.1)
42+
knockapi!
43+
rake
44+
standard
45+
46+
BUNDLED WITH
47+
2.2.22

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Knock Labs, Inc.
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 all
13+
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 THE
21+
SOFTWARE.

README.md

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Knock Ruby library
2+
3+
Knock API access for applications written in Ruby.
4+
5+
## Documentation
6+
7+
See the documentation for Ruby usage examples.
8+
9+
## Installation
10+
11+
```bash
12+
gem install knockapi
13+
```
14+
15+
## Configuration
16+
17+
To use the library you must provide a secret API key, provided in the Knock dashboard.
18+
19+
You can set it as an environment variable:
20+
21+
```bash
22+
KNOCK_API_KEY="sk_12345"
23+
```
24+
25+
Or, you may set the key yourself in an initializer:
26+
27+
```ruby
28+
# /config/initializers/knock.rb
29+
Knock.key = 'sk_12345'
30+
```
31+
32+
## Usage
33+
34+
### Identifying users
35+
36+
```ruby
37+
require "knockapi"
38+
39+
Knock.key = "sk_12345"
40+
41+
Knock::Users.identify(
42+
id: "jhammond",
43+
data: {
44+
name: "John Hammond",
45+
46+
}
47+
)
48+
```
49+
50+
### Sending notifies (triggering workflows)
51+
52+
```ruby
53+
require "knockapi"
54+
55+
Knock.key = "sk_12345"
56+
57+
# The key of the workflow (from Knock dashboard)
58+
Knock::Workflows.trigger(
59+
key: "dinosaurs-loose",
60+
# user id of who performed the action
61+
actor: "dnedry",
62+
# list of user ids for who should receive the notif
63+
recipients: ["jhammond", "agrant", "imalcolm", "esattler"],
64+
# data payload to send through
65+
data: {
66+
type: "trex",
67+
priority: 1,
68+
},
69+
# an optional key to provide to cancel a notify
70+
cancellation_key: trigger_alert.id,
71+
)
72+
```
73+
74+
### Retrieving users
75+
76+
```ruby
77+
require "knockapi"
78+
79+
Knock.key = "sk_12345"
80+
81+
Knock::Users.get(id: "jhammond")
82+
```
83+
84+
### Deleting users
85+
86+
```ruby
87+
require "knockapi"
88+
89+
Knock.key = "sk_12345"
90+
91+
Knock::Users.delete(id: "jhammond")
92+
```
93+
94+
### Preferences
95+
96+
```ruby
97+
require "knockapi"
98+
Knock.key = "sk_12345"
99+
100+
# Set an entire preference set
101+
Knock::Preferences.set(
102+
user_id: "jhammond",
103+
channel_types: { email: true, sms: false },
104+
workflows: {
105+
'dinosaurs-loose': {
106+
channel_types: { email: false, in_app_feed: false }
107+
}
108+
}
109+
)
110+
111+
# Get an entire preference set
112+
Knock::Preferences.get(user_id: "jhammond")
113+
```
114+
115+
### Cancelling workflows
116+
117+
```ruby
118+
require "knockapi"
119+
Knock.key = "sk_12345"
120+
121+
Knock::Workflows.cancel(
122+
key: "dinosaurs-loose",
123+
cancellation_key: trigger_alert.id,
124+
# Optionally, you can provide recipients to cancel for
125+
recipients: ["jhammond"]
126+
)
127+
```
128+
129+
### Signing JWTs
130+
131+
You can use the `jwt` gem to sign JWTs easily. You will need to generate an environment specific signing key, which you can find in the Knock dashboard.
132+
133+
If you're using a signing token you will need to pass this to your client to perform authentication. You can read more about [clientside authentication here](https://docs.knock.app/client-integration/authenticating-users).
134+
135+
```ruby
136+
require 'jwt'
137+
138+
secret = ENV['KNOCK_SIGNING_KEY']
139+
now = Time.now.to_i
140+
141+
payload = {
142+
# The subject of the token
143+
sub: 'jhammond',
144+
# When the token was issued
145+
iat: now,
146+
# Expire the token in 1 week
147+
exp: now + (24 * 7 * 3600)
148+
}
149+
150+
JWT.encode(payload, secret, 'RS256')
151+
```

bin/console

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "knock"
5+
6+
require "irb"
7+
IRB.start(__FILE__)

knockapi.gemspec

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
lib = File.expand_path("lib", __dir__)
4+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5+
require "knock/version"
6+
7+
Gem::Specification.new do |spec|
8+
spec.name = "knockapi"
9+
spec.version = Knock::VERSION
10+
spec.authors = ["Knock Labs, Inc."]
11+
spec.email = ["[email protected]"]
12+
spec.description = "API client for Knock"
13+
spec.summary = "API client for Knock"
14+
spec.homepage = "https://github.com/knocklabs/knock-ruby"
15+
spec.license = "MIT"
16+
spec.metadata = {
17+
"documentation_uri" => "https://docs.knock.app"
18+
}
19+
20+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
21+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23+
spec.require_paths = ["lib"]
24+
25+
spec.add_development_dependency "bundler", ">= 2.0.1"
26+
spec.add_development_dependency "rake"
27+
spec.add_development_dependency "standard"
28+
29+
spec.required_ruby_version = ">= 2.5"
30+
end

lib/knock.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require "knock/version"
4+
require "json"
5+
6+
module Knock
7+
API_HOSTNAME = ENV["KNOCK_API_HOSTNAME"] || "api.knock.app"
8+
9+
def self.key=(value)
10+
Base.key = value
11+
end
12+
13+
def self.key
14+
Base.key
15+
end
16+
17+
def self.key!
18+
key || raise("Knock.key not set")
19+
end
20+
21+
autoload :Base, "knock/base"
22+
autoload :Client, "knock/client"
23+
24+
# Resources
25+
autoload :Preferences, "knock/preferences"
26+
autoload :Users, "knock/users"
27+
autoload :Workflows, "knock/workflows"
28+
29+
# Errors
30+
autoload :APIError, "knock/errors"
31+
autoload :AuthenticationError, "knock/errors"
32+
autoload :InvalidRequestError, "knock/errors"
33+
34+
key = ENV["KNOCK_API_KEY"]
35+
Knock.key = key unless key.nil?
36+
37+
# Triggers the workflow with the given key
38+
#
39+
# @param [String] key The workflow key
40+
# @param [String] actor The actor ID
41+
# @param [Array<String>] recipients The recipient IDs
42+
# @param [Hash] data The data to pass to the workflow
43+
# @param [String] cancellation_key An optional key to identify this workflow
44+
# invocation for cancelling
45+
# @param [String] tenant An optional tenant identifier
46+
#
47+
# @return [Hash] A workflow trigger result
48+
def self.notify(**args)
49+
Knock::Workflows.trigger(**args)
50+
end
51+
end

lib/knock/base.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Knock
2+
## The Base class handles setting and reading the Knock API Key for authentication
3+
module Base
4+
attr_accessor :key
5+
6+
class << self
7+
attr_writer :key
8+
attr_reader :key
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)