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 = :development, token, login, password, encrypt_pem, decrypt_pem, proxy) ⇒ Client

Returns a new instance of Client.



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

def initialize(environment = :development, token, , password, encrypt_pem, decrypt_pem, proxy)
  @base_url = if production?
                'https://apiparceiros.banconeon.com.br/ '
              else
                'https://servicosdev.neonhomol.com.br/servicopj/'
              end

  @environment  = environment
  @token        = token
  @username     = 
  @password     = password
  @encrypt_pem  = encrypt_pem
  @decrypt_pem  = decrypt_pem
  @proxy        = proxy

  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 
  
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

#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



111
112
113
114
115
116
117
# 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_headersObject



70
71
72
73
74
75
76
# File 'lib/neon_api/client.rb', line 70

def auth_headers
  {
      "Cache-Control": "no-cache",
      "Content-Type": "application/x-www-form-urlencoded",
      "Token": token
  }
end

#authenticateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/neon_api/client.rb', line 29

def authenticate
  @last_authenticated_at = Time.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



103
104
105
106
107
108
109
# 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) ⇒ Object



95
96
97
98
99
100
101
# 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

#headersObject



78
79
80
81
82
83
84
# File 'lib/neon_api/client.rb', line 78

def headers
  {
      "Cache-Control": "no-cache",
      "Content-Type": "application/x-www-form-urlencoded",
      "Token": auth_token
  }
end

#production?Boolean

Returns:

  • (Boolean)


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

def production?
  @environment == :production
end

#send_request(object, url) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/neon_api/client.rb', line 46

def send_request(object, url)

  authenticate if expire_time > Time.now

  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



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

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.at(auth_answer['DataReturn']['DataExpiracao'].gsub(/[^\d]/, '').to_i)
  @client_id = auth_answer['DataReturn']['ClientId']
   = auth_answer['DataReturn']['BankAccountId']
end