class Bitzlato::Client
Constants
- WrongResponse
Attributes
email[R]
home_url[R]
jwk[R]
uid[R]
Public Class Methods
new(home_url: , key: , logger: false, email: nil, uid: nil, adapter: nil)
click to toggle source
# File lib/bitzlato/client.rb, line 14 def initialize(home_url: , key: , logger: false, email: nil, uid: nil, adapter: nil) raise ArgumentError, 'email or uid must be presented' if uid.nil? && email.nil? @email = email @uid = uid @jwk = JWT::JWK.import key @home_url = home_url @adapter = adapter || Faraday.default_adapter if logger == true @logger = Faraday::Response::Logger.new(STDOUT) else @logger = logger end end
Public Instance Methods
get(path, params = {})
click to toggle source
# File lib/bitzlato/client.rb, line 28 def get(path, params = {}) parse_response connection.get path, params end
post(path, params = {})
click to toggle source
# File lib/bitzlato/client.rb, line 32 def post(path, params = {}) parse_response connection.post path, params.to_json end
Private Instance Methods
bearer()
click to toggle source
# File lib/bitzlato/client.rb, line 70 def bearer JWT.encode claims, @jwk.keypair, 'ES256', kid: '1' end
claims()
click to toggle source
# File lib/bitzlato/client.rb, line 45 def claims { "aud": "usr", "iat": Time.now.to_i, "jti": SecureRandom.hex(10) }.tap { |c| c['uid'] = @uid unless @uid.nil? c['email'] = @email unless @email.nil? } end
connection()
click to toggle source
Every time build new connection to have fresh jwt token
# File lib/bitzlato/client.rb, line 57 def connection Faraday.new url: @home_url do |c| c.use Faraday::Response::Logger unless @logger.nil? c.headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } c.request :curl, @logger, :warn if ENV['BITZLATO_CURL_LOGGER'] c.authorization :Bearer, bearer c.adapter @adapter end end
parse_response(response)
click to toggle source
# File lib/bitzlato/client.rb, line 38 def parse_response(response) raise WrongResponse, "Wrong response status (#{response.status}) with body #{response.body}" unless response.success? return nil if response.body.empty? raise WrongResponse, "Wrong content type (#{response['content-type']})" if response['content-type'] != 'application/json' JSON.parse response.body end