uni-sdk 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +11 -0
- data/Gemfile +6 -0
- data/README.md +117 -0
- data/Rakefile +2 -0
- data/examples/otp.rb +24 -0
- data/examples/send.rb +13 -0
- data/lib/uni-sdk/client.rb +66 -0
- data/lib/uni-sdk/error.rb +16 -0
- data/lib/uni-sdk/modal/message.rb +13 -0
- data/lib/uni-sdk/modal/otp.rb +19 -0
- data/lib/uni-sdk/modal.rb +7 -0
- data/lib/uni-sdk/response.rb +25 -0
- data/lib/uni-sdk/version.rb +3 -0
- data/lib/uni-sdk.rb +8 -0
- data/uni-sdk.gemspec +24 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cb626b3043c1497ec5732f0d0d86507c269a0ec30cad9cbacf48beb6cb531b0
|
4
|
+
data.tar.gz: 0cf681f5fb70e49bac70024ed0d1f9a25738387ddac907f36cb9abcb0f8dd0a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71390fa7276f0a95ebdd5db4334371401b3a2127b53168e65aced5e27ed07c5f04d61221fc87b7e0f5e2308ba5b8462bb7ee803f695a6f40b02f6da1209a053e
|
7
|
+
data.tar.gz: fa7cbd789c467a8c8ded1c699372f884e4535c93a782005951a5ece86d4160274dea9debd2cd721a148d2469c02c64d7bec5a9d8980285592928531acc539f9a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# Unimatrix Ruby SDK
|
2
|
+
|
3
|
+
[![Gem Version](https://img.shields.io/gem/v/uni-sdk.svg)](https://rubygems.org/gems/uni-sdk) [![Release](https://img.shields.io/github/release/unimtx/uni-ruby-sdk.svg)](https://github.com/unimtx/uni-ruby-sdk/releases/latest) [![GitHub license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/unimtx/uni-ruby-sdk/blob/main/LICENSE)
|
4
|
+
|
5
|
+
The Unimatrix Ruby SDK provides convenient access to integrate communication capabilities into your Ruby applications using the Unimatrix HTTP API. The SDK provides support for sending SMS, 2FA verification, and phone number lookup.
|
6
|
+
|
7
|
+
## Getting started
|
8
|
+
|
9
|
+
Before you begin, you need an [Unimatrix](https://www.unimtx.com/) account. If you don't have one yet, you can [sign up](https://www.unimtx.com/signup?s=ruby.sdk.gh) for an Unimatrix account and get free credits to get you started.
|
10
|
+
|
11
|
+
## Documentation
|
12
|
+
|
13
|
+
Check out the documentation at [unimtx.com/docs](https://www.unimtx.com/docs) for a quick overview.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
The recommended way to install the Unimatrix SDK for Ruby is to use the gem package manager, which is available on [RubyGems](https://rubygems.org/gems/uni-sdk).
|
18
|
+
|
19
|
+
Run the following command to add `uni-sdk` as a dependency to your project:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
gem install uni-sdk
|
23
|
+
```
|
24
|
+
|
25
|
+
If you are installing via Bundler, add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'uni-sdk'
|
29
|
+
```
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
The following example shows how to use the Unimatrix Ruby SDK to interact with Unimatrix services.
|
34
|
+
|
35
|
+
### Initialize a client
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'uni-sdk'
|
39
|
+
|
40
|
+
client = Uni::Client.new('your access key id', 'your access key secret')
|
41
|
+
```
|
42
|
+
|
43
|
+
or you can configure your credentials by environment variables:
|
44
|
+
|
45
|
+
```sh
|
46
|
+
export UNIMTX_ACCESS_KEY_ID=your_access_key_id
|
47
|
+
export UNIMTX_ACCESS_KEY_SECRET=your_access_key_secret
|
48
|
+
```
|
49
|
+
|
50
|
+
### Send SMS
|
51
|
+
|
52
|
+
Send a text message to a single recipient.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
|
56
|
+
require 'uni-sdk'
|
57
|
+
|
58
|
+
client = Uni::Client.new('your access key id', 'your access key secret')
|
59
|
+
|
60
|
+
begin
|
61
|
+
resp = client.messages.send({
|
62
|
+
to: '+1206880xxxx', # in E.164 format
|
63
|
+
text: 'Your verification code is 2048.'
|
64
|
+
})
|
65
|
+
puts resp.data
|
66
|
+
rescue Uni::UniError => e
|
67
|
+
puts 'Exception: ' + e.message
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Send verification code
|
72
|
+
|
73
|
+
Send a one-time passcode (OTP) to a recipient. The following example will automatically generate a verification code.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
require 'uni-sdk'
|
77
|
+
|
78
|
+
client = Uni::Client.new()
|
79
|
+
|
80
|
+
resp = client.otp.send({
|
81
|
+
to: '+1206880xxxx'
|
82
|
+
})
|
83
|
+
puts resp.data
|
84
|
+
```
|
85
|
+
|
86
|
+
### Check verification code
|
87
|
+
|
88
|
+
Verify the one-time passcode (OTP) that a user provided. The following example will check whether the user-provided verification code is correct.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
require 'uni-sdk'
|
92
|
+
|
93
|
+
client = Uni::Client.new()
|
94
|
+
|
95
|
+
resp = client.otp.verify({
|
96
|
+
to: '+1206880xxxx',
|
97
|
+
code: '123456' # the code user provided
|
98
|
+
})
|
99
|
+
puts resp.valid
|
100
|
+
```
|
101
|
+
|
102
|
+
## Reference
|
103
|
+
|
104
|
+
### Other Unimatrix SDKs
|
105
|
+
|
106
|
+
To find Unimatrix SDKs in other programming languages, check out the list below:
|
107
|
+
|
108
|
+
- [Java](https://github.com/unimtx/uni-java-sdk)
|
109
|
+
- [Go](https://github.com/unimtx/uni-go-sdk)
|
110
|
+
- [Node.js](https://github.com/unimtx/uni-node-sdk)
|
111
|
+
- [Python](https://github.com/unimtx/uni-python-sdk)
|
112
|
+
- [PHP](https://github.com/unimtx/uni-php-sdk)
|
113
|
+
- [.NET](https://github.com/unimtx/uni-dotnet-sdk)
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
This library is released under the [MIT License](https://github.com/unimtx/uni-ruby-sdk/blob/main/LICENSE).
|
data/Rakefile
ADDED
data/examples/otp.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'uni-sdk'
|
2
|
+
|
3
|
+
client = Uni::Client.new('your access key id', 'your access key secret')
|
4
|
+
|
5
|
+
# send a verification code to a recipient
|
6
|
+
begin
|
7
|
+
resp = client.otp.send({
|
8
|
+
to: 'your phone number' # in E.164 format
|
9
|
+
})
|
10
|
+
puts resp.data
|
11
|
+
rescue Uni::UniError => e
|
12
|
+
puts 'Exception: ' + e.message
|
13
|
+
end
|
14
|
+
|
15
|
+
# verify a verification code
|
16
|
+
begin
|
17
|
+
resp = client.otp.verify({
|
18
|
+
to: 'your phone number', # in E.164 format
|
19
|
+
code: 'the code you received'
|
20
|
+
})
|
21
|
+
puts resp.valid
|
22
|
+
rescue Uni::UniError => e
|
23
|
+
puts 'Exception: ' + e.message
|
24
|
+
end
|
data/examples/send.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'uni-sdk'
|
2
|
+
|
3
|
+
client = Uni::Client.new('your access key id', 'your access key secret')
|
4
|
+
|
5
|
+
begin
|
6
|
+
resp = client.messages.send({
|
7
|
+
to: 'your phone number', # in E.164 format
|
8
|
+
text: 'Your verification code is 2048.'
|
9
|
+
})
|
10
|
+
puts resp.data
|
11
|
+
rescue Uni::UniError => e
|
12
|
+
puts 'Exception: ' + e.message
|
13
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Uni
|
4
|
+
class Client
|
5
|
+
@@name = 'uni-ruby-sdk'
|
6
|
+
@@default_endpoint = 'https://api.unimtx.com'
|
7
|
+
@@default_signing_algorithm = 'hmac-sha256'
|
8
|
+
|
9
|
+
attr_accessor :access_key_id, :access_key_secret, :endpoint, :signing_algorithm
|
10
|
+
|
11
|
+
def initialize(access_key_id=nil, access_key_secret=nil, endpoint=nil, signing_algorithm=nil)
|
12
|
+
@access_key_id = access_key_id || ENV['UNIMTX_ACCESS_KEY_ID']
|
13
|
+
@access_key_secret = access_key_secret || ENV['UNIMTX_ACCESS_KEY_SECRET']
|
14
|
+
@endpoint = endpoint || ENV['UNIMTX_ENDPOINT'] || @@default_endpoint
|
15
|
+
@signing_algorithm = signing_algorithm || @@default_signing_algorithm
|
16
|
+
@user_agent = @@name + '/' + Uni::VERSION
|
17
|
+
end
|
18
|
+
|
19
|
+
def _sign(query={})
|
20
|
+
algorithm = @signing_algorithm.split('-')[1]
|
21
|
+
query['algorithm'] = @signing_algorithm
|
22
|
+
query['timestamp'] = Time.now.strftime('%s%L')
|
23
|
+
query['nonce'] = rand(36**16).to_s(36)
|
24
|
+
|
25
|
+
sorted_query = query.sort_by { |k, v| k.to_s }
|
26
|
+
str_to_sign = URI.encode_www_form(sorted_query)
|
27
|
+
|
28
|
+
query['signature'] = OpenSSL::HMAC.hexdigest(algorithm, @access_key_secret, str_to_sign)
|
29
|
+
end
|
30
|
+
|
31
|
+
def request(action, data={})
|
32
|
+
query = {
|
33
|
+
action: action,
|
34
|
+
accessKeyId: @access_key_id
|
35
|
+
}
|
36
|
+
|
37
|
+
if !@access_key_secret.nil?
|
38
|
+
_sign(query)
|
39
|
+
end
|
40
|
+
|
41
|
+
conn = Faraday.new(
|
42
|
+
url: @endpoint,
|
43
|
+
params: query,
|
44
|
+
headers: {
|
45
|
+
'User-Agent': @user_agent,
|
46
|
+
'Content-Type': 'application/json;charset=utf-8',
|
47
|
+
'Accept': 'application/json',
|
48
|
+
}
|
49
|
+
) do |f|
|
50
|
+
f.response :json
|
51
|
+
end
|
52
|
+
|
53
|
+
response = conn.post('/', data.to_json)
|
54
|
+
|
55
|
+
Uni::Response.new(response)
|
56
|
+
end
|
57
|
+
|
58
|
+
def messages()
|
59
|
+
Uni::Modal::Message.new(self)
|
60
|
+
end
|
61
|
+
|
62
|
+
def otp()
|
63
|
+
Uni::Modal::Otp.new(self)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Uni
|
2
|
+
class UniError < StandardError
|
3
|
+
attr_reader :message, :response, :code, :request_id, :message
|
4
|
+
|
5
|
+
def initialize(message, response=nil)
|
6
|
+
@response = response
|
7
|
+
@code = (response.nil? ? nil : response.code) || '-1'
|
8
|
+
@request_id = response.nil? ? nil : response.request_id
|
9
|
+
@message = '[' + @code + '] ' + message
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
message
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Uni
|
2
|
+
module Modal
|
3
|
+
class Otp
|
4
|
+
def initialize(client)
|
5
|
+
@client = client
|
6
|
+
end
|
7
|
+
|
8
|
+
def send(data={})
|
9
|
+
@client.request('otp.send', data)
|
10
|
+
end
|
11
|
+
|
12
|
+
def verify(data={})
|
13
|
+
resp = @client.request('otp.verify', data)
|
14
|
+
resp.valid = resp.data.nil? ? false : resp.data['valid']
|
15
|
+
resp
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Uni
|
2
|
+
class Response
|
3
|
+
@@request_id_header_key = 'x-uni-request-id'
|
4
|
+
|
5
|
+
attr_reader :raw, :status, :request_id, :code, :message, :data
|
6
|
+
attr_accessor :valid
|
7
|
+
|
8
|
+
def initialize(response)
|
9
|
+
@raw = response
|
10
|
+
@status = response.status
|
11
|
+
@headers = response.headers
|
12
|
+
@body = response.body
|
13
|
+
@request_id = @headers[@@request_id_header_key]
|
14
|
+
@code = @body['code']
|
15
|
+
@message = @body['message']
|
16
|
+
@data = @body['data']
|
17
|
+
@valid = false
|
18
|
+
|
19
|
+
if @status < 200 || @status >= 300 || @code != '0'
|
20
|
+
raise UniError.new(@message || response.reason_phrase, self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/uni-sdk.rb
ADDED
data/uni-sdk.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'lib/uni-sdk/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'uni-sdk'
|
5
|
+
spec.version = Uni::VERSION
|
6
|
+
spec.authors = ['Unimatrix']
|
7
|
+
spec.email = ['dev@unimtx.com']
|
8
|
+
|
9
|
+
spec.summary = 'Unimatrix Ruby SDK'
|
10
|
+
spec.description = 'The official Unimatrix SDK for Ruby, provides convenient access to integrate communication capabilities into your Ruby applications using the Unimatrix HTTP API. The SDK provides support for sending SMS, 2FA verification, and phone number lookup.'
|
11
|
+
spec.homepage = 'https://unimtx.com'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
14
|
+
|
15
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
16
|
+
spec.metadata['documentation_uri'] = 'https://www.unimtx.com/docs'
|
17
|
+
spec.metadata['source_code_uri'] = 'https://github.com/unimtx/uni-ruby-sdk'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
spec.bindir = 'bin'
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_dependency('faraday', '>= 2.7.0', '< 3.0')
|
24
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uni-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Unimatrix
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -40,7 +40,22 @@ executables: []
|
|
40
40
|
extensions: []
|
41
41
|
extra_rdoc_files: []
|
42
42
|
files:
|
43
|
+
- ".gitignore"
|
44
|
+
- Gemfile
|
43
45
|
- LICENSE
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- examples/otp.rb
|
49
|
+
- examples/send.rb
|
50
|
+
- lib/uni-sdk.rb
|
51
|
+
- lib/uni-sdk/client.rb
|
52
|
+
- lib/uni-sdk/error.rb
|
53
|
+
- lib/uni-sdk/modal.rb
|
54
|
+
- lib/uni-sdk/modal/message.rb
|
55
|
+
- lib/uni-sdk/modal/otp.rb
|
56
|
+
- lib/uni-sdk/response.rb
|
57
|
+
- lib/uni-sdk/version.rb
|
58
|
+
- uni-sdk.gemspec
|
44
59
|
homepage: https://unimtx.com
|
45
60
|
licenses:
|
46
61
|
- MIT
|