class Cruz::Client
License Service API Client
Public Class Methods
new(config = {})
click to toggle source
Public Instance Methods
keys()
click to toggle source
# File lib/cruz/client.rb, line 41 def keys Keys.new(self) end
rest_get_with_token(path, query_params = {}, headers = {})
click to toggle source
# File lib/cruz/client.rb, line 22 def rest_get_with_token(path, query_params = {}, headers = {}) api_headers = token.merge(headers) headers[:params] = query_params unless query_params.empty? response = RestClient.get("#{Cruz.api_base}#{path}", api_headers) from_rest_client_response(response) end
rest_post_with_token(path, body = {}, headers = {})
click to toggle source
# File lib/cruz/client.rb, line 35 def rest_post_with_token(path, body = {}, headers = {}) api_headers = token.merge(headers) response = RestClient.post("#{Cruz.api_base}#{path}", encode_json(body), api_headers) from_rest_client_response(response) end
rest_put_with_token(path, body = {}, headers = {})
click to toggle source
# File lib/cruz/client.rb, line 29 def rest_put_with_token(path, body = {}, headers = {}) api_headers = token.merge(headers) response = RestClient.put("#{Cruz.api_base}#{path}", encode_json(body), api_headers) from_rest_client_response(response) end
Private Instance Methods
cruz_signature()
click to toggle source
# File lib/cruz/client.rb, line 70 def cruz_signature key = Digest::SHA256.digest(@username) iv = Digest::MD5.digest(@app_id) cipher = OpenSSL::Cipher::AES.new(256, :CBC) cipher.encrypt cipher.key = key cipher.iv = iv encrypted = cipher.update(@api_key) + cipher.final Base64.strict_encode64(encrypted) end
decode_json(json)
click to toggle source
# File lib/cruz/client.rb, line 66 def decode_json(json) Oj.load(json) end
encode_json(data)
click to toggle source
# File lib/cruz/client.rb, line 62 def encode_json(data) Oj.dump(data, mode: :compat) end
from_rest_client_response(response)
click to toggle source
# File lib/cruz/client.rb, line 102 def from_rest_client_response(response) if response.code < 200 && response.code >= 400 raise "Invalid response object from API: #{response.body}" \ "(HTTP response code was #{response.code})" end # default decode by json return decode_json(response.body) unless response.headers[:content_type] guard_json_content_type do |body| decode_json(body) end end
generate_token()
click to toggle source
# File lib/cruz/client.rb, line 47 def generate_token unless token_valid? payload = { app_id: @app_id, signature: cruz_signature } response = RestClient.post( "#{Cruz.api_base}/token", payload, headers: { accept: 'application/json' } ) parsed_response = decode_json(response.body) @id_token = parsed_response['token'] @expires_on = Time.parse(parsed_response['expiration']) end raw_token end
guard_json_content_type(response) { |body| ... }
click to toggle source
# File lib/cruz/client.rb, line 116 def guard_json_content_type(response) return unless (response.headers[:content_type] =~ %r{application\/json}) > 0 yield response.body end
raw_token()
click to toggle source
# File lib/cruz/client.rb, line 81 def raw_token if token_valid? { token: @id_token, appId: @app_id, accept: 'application/json' } else {} end end
token()
click to toggle source
# File lib/cruz/client.rb, line 93 def token generate_token unless token_valid? raw_token end
token_valid?()
click to toggle source
# File lib/cruz/client.rb, line 98 def token_valid? @id_token && @expires_on > Time.now end