|
| 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 | +``` |
0 commit comments