uni-sdk 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f133686b5f0658fb55dd663ae9e21265b30962850459576011158d4617408955
4
- data.tar.gz: 49acfe7169e8b12aed8195e2c530e25d595a3a32c58510fe436b17a938468146
3
+ metadata.gz: d98dcd92ba7b6133a759719b91c85310bef8aa82cdbe1f24f5aa23434f9b43a0
4
+ data.tar.gz: 5bbc4980ac4a9b81be41607ac010d513e491e129a5800f474a0b34593617398e
5
5
  SHA512:
6
- metadata.gz: 0f58800a7bd0fccd02ed8b99ff90a56a17c3a0e0b20b6367b108c5b9e47299ea28950bd83da6a1335a5a7f18221e1c7860492bb7f1a57adc28a9f2f2eeda9bab
7
- data.tar.gz: 71c5083c3357f64217e20833d8cca6b23986732ad60dc324c7b8661daad0bb8f5a0e8866f0fd2d6ee850d81197afe254952f7154f5ba410ac74a83b76903a3a6
6
+ metadata.gz: e01c4e43e705b69a6b9f0376a3957c705801103c96a8f626ef9de3d8ebeca05a618e37781b69f2271a4b320c8acc7b717f3de56a2af3c44fcbc2a389e6861483
7
+ data.tar.gz: f96bc1df810aca1e93caf8c89c82fb755a22ab5bdcb5a353ec330857e72af02aa8fc3d280e5f5607b49c97078155880b6702372adcf99c461bf680a11e742b9d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ .DS_STORE
2
+ .bundle/
3
+ .yardoc
4
+ _yardoc/
5
+ Gemfile.lock
6
+ coverage/
7
+ doc/
8
+ pkg/
9
+ spec/reports/
10
+ tmp/
11
+ bin/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uni-sdk.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
data/README.md ADDED
@@ -0,0 +1,73 @@
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
+ ### Send SMS
36
+
37
+ Send a text message to a single recipient.
38
+
39
+ ```ruby
40
+
41
+ require 'uni-sdk'
42
+
43
+ client = Uni::Client.new('your access key id', 'your access key secret')
44
+
45
+ begin
46
+ resp = client.messages.send({
47
+ to: 'your phone number', # in E.164 format
48
+ signature: 'your sender name',
49
+ content: 'Your verification code is 2048.'
50
+ })
51
+ puts resp.data
52
+ rescue Uni::UniError => e
53
+ puts 'Exception: ' + e.message
54
+ end
55
+
56
+ ```
57
+
58
+ ## Reference
59
+
60
+ ### Other Unimatrix SDKs
61
+
62
+ To find Unimatrix SDKs in other programming languages, check out the list below:
63
+
64
+ - [Java](https://github.com/unimtx/uni-java-sdk)
65
+ - [Go](https://github.com/unimtx/uni-go-sdk)
66
+ - [Node.js](https://github.com/unimtx/uni-node-sdk)
67
+ - [Python](https://github.com/unimtx/uni-python-sdk)
68
+ - [PHP](https://github.com/unimtx/uni-php-sdk)
69
+ - [.NET](https://github.com/unimtx/uni-dotnet-sdk)
70
+
71
+ ## License
72
+
73
+ This library is released under the [MIT License](https://github.com/unimtx/uni-ruby-sdk/blob/main/LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task :default => :spec
data/examples/send.rb ADDED
@@ -0,0 +1,14 @@
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
+ signature: 'your sender name',
9
+ content: 'Your verification code is 2048.'
10
+ })
11
+ puts resp.data
12
+ rescue Uni::UniError => e
13
+ puts 'Exception: ' + e.message
14
+ end
@@ -0,0 +1,62 @@
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
13
+ @access_key_secret = access_key_secret
14
+ @endpoint = 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
+ end
62
+ 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,13 @@
1
+ module Uni
2
+ module Modal
3
+ class Message
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def send(data={})
9
+ @client.request('sms.message.send', data)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ require_relative 'modal/message'
2
+
3
+ module Uni
4
+ module Model
5
+ end
6
+ end
@@ -0,0 +1,23 @@
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
+
7
+ def initialize(response)
8
+ @raw = response
9
+ @status = response.status
10
+ @headers = response.headers
11
+ @body = response.body
12
+ @request_id = @headers[@@request_id_header_key]
13
+ @code = @body['code']
14
+ @message = @body['message']
15
+ @data = @body['data']
16
+
17
+ if @status < 200 || @status >= 300 || @code != '0'
18
+ raise UniError.new(@message || response.reason_phrase, self)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Uni
2
+ VERSION = '0.2.1'
3
+ end
data/lib/uni-sdk.rb ADDED
@@ -0,0 +1,8 @@
1
+ require_relative 'uni-sdk/version'
2
+ require_relative 'uni-sdk/client'
3
+ require_relative 'uni-sdk/error'
4
+ require_relative 'uni-sdk/modal'
5
+ require_relative 'uni-sdk/response'
6
+
7
+ module Uni
8
+ end
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Unimatrix
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-17 00:00:00.000000000 Z
11
+ date: 2022-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -40,7 +40,20 @@ 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/send.rb
49
+ - lib/uni-sdk.rb
50
+ - lib/uni-sdk/client.rb
51
+ - lib/uni-sdk/error.rb
52
+ - lib/uni-sdk/modal.rb
53
+ - lib/uni-sdk/modal/message.rb
54
+ - lib/uni-sdk/response.rb
55
+ - lib/uni-sdk/version.rb
56
+ - uni-sdk.gemspec
44
57
  homepage: https://unimtx.com
45
58
  licenses:
46
59
  - MIT