zaig 1.0.6 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab0151c32beb57d31bb2e3ccd10ff6408091c79c61dbbb9f85b03fed94b6498b
4
- data.tar.gz: dc88f4a1dbb7601abddeb3c8812948a1f3c6b9633d1b6899168530831b125b4b
3
+ metadata.gz: 92825d985fa1be6233fa33065769e7ebf8c32fc175d10cf850cc50100977c0de
4
+ data.tar.gz: 7ab76c837269a34c050b756734ee93ffdb9641fdc389a20c8906acadfbfcc1be
5
5
  SHA512:
6
- metadata.gz: 12d241a7a4b5124573f7b45d5a5818075fe45b117bce2ee3d21b404c009eeb9d2f4dbeeff52f6386841c204b568b71811cad6376f23295876645a9a011fd9a1c
7
- data.tar.gz: d761bbadde669244ad80a1644d31350796f2d1e77ea6d2e01b29cf243b7f691f26d28a49675f37567a74a5d88d1c23403fbaa42044b49afbef2121aa918885aa
6
+ metadata.gz: 26010f68ed6e2ab3d0f2d35e3083eefabe4c95c7aee950f948cc2e02f5cc8fd9f9294e1994413adad77f2e135c4cf0216f7cce09a05dd4e9587415538ef173f9
7
+ data.tar.gz: ffeda0495121d32edcd1905273b5c190c2cebec78ba0dea58550e6c5766bff83b95f5814fde0e80551d90bf82a232f82e0df428354c875a916f17257c41c5aca
@@ -3,18 +3,30 @@
3
3
  module Zaig
4
4
  # Class to instance a authenticated connection object.
5
5
  class Connection < Flash::Integration::Connection
6
- def initialize(request_class: Faraday, base_url: Zaig.configuration.base_url, access_token: Zaig.configuration.access_token)
7
- @access_token = access_token
6
+ def initialize(request_class: Faraday, base_url: Zaig.configuration.base_url)
7
+ @jwt_algorithm = Zaig.configuration.jwt_algorithm
8
+ @jwt_exp_time = Zaig.configuration.jwt_exp_time
9
+ @jwt_secret = Zaig.configuration.jwt_secret
10
+ @jwt_user = Zaig.configuration.jwt_user
8
11
 
9
12
  super(request_class: request_class, base_url: base_url)
10
13
  end
11
14
 
12
15
  def default_headers
