class NeonApi::Client

Attributes

aes_iv[RW]
aes_key[RW]
auth_token[RW]
bank_account[RW]
base_url[RW]
client_id[RW]
environment[RW]
expire_time[RW]
last_authenticated_at[RW]
payload[RW]
response[RW]
time_klass[R]
token[RW]
url[RW]

Public Class Methods

new(environment, token, login, password, encrypt_pem, decrypt_pem, proxy) click to toggle source
# File lib/neon_api/client.rb, line 14
def initialize(environment, token, login, password, encrypt_pem, decrypt_pem, proxy)
  @environment  = environment
  @token        = token
  @username     = login
  @password     = password
  @encrypt_pem  = encrypt_pem
  @decrypt_pem  = decrypt_pem
  @proxy        = proxy
  @time_klass   = Time.respond_to?(:zone) ? Time.zone : Time
  @expire_time  = time_klass.now - 3600 # initialize in an expired condition to force first authentication

  RestClient.proxy = @proxy if @proxy
end

Public Instance Methods

aes_cipher(method, payload) click to toggle source
# File lib/neon_api/client.rb, line 111
def aes_cipher(method, payload)
  cipher = OpenSSL::Cipher::AES.new(256, :CBC)
  cipher.send(method)
  cipher.key      = aes_key.map { |u| u.chr }.join
  cipher.iv       = aes_iv.map { |u| u.chr }.join
  (cipher.update(payload) + cipher.final).force_encoding('UTF-8')
end
auth_headers() click to toggle source
# File lib/neon_api/client.rb, line 69
def auth_headers
  {
      "Cache-Control": "no-cache",
      "Content-Type": "application/x-www-form-urlencoded",
      "Token": token
  }
end
authenticate() click to toggle source
# File lib/neon_api/client.rb, line 28
def authenticate
  @last_authenticated_at = time_klass.now

  request = begin
    RestClient::Request.execute(method: :post, url: "#{base_url}/V1/Client/Authentication",
                                payload: { "Data": encrypted_payload(authentication: true) }, headers: auth_headers)
  rescue RestClient::ExceptionWithResponse => err
    err.response
  end

  if request.code == 200
    update_auth JSON.parse(decrypt_payload(payload: JSON.parse(request.body)["Data"], authentication: true))
  else
    raise request
  end
end
decrypt_payload(payload:, authentication: false) click to toggle source
# File lib/neon_api/client.rb, line 103
def decrypt_payload(payload:, authentication: false)
  if authentication
    OpenSSL::PKey::RSA.new(File.read(@decrypt_pem)).private_decrypt(Base64.decode64 payload)
  else
    aes_cipher(:decrypt, Base64.decode64(payload))
  end
end
encrypted_payload(payload: self.payload, authentication: false) click to toggle source
# File lib/neon_api/client.rb, line 95
def encrypted_payload(payload: self.payload, authentication: false)
  if authentication
    Base64.encode64 OpenSSL::PKey::RSA.new(File.read(@encrypt_pem)).public_encrypt(payload)
  else
    Base64.encode64(aes_cipher(:encrypt, payload))
  end
end
headers() click to toggle source
# File lib/neon_api/client.rb, line 77
def headers
  {
      "Cache-Control": "no-cache",
      "Content-Type": "application/x-www-form-urlencoded",
      "Token": auth_token
  }
end
production?() click to toggle source
# File lib/neon_api/client.rb, line 65
def production?
  @environment == :production
end
send_request(object, url) click to toggle source
# File lib/neon_api/client.rb, line 45
def send_request(object, url)

  authenticate if time_klass.now > expire_time

  request = begin
    RestClient::Request.execute(method: :post, url: base_url + url,
                                payload: { "Data": encrypted_payload(payload: object) }, headers: headers)
  rescue RestClient::ExceptionWithResponse => err
    err.response
  end

  if request.code != 500
    @response = JSON.parse(decrypt_payload(payload: JSON.parse(request.body)['Data']))
  else
    @response = request
  end

  return @response
end
update_auth(auth_answer) click to toggle source
# File lib/neon_api/client.rb, line 85
def update_auth(auth_answer)
  @data_return = auth_answer['DataReturn']
  @auth_token = auth_answer['DataReturn']['Token']
  @aes_key = auth_answer['DataReturn']['AESKey']
  @aes_iv = auth_answer['DataReturn']['AESIV']
  @expire_time = time_klass.at(auth_answer['DataReturn']['DataExpiracao'].gsub(/[^\d]/, '').to_i)
  @client_id = auth_answer['DataReturn']['ClientId']
  @bank_account = auth_answer['DataReturn']['BankAccountId']
end