Class: NeonApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/neon_api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, token, login, password, encrypt_pem, decrypt_pem, proxy) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/neon_api/client.rb', line 14

def initialize(environment, token, , password, encrypt_pem, decrypt_pem, proxy)
  @environment  = environment
  @token        = token
  @username     = 
  @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

  RestClient.proxy = @proxy if @proxy
end

Instance Attribute Details

#aes_ivObject

Returns the value of attribute aes_iv.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def aes_iv
  @aes_iv
end

#aes_keyObject

Returns the value of attribute aes_key.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def aes_key
  @aes_key
end

#auth_tokenObject

Returns the value of attribute auth_token.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def auth_token
  @auth_token
end

#bank_accountObject

Returns the value of attribute bank_account.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def 
  @bank_account
end

#base_urlObject

Returns the value of attribute base_url.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def base_url
  @base_url
end

#client_idObject

Returns the value of attribute client_id.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def client_id
  @client_id
end

#environmentObject

Returns the value of attribute environment.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def environment
  @environment
end

#expire_timeObject

Returns the value of attribute expire_time.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def expire_time
  @expire_time
end

#last_authenticated_atObject

Returns the value of attribute last_authenticated_at.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def last_authenticated_at
  @last_authenticated_at
end

#payloadObject

Returns the value of attribute payload.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def payload
  @payload
end

#responseObject

Returns the value of attribute response.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def response
  @response
end

#time_klassObject (readonly)

Returns the value of attribute time_klass.



12
13
14
# File 'lib/neon_api/client.rb', line 12

def time_klass
  @time_klass
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def token
  @token
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/neon_api/client.rb', line 9

def url
  @url
end

Instance Method Details

#aes_cipher(method, payload) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/neon_api/client.rb', line 110

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_headersObject



69
70
71
72
73
74
75
# 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

#authenticateObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/neon_api/client.rb', line 102

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) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/neon_api/client.rb', line 94

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

#headersObject



77
78
79
80
81
82
83
# 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?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/neon_api/client.rb', line 65

def production?
  @environment == :production
end

#send_request(object, url) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/neon_api/client.rb', line 85

def update_auth(auth_answer)
  @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