Class: Uni::Client
- Inherits:
-
Object
- Object
- Uni::Client
- Defined in:
- lib/uni-sdk/client.rb
Constant Summary collapse
- @@name =
'uni-ruby-sdk'- @@default_endpoint =
'https://api.unimtx.com'- @@default_signing_algorithm =
'hmac-sha256'
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
-
#access_key_secret ⇒ Object
Returns the value of attribute access_key_secret.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#signing_algorithm ⇒ Object
Returns the value of attribute signing_algorithm.
Instance Method Summary collapse
- #_sign(query = {}) ⇒ Object
-
#initialize(access_key_id = nil, access_key_secret = nil, endpoint = nil, signing_algorithm = nil) ⇒ Client
constructor
A new instance of Client.
- #messages ⇒ Object
- #otp ⇒ Object
- #request(action, data = {}) ⇒ Object
Constructor Details
#initialize(access_key_id = nil, access_key_secret = nil, endpoint = nil, signing_algorithm = nil) ⇒ Client
11 12 13 14 15 16 17 |
# File 'lib/uni-sdk/client.rb', line 11 def initialize(access_key_id=nil, access_key_secret=nil, endpoint=nil, signing_algorithm=nil) @access_key_id = access_key_id || ENV['UNIMTX_ACCESS_KEY_ID'] @access_key_secret = access_key_secret || ENV['UNIMTX_ACCESS_KEY_SECRET'] @endpoint = endpoint || ENV['UNIMTX_ENDPOINT'] || @@default_endpoint @signing_algorithm = signing_algorithm || @@default_signing_algorithm @user_agent = @@name + '/' + Uni::VERSION end |
Instance Attribute Details
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
9 10 11 |
# File 'lib/uni-sdk/client.rb', line 9 def access_key_id @access_key_id end |
#access_key_secret ⇒ Object
Returns the value of attribute access_key_secret.
9 10 11 |
# File 'lib/uni-sdk/client.rb', line 9 def access_key_secret @access_key_secret end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
9 10 11 |
# File 'lib/uni-sdk/client.rb', line 9 def endpoint @endpoint end |
#signing_algorithm ⇒ Object
Returns the value of attribute signing_algorithm.
9 10 11 |
# File 'lib/uni-sdk/client.rb', line 9 def signing_algorithm @signing_algorithm end |
Instance Method Details
#_sign(query = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/uni-sdk/client.rb', line 19 def _sign(query={}) algorithm = @signing_algorithm.split('-')[1] query['algorithm'] = @signing_algorithm query['timestamp'] = Time.now.strftime('%s%L') query['nonce'] = rand(36**16).to_s(36) sorted_query = query.sort_by { |k, v| k.to_s } str_to_sign = URI.encode_www_form(sorted_query) query['signature'] = OpenSSL::HMAC.hexdigest(algorithm, @access_key_secret, str_to_sign) end |
#messages ⇒ Object
58 59 60 |
# File 'lib/uni-sdk/client.rb', line 58 def () Uni::Modal::Message.new(self) end |
#otp ⇒ Object
62 63 64 |
# File 'lib/uni-sdk/client.rb', line 62 def otp() Uni::Modal::Otp.new(self) end |
#request(action, data = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/uni-sdk/client.rb', line 31 def request(action, data={}) query = { action: action, accessKeyId: @access_key_id } if !@access_key_secret.nil? _sign(query) end conn = Faraday.new( url: @endpoint, params: query, headers: { 'User-Agent': @user_agent, 'Content-Type': 'application/json;charset=utf-8', 'Accept': 'application/json', } ) do |f| f.response :json end response = conn.post('/', data.to_json) Uni::Response.new(response) end |