Class: NeonApi::Client
- Inherits:
-
Object
- Object
- NeonApi::Client
- Defined in:
- lib/neon_api/client.rb
Instance Attribute Summary collapse
-
#aes_iv ⇒ Object
Returns the value of attribute aes_iv.
-
#aes_key ⇒ Object
Returns the value of attribute aes_key.
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
-
#bank_account ⇒ Object
Returns the value of attribute bank_account.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#expire_time ⇒ Object
Returns the value of attribute expire_time.
-
#last_authenticated_at ⇒ Object
Returns the value of attribute last_authenticated_at.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#response ⇒ Object
Returns the value of attribute response.
-
#token ⇒ Object
Returns the value of attribute token.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #aes_cipher(method, payload) ⇒ Object
- #auth_headers ⇒ Object
- #authenticate ⇒ Object
- #decrypt_payload(payload:, authentication: false) ⇒ Object
- #encrypted_payload(payload: self.payload, authentication: false) ⇒ Object
- #headers ⇒ Object
-
#initialize(environment = :development, token, login, password, encrypt_pem, decrypt_pem) ⇒ Client
constructor
A new instance of Client.
- #production? ⇒ Boolean
- #send_request(object, url) ⇒ Object
- #update_auth(auth_answer) ⇒ Object
Constructor Details
#initialize(environment = :development, token, login, password, encrypt_pem, decrypt_pem) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/neon_api/client.rb', line 11 def initialize(environment = :development, token, login, password, encrypt_pem, decrypt_pem) @base_url = if production? else 'https://servicosdev.neonhomol.com.br/servicopj/' end @environment = environment @token = token @username = login @password = password @encrypt_pem = encrypt_pem @decrypt_pem = decrypt_pem end |
Instance Attribute Details
#aes_iv ⇒ Object
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_key ⇒ Object
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_token ⇒ Object
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_account ⇒ Object
Returns the value of attribute bank_account.
9 10 11 |
# File 'lib/neon_api/client.rb', line 9 def bank_account @bank_account end |
#client_id ⇒ Object
Returns the value of attribute client_id.
9 10 11 |
# File 'lib/neon_api/client.rb', line 9 def client_id @client_id end |
#environment ⇒ Object
Returns the value of attribute environment.
9 10 11 |
# File 'lib/neon_api/client.rb', line 9 def environment @environment end |
#expire_time ⇒ Object
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_at ⇒ Object
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 |
#payload ⇒ Object
Returns the value of attribute payload.
9 10 11 |
# File 'lib/neon_api/client.rb', line 9 def payload @payload end |
#response ⇒ Object
Returns the value of attribute response.
9 10 11 |
# File 'lib/neon_api/client.rb', line 9 def response @response end |
#token ⇒ Object
Returns the value of attribute token.
9 10 11 |
# File 'lib/neon_api/client.rb', line 9 def token @token end |
#url ⇒ Object
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
107 108 109 110 111 112 113 |
# File 'lib/neon_api/client.rb', line 107 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 ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/neon_api/client.rb', line 66 def auth_headers { "Cache-Control": "no-cache", "Content-Type": "application/x-www-form-urlencoded", "Token": token } end |
#authenticate ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/neon_api/client.rb', line 25 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
99 100 101 102 103 104 105 |
# File 'lib/neon_api/client.rb', line 99 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
91 92 93 94 95 96 97 |
# File 'lib/neon_api/client.rb', line 91 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 ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/neon_api/client.rb', line 74 def headers { "Cache-Control": "no-cache", "Content-Type": "application/x-www-form-urlencoded", "Token": auth_token } end |
#production? ⇒ Boolean
62 63 64 |
# File 'lib/neon_api/client.rb', line 62 def production? @environment == :production end |
#send_request(object, url) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/neon_api/client.rb', line 42 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
82 83 84 85 86 87 88 89 |
# File 'lib/neon_api/client.rb', line 82 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'] @bank_account = auth_answer['DataReturn']['BankAccountId'] end |