Class: MidasClient::Request

Inherits:
Object
  • Object
show all
Includes:
Util, RestClient
Defined in:
lib/midas_client/request.rb

Direct Known Subclasses

Management, Queries, Subscription, Transaction

Instance Attribute Summary collapse

Attributes included from Util

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#error_log, #log, #sanitize_pci

Constructor Details

#initialize(login = nil, password = nil, option = {}) ⇒ Request



8
9
10
11
12
# File 'lib/midas_client/request.rb', line 8

def initialize(=nil, password=nil, option={})
  @login = 
  @password = password
  .blank? ? log("POS STATIC INITIALIZED!") : log("POS #{login} INITIALIZED!")
end

Instance Attribute Details

#loginObject

Returns the value of attribute login.



6
7
8
# File 'lib/midas_client/request.rb', line 6

def 
  @login
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/midas_client/request.rb', line 6

def password
  @password
end

Class Method Details

.external_request(method, endpoint, parameters = {}, headers = {content_type: :json, accept: :json}) ⇒ Object

Method to call any other



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/midas_client/request.rb', line 59

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



15
16
17
18
19
20
21
22
23
# File 'lib/midas_client/request.rb', line 15

def base_result(success, code, message)
  {
      result: {
          success: success,
          code: code,
          message: "#{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



26
27
28
29
30
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
# File 'lib/midas_client/request.rb', line 26

def request(method, endpoint, , 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: ,
                                           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