13
- {
16
+ headers = {
14
17
  "Content-Type": "application/json",
15
- Accept: "application/json",
16
- "x-api-key": @access_token
18
+ Accept: "application/json"
17
19
  }
20
+
21
+ return headers if @jwt_secret.nil? || @jwt_secret&.empty?
22
+
23
+ headers[:Authorization] = "Bearer #{access_token}"
24
+ headers
18
25
  end
26
+
27
+ private
28
+ def access_token
29
+ JWT.encode({ exp: @jwt_exp_time, user: @jwt_user }, @jwt_secret, @jwt_algorithm)
30
+ end
19
31
  end
20
32
  end
@@ -5,11 +5,9 @@ module Zaig
5
5
  class Registration
6
6
  attr_reader :connection
7
7
 
8
- include Singleton
9
-
10
8
  ENDPOINT = "zaig/consulta_de_credito"
11
9
 
12
- def initialize(connection: Zaig::Connection.new, registration_payload: Zaig::RegistrationPayload.instance)
10
+ def initialize(connection: Zaig::Connection.new, registration_payload: Zaig::RegistrationPayload.new)
13
11
  @connection = connection
14
12
  @registration_payload = registration_payload
15
13
  end
@@ -17,9 +15,7 @@ module Zaig
17
15
  def call(obj)
18
16
  payload = @registration_payload.call(obj)
19
17
 
20
- req_endpoint = Zaig.configuration.registration_endpoint
21
-
22
- res = @connection.post(url: (req_endpoint.nil? ? ENDPOINT : req_endpoint), body: payload.to_json)
18
+ res = @connection.post(url: Zaig.configuration.registration_endpoint, body: payload.to_json)
23
19
 
24
20
  verify_response(res)
25
21
 
@@ -50,8 +46,8 @@ module Zaig
50
46
  unless parsed_res.key?(:resposta_zaig)
51
47
  detail = JSON.parse(res.body, symbolize_names: true)[:detail]
52
48
 
53
- error = ::Zaig::FieldValidationError.new I18n.t("zaig.errors.validation_error", field_name: "resposta_zaig")
54
- error.detail = JSON.parse(res.body, symbolize_names: true)[:detail]
49
+ error = ::Zaig::FieldValidationError.new I18n.t("zaig.errors.validation_error")
50
+ error.detail = detail
55
51
 
56
52
  raise error
57
53
  end
@@ -3,30 +3,28 @@
3
3
  module Zaig
4
4
  # Service class to build a registration payload request.
5
5
  class RegistrationPayload
6
- include Singleton
7
-
8
6
  def call(obj)
9
7
  payload = {
10
- address: build_address(obj[:address]),
11
8
  client_category: obj[:client_category],
12
- constitution_date: obj[:constitution_date],
13
- constitution_type: obj[:constitution_type],
14
9
  credit_request_date: obj[:credit_request_date],
15
10
  credit_type: obj[:credit_type] || "clean",
16
11
  document_number: CNPJ.new(obj[:document_number]).formatted,
17
- email: obj[:email],
18
12
  id: obj[:id],
19
13
  legal_name: obj[:legal_name],
20
14
  monthly_revenue: format_money(obj[:monthly_revenue], require_positive: true),
21
- phones: build_phones(obj[:phones]),
22
- shareholders: build_shareholders(obj[:shareholders]),
23
15
  trading_name: obj[:trading_name]
24
16
  }
25
- payload[:guarantors] = build_shareholders(obj[:guarantors]) if obj.key?(:guarantors)
26
- payload[:financial] = build_financial(obj[:financial]) if obj.key?(:financial)
27
- payload[:scr_parameters] = build_scr_parameters(obj[:scr_parameters]) if obj.key?(:scr_parameters)
28
- payload[:source] = build_source(obj[:source]) if obj.key?(:source)
29
- payload[:warrants] = build_warrants(obj[:warrants]) if obj.key?(:warrants)
17
+ payload[:address] = build_address(obj[:address]) if obj[:address]
18
+ payload[:constitution_date] = obj[:constitution_date] if obj[:constitution_date]
19
+ payload[:constitution_type] = obj[:constitution_type] if obj[:constitution_type]
20
+ payload[:email] = obj[:email] if obj[:email]
21
+ payload[:financial] = build_financial(obj[:financial]) if obj[:financial]
22
+ payload[:guarantors] = build_shareholders(obj[:guarantors]) if obj[:guarantors]
23
+ payload[:phones] = build_phones(obj[:phones]) if obj[:phones]
24
+ payload[:scr_parameters] = build_scr_parameters(obj[:scr_parameters]) if obj[:scr_parameters]
25
+ payload[:shareholders] = build_shareholders(obj[:shareholders]) if obj[:shareholders]
26
+ payload[:source] = build_source(obj[:source]) if obj[:source]
27
+ payload[:warrants] = build_warrants(obj[:warrants]) if obj[:warrants]
30
28
  payload
31
29
  end
32
30
 
@@ -36,9 +34,8 @@ module Zaig
36
34
  end
37
35
 
38
36
  def build_address(obj_address)
39
- {
37
+ address = {
40
38
  city: obj_address[:city],
41
- complement: obj_address[:complement] || "",
42
39
  country: obj_address[:country] || "BRA",
43
40
  neighborhood: obj_address[:neighborhood],
44
41
  number: obj_address[:number],
@@ -46,6 +43,8 @@ module Zaig
46
43
  street: obj_address[:street],
47
44
  uf: obj_address[:uf]
48
45
  }
46
+ address[:complement] = obj_address[:complement] if !obj_address[:complement].nil? || !obj_address[:complement]&.empty?
47
+ address
49
48
  end
50
49
 
51
50
  def build_phone(obj)
data/lib/zaig/version.rb CHANGED
@@ -8,5 +8,5 @@ module Zaig
8
8
  # Major - Incremented for incompatible changes with previous release (or big enough new features)
9
9
  # Minor - Incremented for new backwards-compatible features + deprecations
10
10
  # Patch - Incremented for backwards-compatible bug fixes
11
- VERSION = "1.0.6"
11
+ VERSION = "1.0.9"
12
12
  end
data/lib/zaig.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require "cpf_cnpj"
4
4
  require "flash_integration"
5
5
  require "i18n"
6
- require "singleton"
6
+ require "jwt"
7
7
 
8
8
  require "zaig/base_error"
9
9
  require "zaig/already_exists_error"
@@ -48,6 +48,19 @@ module Zaig
48
48
 
49
49
  # Basic configuration settings
50
50
  class Configuration
51
- attr_accessor :access_token, :base_url, :registration_endpoint
51
+ attr_accessor :base_url, :jwt_secret, :jwt_user
52
+ attr_writer :jwt_algorithm, :jwt_exp_time, :registration_endpoint
53
+
54
+ def jwt_algorithm
55
+ @jwt_algorithm ||= "HS256"
56
+ end
57
+
58
+ def jwt_exp_time
59
+ @jwt_exp_time ||= 1800
60
+ end
61
+
62
+ def registration_endpoint
63
+ @registration_endpoint ||= "zaig/consulta_de_credito"
64
+ end
52
65
  end
53
66
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaig
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danilo Carolino
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-19 00:00:00.000000000 Z
11
+ date: 2022-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cpf_cnpj
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: singleton
42
+ name: jwt
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="