layer-api 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIGRATING.md +212 -0
- data/README.md +166 -43
- data/layer-api.gemspec +3 -3
- data/lib/layer/api.rb +16 -10
- data/lib/layer/errors.rb +43 -0
- data/lib/layer/http_client.rb +70 -0
- data/lib/layer/identity_token.rb +53 -0
- data/lib/layer/middleware/api_errors.rb +13 -0
- data/lib/layer/platform/client.rb +57 -0
- data/lib/layer/resource.rb +80 -0
- data/lib/layer/resource_proxy.rb +20 -0
- data/lib/layer/resources/announcement.rb +6 -0
- data/lib/layer/resources/block.rb +31 -0
- data/lib/layer/resources/conversation.rb +9 -0
- data/lib/layer/resources/message.rb +6 -0
- data/lib/layer/resources/user.rb +21 -0
- data/lib/layer/{api/version.rb → version.rb} +1 -1
- metadata +19 -15
- data/lib/layer/api/client.rb +0 -25
- data/lib/layer/api/client/announcements.rb +0 -9
- data/lib/layer/api/client/conversations.rb +0 -30
- data/lib/layer/api/client/identity_token.rb +0 -61
- data/lib/layer/api/client/users.rb +0 -18
- data/lib/layer/api/configuration.rb +0 -30
- data/lib/layer/api/connection.rb +0 -44
- data/lib/layer/api/error.rb +0 -39
- data/lib/layer/api/middleware/api_errors.rb +0 -15
@@ -0,0 +1,21 @@
|
|
1
|
+
module Layer
|
2
|
+
module Resources
|
3
|
+
class User < Layer::Resource
|
4
|
+
def self.find(url, id)
|
5
|
+
new({"id" => id, "url" => "users/#{id}"})
|
6
|
+
end
|
7
|
+
|
8
|
+
def blocks
|
9
|
+
Layer::ResourceProxy.new(self, Layer::Resources::Block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def conversations
|
13
|
+
Layer::ResourceProxy.new(self, Layer::Resources::Conversation)
|
14
|
+
end
|
15
|
+
|
16
|
+
def messages
|
17
|
+
Layer::ResourceProxy.new(self, Layer::Resources::Message)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
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.4.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:
|
11
|
+
date: 2016-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -140,7 +140,7 @@ dependencies:
|
|
140
140
|
- - "~>"
|
141
141
|
- !ruby/object:Gem::Version
|
142
142
|
version: 1.5.1
|
143
|
-
description: Simple wrapper for the Layer
|
143
|
+
description: Simple wrapper for the Layer's Web API's
|
144
144
|
email:
|
145
145
|
- jake.kelly10@gmail.com
|
146
146
|
executables: []
|
@@ -151,22 +151,26 @@ files:
|
|
151
151
|
- ".rspec"
|
152
152
|
- ".travis.yml"
|
153
153
|
- Gemfile
|
154
|
+
- MIGRATING.md
|
154
155
|
- README.md
|
155
156
|
- Rakefile
|
156
157
|
- bin/console
|
157
158
|
- bin/setup
|
158
159
|
- layer-api.gemspec
|
159
160
|
- lib/layer/api.rb
|
160
|
-
- lib/layer/
|
161
|
-
- lib/layer/
|
162
|
-
- lib/layer/
|
163
|
-
- lib/layer/
|
164
|
-
- lib/layer/
|
165
|
-
- lib/layer/
|
166
|
-
- lib/layer/
|
167
|
-
- lib/layer/
|
168
|
-
- lib/layer/
|
169
|
-
- lib/layer/
|
161
|
+
- lib/layer/errors.rb
|
162
|
+
- lib/layer/http_client.rb
|
163
|
+
- lib/layer/identity_token.rb
|
164
|
+
- lib/layer/middleware/api_errors.rb
|
165
|
+
- lib/layer/platform/client.rb
|
166
|
+
- lib/layer/resource.rb
|
167
|
+
- lib/layer/resource_proxy.rb
|
168
|
+
- lib/layer/resources/announcement.rb
|
169
|
+
- lib/layer/resources/block.rb
|
170
|
+
- lib/layer/resources/conversation.rb
|
171
|
+
- lib/layer/resources/message.rb
|
172
|
+
- lib/layer/resources/user.rb
|
173
|
+
- lib/layer/version.rb
|
170
174
|
homepage: https://github.com/cakejelly/layer-api
|
171
175
|
licenses:
|
172
176
|
- MIT
|
@@ -187,8 +191,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
187
191
|
version: '0'
|
188
192
|
requirements: []
|
189
193
|
rubyforge_project:
|
190
|
-
rubygems_version: 2.4.
|
194
|
+
rubygems_version: 2.4.5.1
|
191
195
|
signing_key:
|
192
196
|
specification_version: 4
|
193
|
-
summary: A ruby toolkit for Layer's
|
197
|
+
summary: A ruby toolkit for Layer's Web API's (https://developer.layer.com/docs)
|
194
198
|
test_files: []
|
data/lib/layer/api/client.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require "layer/api/client/conversations"
|
2
|
-
require "layer/api/client/announcements"
|
3
|
-
require "layer/api/client/users"
|
4
|
-
require "layer/api/client/identity_token"
|
5
|
-
|
6
|
-
module Layer
|
7
|
-
module Api
|
8
|
-
class Client
|
9
|
-
include Layer::Api::Connection
|
10
|
-
include Layer::Api::Configuration
|
11
|
-
|
12
|
-
include Layer::Api::Conversations
|
13
|
-
include Layer::Api::Announcements
|
14
|
-
include Layer::Api::Users
|
15
|
-
|
16
|
-
attr_accessor :api_token, :app_id
|
17
|
-
|
18
|
-
def initialize(options = {})
|
19
|
-
@api_token = options[:api_token] || ENV['LAYER_API_TOKEN']
|
20
|
-
@app_id = options[:app_id] || ENV['LAYER_APP_ID']
|
21
|
-
strip_layer_prefix(@app_id)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Layer
|
2
|
-
module Api
|
3
|
-
module Conversations
|
4
|
-
def get_conversation(conversation_id)
|
5
|
-
get("conversations/#{conversation_id}")
|
6
|
-
end
|
7
|
-
|
8
|
-
def create_conversation(params = {})
|
9
|
-
post("conversations", body: params.to_json)
|
10
|
-
end
|
11
|
-
|
12
|
-
def edit_conversation(conversation_id, params = {})
|
13
|
-
patch("conversations/#{conversation_id}",
|
14
|
-
body: params.to_json,
|
15
|
-
headers: layer_patch_header
|
16
|
-
)
|
17
|
-
end
|
18
|
-
|
19
|
-
def send_message(conversation_id, message = {})
|
20
|
-
post("conversations/#{conversation_id}/messages",
|
21
|
-
body: message.to_json
|
22
|
-
)
|
23
|
-
end
|
24
|
-
|
25
|
-
def get_stripped_id(raw_id)
|
26
|
-
raw_id.sub("layer:///conversations/", "")
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
module Layer
|
2
|
-
module Api
|
3
|
-
class IdentityToken
|
4
|
-
attr_reader :user_id, :nonce, :expires_at
|
5
|
-
|
6
|
-
def initialize(options = {})
|
7
|
-
@user_id = options[:user_id]
|
8
|
-
@nonce = options[:nonce]
|
9
|
-
@expires_at = (options[:expires_at] || Time.now+(1209600))
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_s
|
13
|
-
get_jwt
|
14
|
-
end
|
15
|
-
|
16
|
-
def layer_key_id
|
17
|
-
ENV['LAYER_KEY_ID']
|
18
|
-
end
|
19
|
-
|
20
|
-
def layer_provider_id
|
21
|
-
ENV['LAYER_PROVIDER_ID']
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def get_jwt
|
27
|
-
JWT.encode(claim, private_key, 'RS256', headers)
|
28
|
-
end
|
29
|
-
|
30
|
-
def headers
|
31
|
-
{
|
32
|
-
typ: 'JWT',
|
33
|
-
cty: 'layer-eit;v=1',
|
34
|
-
kid: layer_key_id
|
35
|
-
}
|
36
|
-
end
|
37
|
-
|
38
|
-
def claim
|
39
|
-
{
|
40
|
-
iss: layer_provider_id,
|
41
|
-
prn: user_id.to_s,
|
42
|
-
iat: Time.now.to_i,
|
43
|
-
exp: expires_at.to_i,
|
44
|
-
nce: nonce
|
45
|
-
}
|
46
|
-
end
|
47
|
-
|
48
|
-
def private_key
|
49
|
-
# Cloud66 stores newlines as \n instead of \\n
|
50
|
-
key = ENV['LAYER_PRIVATE_KEY'].dup
|
51
|
-
OpenSSL::PKey::RSA.new(key.gsub!("\\n","\n"))
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
class Client
|
56
|
-
def generate_identity_token(options = {})
|
57
|
-
IdentityToken.new(options).to_s
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Layer
|
2
|
-
module Api
|
3
|
-
module Users
|
4
|
-
def get_blocklist(user_id)
|
5
|
-
get("users/#{user_id}/blocks")
|
6
|
-
end
|
7
|
-
|
8
|
-
def block_user(owner_id, user_id)
|
9
|
-
params = { user_id: user_id }
|
10
|
-
post("users/#{owner_id}/blocks", body: params.to_json)
|
11
|
-
end
|
12
|
-
|
13
|
-
def unblock_user(owner_id, user_id)
|
14
|
-
delete("users/#{owner_id}/blocks/#{user_id}")
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'securerandom'
|
2
|
-
|
3
|
-
module Layer
|
4
|
-
module Api
|
5
|
-
module Configuration
|
6
|
-
DEFAULT_HOST = "https://api.layer.com"
|
7
|
-
|
8
|
-
def default_layer_headers
|
9
|
-
{
|
10
|
-
'Accept' => 'application/vnd.layer+json; version=1.0',
|
11
|
-
'Authorization' => "Bearer #{api_token}",
|
12
|
-
'Content-Type' => 'application/json',
|
13
|
-
'If-None-Match' => SecureRandom.uuid
|
14
|
-
}
|
15
|
-
end
|
16
|
-
|
17
|
-
def layer_patch_header
|
18
|
-
{ 'Content-Type' => 'application/vnd.layer-patch+json' }
|
19
|
-
end
|
20
|
-
|
21
|
-
def base_url
|
22
|
-
"#{DEFAULT_HOST}/apps/#{app_id}"
|
23
|
-
end
|
24
|
-
|
25
|
-
def strip_layer_prefix(string)
|
26
|
-
string.split("/").last if string
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
data/lib/layer/api/connection.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
module Layer
|
2
|
-
module Api
|
3
|
-
module Connection
|
4
|
-
def connection
|
5
|
-
@connection ||= Faraday.new(url: base_url) do |faraday|
|
6
|
-
faraday.headers = default_layer_headers
|
7
|
-
faraday.request :url_encoded
|
8
|
-
faraday.adapter Faraday.default_adapter
|
9
|
-
faraday.use Middleware::ApiErrors
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def get(url, options = {})
|
14
|
-
call(:get, url, options)
|
15
|
-
end
|
16
|
-
|
17
|
-
def post(url, options = {})
|
18
|
-
call(:post, url, options)
|
19
|
-
end
|
20
|
-
|
21
|
-
def patch(url, options = {})
|
22
|
-
call(:patch, url, options)
|
23
|
-
end
|
24
|
-
|
25
|
-
def delete(url)
|
26
|
-
call(:delete, url, options = {})
|
27
|
-
end
|
28
|
-
|
29
|
-
def call(method, url, options = {})
|
30
|
-
response = run_request(method, url, options)
|
31
|
-
response.body.empty? ? nil : JSON.parse(response.body)
|
32
|
-
end
|
33
|
-
|
34
|
-
def run_request(method, url, options = {})
|
35
|
-
connection.run_request(
|
36
|
-
method,
|
37
|
-
url,
|
38
|
-
options[:body],
|
39
|
-
options[:headers]
|
40
|
-
)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
data/lib/layer/api/error.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
module Layer
|
2
|
-
module Api
|
3
|
-
class Error < StandardError
|
4
|
-
def self.from_response(response)
|
5
|
-
status = response[:status]
|
6
|
-
|
7
|
-
if klass = case status
|
8
|
-
when 400 then Layer::Api::BadRequest
|
9
|
-
when 404 then Layer::Api::NotFound
|
10
|
-
when 500..599 then Layer::Api::ServerError
|
11
|
-
else self
|
12
|
-
end
|
13
|
-
klass.new(response)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def initialize(response)
|
18
|
-
@response = response
|
19
|
-
super(build_error_message)
|
20
|
-
end
|
21
|
-
|
22
|
-
def response
|
23
|
-
@response
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def build_error_message
|
29
|
-
message = "Layer responded with status "
|
30
|
-
message << "#{@response[:status]}: #{@response[:body]}"
|
31
|
-
message
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class BadRequest < Error; end
|
36
|
-
class NotFound < Error; end
|
37
|
-
class ServerError < Error; end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Layer
|
2
|
-
module Api
|
3
|
-
module Middleware
|
4
|
-
class ApiErrors < Faraday::Response::Middleware
|
5
|
-
ERROR_CODES = 400...600
|
6
|
-
|
7
|
-
def on_complete(response)
|
8
|
-
if ERROR_CODES.include?(response.status)
|
9
|
-
raise Layer::Api::Error.from_response(response)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|