Class: SdkRubyApisEfi::Endpoints

Inherits:
Object
  • Object
show all
Defined in:
lib/sdk_ruby_apis_efi/endpoints.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Endpoints

Returns a new instance of Endpoints.



14
15
16
17
18
19
20
21
22
23
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 14

def initialize(options)
  super()
  @token = nil
  @options = options
  @charges = Constants::APIs::CHARGES
  @pix = Constants::APIs::PIX
  @open_finance = Constants::APIs::OPEN_FINANCE
  @payments = Constants::APIs::PAYMENTS
  @accounts_opening = Constants::APIs::ACCOUNTS_OPENING
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, **kwargs) ⇒ Object



25
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
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 25

def method_missing(name, **kwargs)
  if @charges[:ENDPOINTS].include?(name)
    @endpoints = @charges[:ENDPOINTS]
    @urls = @charges[:URL]
    request(@charges[:ENDPOINTS][name], **kwargs)

  elsif @pix[:ENDPOINTS].include?(name)
    @endpoints = @pix[:ENDPOINTS]
    @urls = @pix[:URL]
    request(@pix[:ENDPOINTS][name], **kwargs)
  
  elsif @open_finance[:ENDPOINTS].include?(name)
    @endpoints = @open_finance[:ENDPOINTS]
    @urls = @open_finance[:URL]
    request(@open_finance[:ENDPOINTS][name], **kwargs)
  
  elsif @payments[:ENDPOINTS].include?(name)
    @endpoints = @payments[:ENDPOINTS]
    @urls = @payments[:URL]
    request(@payments[:ENDPOINTS][name], **kwargs)
  
  elsif @accounts_opening[:ENDPOINTS].include?(name)
    @endpoints = @accounts_opening[:ENDPOINTS]
    @urls = @accounts_opening[:URL]
    request(@accounts_opening[:ENDPOINTS][name], **kwargs)
  
  else
    raise "Method not found"
  end
  
end

Instance Method Details

#authenticateObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 133

def authenticate

  url = build_url(@endpoints[:authorize][:route], {})

  headers = {
    "accept" => "application/json",
    "api-sdk" => "efi-ruby-#{SdkRubyApisEfi::VERSION}"  
  }

  auth_headers = {
    user: @options[:client_id],
    pass: @options[:client_secret]
  }
  
  auth_body =  {grant_type: :client_credentials}

  if (@options.has_key?(:certificate))
  
    response = 
      HTTP 
      .headers(headers)
      .basic_auth(auth_headers)
      .post(url, json: auth_body, ssl_context: OpenSSL::SSL::SSLContext.new.tap do |ctx|
        ctx.set_params(
          cert: OpenSSL::X509::Certificate.new(File.read(@options[:certificate])),
          key:  OpenSSL::PKey::RSA.new(File.read(@options[:certificate]))
        )
      end)
      
  else
    response = 
      HTTP     
      .headers(headers)
      .basic_auth(auth_headers)
      .post(url, json: auth_body)
  end
  
  if response.status.to_s == STATUS::UNAUTHORIZED
    fail "unable to authenticate"
  else
    @token = response
  end
end

#build_url(route, params) ⇒ Object



186
187
188
189
190
191
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 186

def build_url(route, params)
  params = {} if params.nil?
  route = remove_placeholders(route, params)
  complete_url = complete_url(route, params)
  
end

#complete_url(route, params) ⇒ Object



216
217
218
219
220
221
222
223
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 216

def complete_url(route, params)
  mapped = map_params(params)
  if !mapped.empty?
    "#{@base_url}#{route}?#{mapped}"
  else
    "#{@base_url}#{route}"
  end
end

#get_urlObject



178
179
180
181
182
183
184
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 178

def get_url
  @base_url = @urls[:sandbox]
  if @options.has_key?(:sandbox)
    @base_url = @options[:sandbox] ? @urls[:sandbox] : @urls[:production]
  end

end

#map_params(params) ⇒ Object



210
211
212
213
214
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 210

def map_params(params)
  params.map do |key|
    "#{key[0]}=#{CGI.escape(key[1].to_s)}"
  end.join("&")
end

#query_string(params) ⇒ Object



205
206
207
208
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 205

def query_string(params)
  mapped = params.map { |p, value| "#{p}=#{value}" }
  mapped.join('&')
end

#remove_placeholders(route, params) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 193

def remove_placeholders(route, params)
  regex = /\:(\w+)/
  route.scan(regex).each do |key|
    key = key[0]
    value = params[key.to_sym].to_s
    route = route.gsub(":#{key}", value)
    params.delete(key.to_sym)
  end

  return route
end

#request(settings, **kwargs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 57

def request(settings, **kwargs)

  params = kwargs[:params] || {}
  body = kwargs[:body] || {}
  headers = kwargs[:headers] || {}

  get_url

  if @token.nil?
    authenticate
  end


  response = send(settings, params, body, headers)
  
  begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    "{'code': #{response.code}}"
  else
    JSON.parse(response.body)
  end
  
end

#send(settings, params, body, headersComplement) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sdk_ruby_apis_efi/endpoints.rb', line 82

def send(settings, params, body, headersComplement)
  url = build_url(settings[:route], params)
  if body == {}
    body = nil
  end
  headers = {
    "accept" => "application/json",
    "api-sdk" => "efi-ruby-#{SdkRubyApisEfi::VERSION}"
  }
  
  if headersComplement.any?
    headersComplement.each do |key, value|
      headers[key] = value
    end
  end
  
  if @options.has_key?(:partner_token)
    headers['partner-token'] = @options[:partner_token]
  end

  
  if @options[:"x-skip-mtls-checking"]
    headers["x-skip-mtls-checking"] = @options[:"x-skip-mtls-checking"]
  end

  @token = @token.parse
  
  if @options.has_key?(:certificate)

    HTTP
      .headers(headers)
      .auth("Bearer #{@token['access_token']}")
      .method(settings[:method])
      .call(url, json: body, ssl_context: OpenSSL::SSL::SSLContext.new.tap do |ctx|
        ctx.set_params(
          cert: OpenSSL::X509::Certificate.new(File.read(@options[:certificate])),
          key:  OpenSSL::PKey::RSA.new(File.read(@options[:certificate]))
        )
      end)
  else
    
    HTTP
        .headers(headers)
        .auth("Bearer #{@token['access_token']}")
        .method(settings[:method])
        .call(url, json: body)
  end

  
end