Class: MidasClient::Request
- Inherits:
-
Object
- Object
- MidasClient::Request
- Defined in:
- lib/midas_client/request.rb
Direct Known Subclasses
Constant Summary
Constants included from EndPoints
EndPoints::DEVELOPMENT, EndPoints::MANAGEMENTS, EndPoints::OPERATIONS, EndPoints::PRODUCTION, EndPoints::QUERIES, EndPoints::SUBSCRIPTIONS
Instance Attribute Summary collapse
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#login ⇒ Object
Returns the value of attribute login.
-
#password ⇒ Object
Returns the value of attribute password.
-
#query ⇒ Object
Returns the value of attribute query.
-
#subscription ⇒ Object
Returns the value of attribute subscription.
-
#transaction ⇒ Object
Returns the value of attribute transaction.
Attributes included from Util
Class Method Summary collapse
-
.external_request(method, endpoint, parameters = {}, headers = {content_type: :json, accept: :json}) ⇒ Object
Method to call any other.
Instance Method Summary collapse
-
#base_result(success, code, message) ⇒ Object
Method that’s wrap generic response.
-
#initialize(login = nil, password = nil, environment = 'DEVELOPMENT', option = {}) ⇒ Request
constructor
A new instance of Request.
-
#request(method, endpoint, login, password, parameters = {}, headers = {content_type: :json, accept: :json}) ⇒ Object
Method that’s wrap rest-client generic request.
Methods included from EndPoints
#get_env, #get_environment, #production?, #set_env
Methods included from Util
#error_log, #log, #sanitize_pci
Constructor Details
#initialize(login = nil, password = nil, environment = 'DEVELOPMENT', option = {}) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 |
# File 'lib/midas_client/request.rb', line 12 def initialize(login=nil, password=nil, environment='DEVELOPMENT', option={}) @login = login @password = password @environment = environment login.blank? ? log("POS STATIC INITIALIZED!") : log("POS #{login} INITIALIZED!") end |
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
7 8 9 |
# File 'lib/midas_client/request.rb', line 7 def environment @environment end |
#login ⇒ Object
Returns the value of attribute login.
7 8 9 |
# File 'lib/midas_client/request.rb', line 7 def login @login end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/midas_client/request.rb', line 7 def password @password end |
#query ⇒ Object
Returns the value of attribute query.
10 11 12 |
# File 'lib/midas_client/request.rb', line 10 def query @query end |
#subscription ⇒ Object
Returns the value of attribute subscription.
9 10 11 |
# File 'lib/midas_client/request.rb', line 9 def subscription @subscription end |
#transaction ⇒ Object
Returns the value of attribute transaction.
8 9 10 |
# File 'lib/midas_client/request.rb', line 8 def transaction @transaction end |
Class Method Details
.external_request(method, endpoint, parameters = {}, headers = {content_type: :json, accept: :json}) ⇒ Object
Method to call any other
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/midas_client/request.rb', line 64 def self.external_request(method, endpoint, parameters={}, headers={content_type: :json, accept: :json}) extend Util # inicia a contagem da execução start_time_execution = Time.now log "[EXTERNAL][REQUEST][#{method}] URL: #{endpoint} payload: #{sanitize_pci(parameters)}" begin response = RestClient::Request.execute(method: method, url: endpoint, payload: (parameters.to_json unless parameters.nil?), headers: headers) rescue => e response = e.response || e end # parsing da resposta begin response = JSON.parse(response, symbolize_names: true ) rescue => e error_log "[EXTERNAL][REQUEST]METHOD: #{method} URL: #{endpoint} Mensagem: #{e.to_s}" response = base_result(false, '999', "Operação não concluída. Motivo: #{e.to_s}") ensure total_time_execution = Time.now - start_time_execution log "[EXTERNAL][RESPONSE][#{method}] URL: #{endpoint} TEMPO: #{total_time_execution}s RESULT: #{sanitize_pci(response)}" end response end |
Instance Method Details
#base_result(success, code, message) ⇒ Object
Method that’s wrap generic response
20 21 22 23 24 25 26 27 28 |
# File 'lib/midas_client/request.rb', line 20 def base_result(success, code, ) { result: { success: success, code: code, message: "#{}" } }.to_hash end |
#request(method, endpoint, login, password, parameters = {}, headers = {content_type: :json, accept: :json}) ⇒ Object
Method that’s wrap rest-client generic request
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/midas_client/request.rb', line 31 def request(method, endpoint, login, password, parameters={}, headers={content_type: :json, accept: :json}) # inicia a contagem da execução start_time_execution = Time.now log "METHOD: #{method} URL: #{endpoint} login: #{login} password: #{password} payload: #{self.sanitize_pci(parameters)}" begin response = RestClient::Request.execute(method: method, url: endpoint, user: login, password: password, content_type: :json, ssl_version: :TLSv1_2, payload: (parameters.to_json unless parameters.nil?), headers: headers) rescue => e response = e.response || e end # parsing da resposta begin response = JSON.parse(response, symbolize_names: true ) rescue => e error_log "METHOD: #{method} URL: #{endpoint} login: #{login} Mensagem: #{e.to_s}" response = base_result(false, '999', "Operação não concluída. Motivo: #{e.to_s}") ensure total_time_execution = Time.now - start_time_execution log "[RESPONSE] #{self.sanitize_pci(response)}" log "Executado #{method} URL: #{endpoint} LOGIN: #{login} TEMPO: #{total_time_execution}s SUCCESS: #{response[:result][:success]}" end response end |