Skip to content

Commit 014b36f

Browse files
committed
Merge pull request #9 from cakejelly/webhooks-support
Adds support for Webhooks
2 parents 1c7001b + 5c78e9c commit 014b36f

18 files changed

+758
-80
lines changed

README.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Or install it yourself as:
2828
## Usage
2929

3030
### Resources
31-
All client methods return `Resource` objects or a collection of `Resource` objects. Every attribute from a resource can be accessed by calling attribute methods:
31+
All client methods return `Resource` objects or a collection of `Resource` objects. Every attribute from a resource can be accessed by calling attribute methods:
3232

3333
```ruby
3434
conversation = platform.conversations.find("fb2f3a48-523d-4449-a57f-c6651fc6612c")
@@ -46,8 +46,8 @@ conversation.attributes
4646
# => {"id" => "fb2f3a48-523d-4449-a57f-c6651fc6612c", "url" => "https://api.layer.com/apps/<APP_ID>/conversations/fb2f3a48-523d-4449-a57f-c6651fc6612c", ...}
4747
```
4848

49-
### [Platform API](https://developer.layer.com/docs/platform)
50-
See the official [Platform API docs](https://developer.layer.com/docs/platform) for additional info.
49+
### [Platform](https://developer.layer.com/docs/platform)
50+
See the [Platform API docs](https://developer.layer.com/docs/platform) for additional info.
5151

5252
#### Authentication/setup
5353

@@ -280,12 +280,76 @@ Make sure the following environment variables are set:
280280
```ruby
281281
# Returns a valid signed identity token. #
282282
token = platform.generate_identity_token(user_id: "1234", nonce: "your_random_nonce")
283-
# => #<Layer::IdentityToken:0x007f89b4adb890
283+
# => #<Layer::IdentityToken:0x007f89b4adb890>
284284

285285
token.to_s
286286
# => "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsInR.cCI6IkpXVCIsImN0eSI6ImxheWVyLWVpdDt2PTEiLCJraWQiOiJhNz.5YTE0MC02YzY3LTExZTUtYjM0Mi1jZGJmNDAwZTE5NDgifQ"
287287
```
288288

289+
### [Webhooks](https://developer.layer.com/docs/webhooks)
290+
See the [Webhooks API docs](https://developer.layer.com/docs/webhooks) for additional info.
291+
292+
#### Authentication/setup
293+
294+
```ruby
295+
client = Layer::Webhooks::Client.new(api_token: "your_api_token", app_id: "your_app_id")
296+
# => #<Layer::Webhooks::Client @api_token="...", @app_id="...">
297+
```
298+
If you have `ENV['LAYER_API_TOKEN']` and `ENV['LAYER_APP_ID']` environment variables setup, they will be used by default and don't need to be included:
299+
```ruby
300+
client = Layer::Webhooks::Client.new
301+
# => #<Layer::Webhooks::Client @api_token="...", @app_id="...">
302+
```
303+
304+
#### Registering Webhooks
305+
306+
```ruby
307+
client.webhooks.create(
308+
version: "1.0",
309+
target_url: "https://mydomain.com/my-webhook-endpoint",
310+
events: ["conversation.created", "message.sent"],
311+
secret: "1697f925ec7b1697f925ec7b",
312+
config: {:key1=>"value1", :key2=>"value2"}
313+
)
314+
# => #<Layer::Resources::Webhook @attributes={...}>
315+
```
316+
317+
#### Listing Webhooks
318+
319+
```ruby
320+
client.webhooks.list
321+
# => [#<Layer::Resources::Webhook>, #<Layer::Resources::Webhook>, ...]
322+
```
323+
324+
#### Retrieving Webhooks
325+
326+
```ruby
327+
client.webhooks.find("webhook_id")
328+
# => #<Layer::Resources::Webhook @attributes={...}>
329+
```
330+
331+
#### Activating Webhooks
332+
333+
```ruby
334+
webhook = client.webhooks.find("webhook_id")
335+
webhook.activate
336+
```
337+
338+
#### Deactivating Webhooks
339+
340+
```ruby
341+
webhook = client.webhooks.find("webhook_id")
342+
webhook.deactivate
343+
```
344+
345+
#### Deleting Webhooks
346+
347+
```ruby
348+
webhook = client.webhooks.find("webhook_id")
349+
webhook.destroy
350+
```
351+
352+
289353
## Development ##
290354

291355
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

lib/layer/api.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
require "layer/resource"
99
require "layer/resource_proxy"
1010
require "layer/identity_token"
11+
require "layer/base_client"
1112

1213
# Platform
1314
require "layer/platform/client"
1415

16+
# Webhooks
17+
require "layer/webhooks/client"
18+
1519
# Resources
1620
require "layer/resources/conversation"
1721
require "layer/resources/message"
1822
require "layer/resources/announcement"
1923
require "layer/resources/user"
2024
require "layer/resources/block"
25+
require "layer/resources/webhook"
2126

2227
require "layer/middleware/api_errors"

lib/layer/base_client.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Layer
2+
class BaseClient
3+
DEFAULT_HOST = "https://api.layer.com"
4+
5+
def client
6+
@http_client ||= Layer::HttpClient.new(base_url, default_headers)
7+
end
8+
9+
def strip_layer_prefix(string)
10+
string.split("/").last if string
11+
end
12+
13+
def default_headers
14+
{
15+
'Accept' => 'application/vnd.layer+json; version=1.0',
16+
'Content-Type' => 'application/json'
17+
}
18+
end
19+
20+
def base_url
21+
DEFAULT_HOST
22+
end
23+
end
24+
end

lib/layer/http_client.rb

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
module Layer
44
class HttpClient
5-
DEFAULT_HOST = "https://api.layer.com"
5+
attr_reader :base_url, :default_headers
66

7-
attr_reader :app_id, :api_token
8-
9-
def initialize(app_id, api_token)
10-
@app_id = app_id
11-
@api_token = api_token
7+
def initialize(base_url, default_headers)
8+
@base_url = base_url
9+
@default_headers = default_headers
1210
end
1311

1412
def connection
1513
@connection ||= Faraday.new(url: base_url) do |faraday|
16-
faraday.headers = default_layer_headers
14+
faraday.headers = default_headers
1715
faraday.request :url_encoded
1816
faraday.adapter Faraday.default_adapter
1917
faraday.use Middleware::ApiErrors
@@ -53,20 +51,8 @@ def run_request(method, url, options = {})
5351
)
5452
end
5553

56-
def default_layer_headers
57-
{
58-
'Accept' => 'application/vnd.layer+json; version=1.0',
59-
'Authorization' => "Bearer #{api_token}",
60-
'Content-Type' => 'application/json'
61-
}
62-
end
63-
6454
def layer_patch_header
6555
{ 'Content-Type' => 'application/vnd.layer-patch+json' }
6656
end
67-
68-
def base_url
69-
"#{DEFAULT_HOST}/apps/#{app_id}"
70-
end
7157
end
7258
end

lib/layer/platform/client.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Layer
22
module Platform
3-
class Client
3+
class Client < Layer::BaseClient
44
attr_accessor :api_token, :app_id
55

66
def initialize(options = {})
@@ -9,14 +9,6 @@ def initialize(options = {})
99
@app_id = strip_layer_prefix(id)
1010
end
1111

12-
def client
13-
@http_client ||= Layer::HttpClient.new(@app_id, @api_token)
14-
end
15-
16-
def strip_layer_prefix(string)
17-
string.split("/").last if string
18-
end
19-
2012
def announcements
2113
Layer::ResourceProxy.new(client, nil, Layer::Resources::Announcement)
2214
end
@@ -33,6 +25,14 @@ def generate_identity_token(options = {})
3325
Layer::IdentityToken.new(options)
3426
end
3527

28+
def default_headers
29+
super.merge({"Authorization" => "Bearer #{api_token}"})
30+
end
31+
32+
def base_url
33+
"#{DEFAULT_HOST}/apps/#{app_id}"
34+
end
35+
3636
def inspect
3737
"#<#{self.class} api_token=\"#{@api_token}\" app_id=\"#{@app_id}\">"
3838
end

lib/layer/resources/webhook.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Layer
2+
module Resources
3+
class Webhook < Layer::Resource
4+
def activate
5+
client.post("#{url}/activate")
6+
end
7+
8+
def deactivate
9+
client.post("#{url}/deactivate")
10+
end
11+
end
12+
end
13+
end

lib/layer/webhooks/client.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Layer
2+
module Webhooks
3+
class Client < Layer::BaseClient
4+
attr_accessor :api_token, :app_id
5+
6+
def initialize(options = {})
7+
id = options[:app_id] || ENV['LAYER_APP_ID']
8+
@api_token = options[:api_token] || ENV['LAYER_API_TOKEN']
9+
@app_id = strip_layer_prefix(id)
10+
end
11+
12+
def webhooks
13+
Layer::ResourceProxy.new(client, nil, Layer::Resources::Webhook)
14+
end
15+
16+
def default_headers
17+
super.merge(
18+
{
19+
"Authorization" => "Bearer #{api_token}",
20+
"Accept" => "application/vnd.layer.webhooks+json; version=1.0"
21+
}
22+
)
23+
end
24+
25+
def base_url
26+
"#{DEFAULT_HOST}/apps/#{app_id}"
27+
end
28+
29+
def inspect
30+
"#<#{self.class} api_token=\"#{@api_token}\" app_id=\"#{@app_id}\">"
31+
end
32+
33+
alias_method :to_s, :inspect
34+
end
35+
end
36+
end

spec/cassettes/webhook_activate.yml

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)