Class: MidasClient::Request

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

Direct Known Subclasses

Billet, Management, Query, Subscription, Transaction

Constant Summary

Constants included from EndPoints

EndPoints::DEVELOPMENT, EndPoints::MANAGEMENTS, EndPoints::OPERATIONS, EndPoints::PRODUCTION, EndPoints::QUERIES, EndPoints::SUBSCRIPTIONS

Instance Attribute Summary collapse

Attributes included from Util

#logger

Class Method Summary collapse

Instance Method Summary collapse

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(=nil, password=nil, environment='DEVELOPMENT', option={})
  @login = 
  @password = password
  @environment = environment
  .blank? ? log("POS STATIC INITIALIZED!") : log("POS #{} INITIALIZED!")
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#loginObject

Returns the value of attribute login.



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

def 
  @login
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

#subscriptionObject

Returns the value of attribute subscription.



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

def subscription
  @subscription
end

#transactionObject

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



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, , 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: #{} 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: #{} 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: #{} TEMPO: #{total_time_execution}s SUCCESS: #{response[:result][:success]}"
  end
  response
end