layer-api 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +79 -15
- data/lib/layer/api.rb +5 -0
- data/lib/layer/base_client.rb +24 -0
- data/lib/layer/http_client.rb +5 -19
- data/lib/layer/platform/client.rb +9 -9
- data/lib/layer/resources/webhook.rb +13 -0
- data/lib/layer/version.rb +1 -1
- data/lib/layer/webhooks/client.rb +36 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 496391415565acba6e9457ee56385474741f3ba1
|
4
|
+
data.tar.gz: f849b844f3362d4285dfc329d8fa268af4dea07a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d23bf3f10474ff0bbe727f28650b490a84459e1760d54e43457ed67df1f00a11d9ab06154aefe555f4bea2a132326f436fbd3a944946c30c83f76cb8ebf3d637
|
7
|
+
data.tar.gz: 41377bb6ff55bc0c127a56cd0634a32ccd4fde0af998c3982f55fe03dd8dbdae443feb05fbb6372b8d9d8761697d6e7833fc2c85d7bed1e04295ca2dee24c63f
|
data/README.md
CHANGED
@@ -28,11 +28,11 @@ Or install it yourself as:
|
|
28
28
|
## Usage
|
29
29
|
|
30
30
|
### Resources
|
31
|
-
All client methods return
|
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:
|
32
32
|
|
33
33
|
```ruby
|
34
34
|
conversation = platform.conversations.find("fb2f3a48-523d-4449-a57f-c6651fc6612c")
|
35
|
-
#<Layer::Resources::Conversation
|
35
|
+
#<Layer::Resources::Conversation @attributes={...}>
|
36
36
|
|
37
37
|
# Get the stripped uuid for any resource
|
38
38
|
conversation.uuid
|
@@ -46,19 +46,19 @@ conversation.attributes
|
|
46
46
|
# => {"id" => "fb2f3a48-523d-4449-a57f-c6651fc6612c", "url" => "https://api.layer.com/apps/<APP_ID>/conversations/fb2f3a48-523d-4449-a57f-c6651fc6612c", ...}
|
47
47
|
```
|
48
48
|
|
49
|
-
### [Platform
|
50
|
-
See the
|
49
|
+
### [Platform](https://developer.layer.com/docs/platform)
|
50
|
+
See the [Platform API docs](https://developer.layer.com/docs/platform) for additional info.
|
51
51
|
|
52
52
|
#### Authentication/setup
|
53
53
|
|
54
54
|
```ruby
|
55
55
|
platform = Layer::Platform::Client.new(api_token: "your_api_token", app_id: "your_app_id")
|
56
|
-
# => #<Layer::Platform::Client
|
56
|
+
# => #<Layer::Platform::Client @api_token="...", @app_id="...">
|
57
57
|
```
|
58
58
|
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:
|
59
59
|
```ruby
|
60
60
|
platform = Layer::Platform::Client.new
|
61
|
-
# => #<Layer::Platform::Client
|
61
|
+
# => #<Layer::Platform::Client @api_token="...", @app_id="...">
|
62
62
|
```
|
63
63
|
|
64
64
|
#### Retrieving Conversations ####
|
@@ -76,11 +76,11 @@ convs = user.conversations.list
|
|
76
76
|
# For a user
|
77
77
|
user = platform.users.find("user_id")
|
78
78
|
conv = user.conversations.find("conversation_id")
|
79
|
-
# => #<Layer::Resources::Conversation
|
79
|
+
# => #<Layer::Resources::Conversation @attributes={...}>
|
80
80
|
|
81
81
|
# or alternatively
|
82
82
|
conv = platform.conversations.find("conversation_id")
|
83
|
-
# => #<Layer::Resources::Conversation
|
83
|
+
# => #<Layer::Resources::Conversation @attributes={...}>
|
84
84
|
```
|
85
85
|
|
86
86
|
#### Creating Conversations ####
|
@@ -98,7 +98,7 @@ conversation = {
|
|
98
98
|
}
|
99
99
|
|
100
100
|
platform.conversations.create(conversation)
|
101
|
-
# => #<Layer::Resources::Conversation
|
101
|
+
# => #<Layer::Resources::Conversation @attributes={...}>
|
102
102
|
```
|
103
103
|
|
104
104
|
#### Editing Conversations ####
|
@@ -149,7 +149,7 @@ message = {
|
|
149
149
|
|
150
150
|
conv = platform.conversations.find("conversation_id")
|
151
151
|
conv.messages.create(message)
|
152
|
-
# => #<Layer::Resources::Message
|
152
|
+
# => #<Layer::Resources::Message @attributes={...}>
|
153
153
|
```
|
154
154
|
|
155
155
|
#### Retrieving Messages ####
|
@@ -172,12 +172,12 @@ conv.messages.list
|
|
172
172
|
# From a specific user's perspective
|
173
173
|
user = platform.users.find("user_id")
|
174
174
|
messages = user.messages.find("message_id")
|
175
|
-
# => #<Layer::Resources::Message
|
175
|
+
# => #<Layer::Resources::Message @attributes={...}>
|
176
176
|
|
177
177
|
# From the systems perspective
|
178
178
|
conv = platform.conversations.find("conversation_id")
|
179
179
|
messages = conv.messages.find("message_id")
|
180
|
-
# => #<Layer::Resources::Message
|
180
|
+
# => #<Layer::Resources::Message @attributes={...}>
|
181
181
|
```
|
182
182
|
|
183
183
|
#### Deleting A Message
|
@@ -209,7 +209,7 @@ announcement = {
|
|
209
209
|
}
|
210
210
|
|
211
211
|
platform.announcements.create(announcement)
|
212
|
-
# => #<Layer::Resources::Announcement
|
212
|
+
# => #<Layer::Resources::Announcement @attributes={...}>
|
213
213
|
```
|
214
214
|
|
215
215
|
#### Modifying A Users Block List ####
|
@@ -233,7 +233,7 @@ user.update(operations)
|
|
233
233
|
user = platform.users.find("user_id")
|
234
234
|
|
235
235
|
blocks = user.blocks.list
|
236
|
-
# => [#<Layer::Resources::Block
|
236
|
+
# => [#<Layer::Resources::Block>, [#<Layer::Resources::Block>, ...]
|
237
237
|
```
|
238
238
|
|
239
239
|
#### Blocking Users
|
@@ -280,12 +280,76 @@ Make sure the following environment variables are set:
|
|
280
280
|
```ruby
|
281
281
|
# Returns a valid signed identity token. #
|
282
282
|
token = platform.generate_identity_token(user_id: "1234", nonce: "your_random_nonce")
|
283
|
-
# => #<Layer::IdentityToken:0x007f89b4adb890
|
283
|
+
# => #<Layer::IdentityToken:0x007f89b4adb890>
|
284
284
|
|
285
285
|
token.to_s
|
286
286
|
# => "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsInR.cCI6IkpXVCIsImN0eSI6ImxheWVyLWVpdDt2PTEiLCJraWQiOiJhNz.5YTE0MC02YzY3LTExZTUtYjM0Mi1jZGJmNDAwZTE5NDgifQ"
|
287
287
|
```
|
288
288
|
|
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
|
+
|
289
353
|
## Development ##
|
290
354
|
|
291
355
|
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.
|
data/lib/layer/api.rb
CHANGED
@@ -8,15 +8,20 @@ require "layer/errors"
|
|
8
8
|
require "layer/resource"
|
9
9
|
require "layer/resource_proxy"
|
10
10
|
require "layer/identity_token"
|
11
|
+
require "layer/base_client"
|
11
12
|
|
12
13
|
# Platform
|
13
14
|
require "layer/platform/client"
|
14
15
|
|
16
|
+
# Webhooks
|
17
|
+
require "layer/webhooks/client"
|
18
|
+
|
15
19
|
# Resources
|
16
20
|
require "layer/resources/conversation"
|
17
21
|
require "layer/resources/message"
|
18
22
|
require "layer/resources/announcement"
|
19
23
|
require "layer/resources/user"
|
20
24
|
require "layer/resources/block"
|
25
|
+
require "layer/resources/webhook"
|
21
26
|
|
22
27
|
require "layer/middleware/api_errors"
|
@@ -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
|
data/lib/layer/http_client.rb
CHANGED
@@ -2,18 +2,16 @@ require 'securerandom'
|
|
2
2
|
|
3
3
|
module Layer
|
4
4
|
class HttpClient
|
5
|
-
|
5
|
+
attr_reader :base_url, :default_headers
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
12
10
|
end
|
13
11
|
|
14
12
|
def connection
|
15
13
|
@connection ||= Faraday.new(url: base_url) do |faraday|
|
16
|
-
faraday.headers =
|
14
|
+
faraday.headers = default_headers
|
17
15
|
faraday.request :url_encoded
|
18
16
|
faraday.adapter Faraday.default_adapter
|
19
17
|
faraday.use Middleware::ApiErrors
|
@@ -53,20 +51,8 @@ module Layer
|
|
53
51
|
)
|
54
52
|
end
|
55
53
|
|
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
|
-
|
64
54
|
def layer_patch_header
|
65
55
|
{ 'Content-Type' => 'application/vnd.layer-patch+json' }
|
66
56
|
end
|
67
|
-
|
68
|
-
def base_url
|
69
|
-
"#{DEFAULT_HOST}/apps/#{app_id}"
|
70
|
-
end
|
71
57
|
end
|
72
58
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Layer
|
2
2
|
module Platform
|
3
|
-
class Client
|
3
|
+
class Client < Layer::BaseClient
|
4
4
|
attr_accessor :api_token, :app_id
|
5
5
|
|
6
6
|
def initialize(options = {})
|
@@ -9,14 +9,6 @@ module Layer
|
|
9
9
|
@app_id = strip_layer_prefix(id)
|
10
10
|
end
|
11
11
|
|
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
|
-
|
20
12
|
def announcements
|
21
13
|
Layer::ResourceProxy.new(client, nil, Layer::Resources::Announcement)
|
22
14
|
end
|
@@ -33,6 +25,14 @@ module Layer
|
|
33
25
|
Layer::IdentityToken.new(options)
|
34
26
|
end
|
35
27
|
|
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
|
+
|
36
36
|
def inspect
|
37
37
|
"#<#{self.class} api_token=\"#{@api_token}\" app_id=\"#{@app_id}\">"
|
38
38
|
end
|
data/lib/layer/version.rb
CHANGED
@@ -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
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: layer-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jake Kelly
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -158,6 +158,7 @@ files:
|
|
158
158
|
- bin/setup
|
159
159
|
- layer-api.gemspec
|
160
160
|
- lib/layer/api.rb
|
161
|
+
- lib/layer/base_client.rb
|
161
162
|
- lib/layer/errors.rb
|
162
163
|
- lib/layer/http_client.rb
|
163
164
|
- lib/layer/identity_token.rb
|
@@ -170,7 +171,9 @@ files:
|
|
170
171
|
- lib/layer/resources/conversation.rb
|
171
172
|
- lib/layer/resources/message.rb
|
172
173
|
- lib/layer/resources/user.rb
|
174
|
+
- lib/layer/resources/webhook.rb
|
173
175
|
- lib/layer/version.rb
|
176
|
+
- lib/layer/webhooks/client.rb
|
174
177
|
homepage: https://github.com/cakejelly/layer-api
|
175
178
|
licenses:
|
176
179
|
- MIT
|