zaig 1.0.5 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d7e5e123adff444d932edaf15cf93f0d56c036e19a12391957736cdb5b3a0f1
4
- data.tar.gz: 6dff02d64d11a052be1a9dc47f77b6a490d3208b1de8b84d487527259099e546
3
+ metadata.gz: 05cec7da728cfbfdf5457960a2d289e4963d22874f9790e12408eb2498aff1de
4
+ data.tar.gz: 1b73d0fe9a384e2bb73dd6af74939117329142fa9bf9aa27eeb28ccca5af2f95
5
5
  SHA512:
6
- metadata.gz: 06ea034e23aef7a39e5dd7fe35260746b31d8095ce9f524bb45f60b02b92d6285fe24bd305e43c1f24b49b3d0cb9e61777d6612430741bc49de0f384c88d6f7f
7
- data.tar.gz: f637d5ab308c48793bda590598a41fdff30d8103f395c03cfaf38e87eba2446b4ef77cfe30d3020d174f397e10990802477865559610d80859d4f14f6382bad8
6
+ metadata.gz: 7f5c7152922f7a0823b805ad584986ed2c4b2f86484b5ca1f0c5544960cc15a20cbc48a1c0c299eb7b8e226a09d61eb74b5915a928fc4492876b0d7ca8fe4443
7
+ data.tar.gz: fa24fdd51580ef3a9f64bc02eda165e2c928fbc087b8595b5570324657e2509def4a510f84ca3452f0b54e24dd15c58ba44a01a9bd41e0a85e780ec8a88944a0
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class AlreadyExistsError < BaseError
4
+ class AlreadyExistsError < Zaig::BaseError
5
5
  end
6
6
  end
@@ -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
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class ExternalTimeoutError < BaseError
4
+ class ExternalTimeoutError < Zaig::BaseError
5
5
  end
6
6
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class FieldValidationError < BaseError
4
+ class FieldValidationError < Zaig::BaseError
5
5
  attr_accessor :detail
6
6
  end
7
7
  end
@@ -17,9 +17,7 @@ module Zaig
17
17
  def call(obj)
18
18
  payload = @registration_payload.call(obj)
19
19
 
20
- req_endpoint = Zaig.configuration.registration_endpoint
21
-
22
- res = @connection.post(url: (req_endpoint.nil? ? ENDPOINT : req_endpoint), body: payload.to_json)
20
+ res = @connection.post(url: Zaig.configuration.registration_endpoint, body: payload.to_json)
23
21
 
24
22
  verify_response(res)
25
23
 
@@ -50,13 +48,15 @@ module Zaig
50
48
  unless parsed_res.key?(:resposta_zaig)
51
49
  detail = JSON.parse(res.body, symbolize_names: true)[:detail]
52
50
 
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]
51
+ error = ::Zaig::FieldValidationError.new I18n.t("zaig.errors.validation_error")
52
+ error.detail = detail
55
53
 
56
54
  raise error
57
55
  end
58
56
 
59
- zaig_msg = parsed_res[:resposta_zaig][:msg]
57
+ title = parsed_res[:resposta_zaig][:conteudo][:title]
58
+ description = parsed_res[:resposta_zaig][:conteudo][:description]
59
+ zaig_msg = "#{title}: #{description}"
60
60
  zaig_http_status = parsed_res[:resposta_zaig][:status]
61
61
 
62
62
  raise_error_by_zaig_status(zaig_msg, zaig_http_status)
@@ -65,15 +65,15 @@ module Zaig
65
65
  def raise_error_by_zaig_status(msg, status)
66
66
  case status
67
67
  when 400
68
- raise ::Zaig::FieldValidationError, msg
68
+ raise ::Zaig::FieldValidationError.new msg
69
69
  when 408
70
- raise ::Zaig::ExternalTimeoutError, msg
70
+ raise ::Zaig::ExternalTimeoutError.new msg
71
71
  when 409
72
- raise ::Zaig::AlreadyExistsError, msg
72
+ raise ::Zaig::AlreadyExistsError.new msg
73
73
  when 422
74
- raise ::Zaig::UnprocessableEntityError, msg
74
+ raise ::Zaig::UnprocessableEntityError.new msg
75
75
  else
76
- raise ::Zaig::UnexpectedError, msg
76
+ raise ::Zaig::UnexpectedError.new msg
77
77
  end
78
78
  end
79
79
  end
@@ -6,7 +6,7 @@ module Zaig
6
6
  include Singleton
7
7
 
8
8
  def call(obj)
9
- {
9
+ payload = {
10
10
  address: build_address(obj[:address]),
11
11
  client_category: obj[:client_category],
12
12
  constitution_date: obj[:constitution_date],
@@ -15,18 +15,19 @@ module Zaig
15
15
  credit_type: obj[:credit_type] || "clean",
16
16
  document_number: CNPJ.new(obj[:document_number]).formatted,
17
17
  email: obj[:email],
18
- financial: obj.key?("financial") ? build_financial(obj[:financial]) : nil,
19
- guarantors: obj.key?("guarantors") ? build_shareholders(obj[:guarantors]) : nil,
20
18
  id: obj[:id],
21
19
  legal_name: obj[:legal_name],
22
20
  monthly_revenue: format_money(obj[:monthly_revenue], require_positive: true),
23
21
  phones: build_phones(obj[:phones]),
24
22
  shareholders: build_shareholders(obj[:shareholders]),
25
- source: obj.key?("source") ? build_source(obj[:source]) : nil,
26
- scr_parameters: obj.key?("scr_parameters") ? build_scr_parameters(obj[:scr_parameters]) : nil,
27
- trading_name: obj[:trading_name],
28
- warrants: obj.key?("warrants") ? build_warrants(obj[:warrants]) : nil
23
+ trading_name: obj[:trading_name]
29
24
  }
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)
30
+ payload
30
31
  end
31
32
 
32
33
  private
@@ -35,9 +36,8 @@ module Zaig
35
36
  end
36
37
 
37
38
  def build_address(obj_address)
38
- {
39
+ address = {
39
40
  city: obj_address[:city],
40
- complement: obj_address[:complement] || "",
41
41
  country: obj_address[:country] || "BRA",
42
42
  neighborhood: obj_address[:neighborhood],
43
43
  number: obj_address[:number],
@@ -45,6 +45,8 @@ module Zaig
45
45
  street: obj_address[:street],
46
46
  uf: obj_address[:uf]
47
47
  }
48
+ address[:complement] = obj_address[:complement] if !obj_address[:complement].nil? || !obj_address[:complement]&.empty?
49
+ address
48
50
  end
49
51
 
50
52
  def build_phone(obj)
@@ -76,10 +78,10 @@ module Zaig
76
78
  declared_assets: format_money(s[:declared_assets]),
77
79
  document_number: CPF.new(s[:document_number]).formatted,
78
80
  email: s[:email],
79
- father_name: s[:father_name],
81
+ father_name: filter_blank(s[:father_name]),
80
82
  gender: s[:gender],
81
83
  monthly_income: format_money(s[:monthly_income]),
82
- mother_name: s[:mother_name],
84
+ mother_name: filter_blank(s[:mother_name]),
83
85
  name: s[:name],
84
86
  nationality: s[:nationality],
85
87
  occupation: s[:occupation],
@@ -161,7 +163,14 @@ module Zaig
161
163
  signers
162
164
  end
163
165
 
166
+ def filter_blank(value)
167
+ return value if value.nil?
168
+
169
+ value.strip == "" ? nil : value
170
+ end
171
+
164
172
  def format_cep(cep)
173
+ cep = cep.delete("-")
165
174
  cep.gsub(/[^0-9]/, "").insert(5, "-")
166
175
  end
167
176
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class ServerError < BaseError
4
+ class ServerError < Zaig::BaseError
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class ServerTimeoutError < BaseError
4
+ class ServerTimeoutError < Zaig::BaseError
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class UnexpectedError < BaseError
4
+ class UnexpectedError < Zaig::BaseError
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class UnprocessableEntityError < BaseError
4
+ class UnprocessableEntityError < Zaig::BaseError
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class ValidationError < BaseError
4
+ class ValidationError < Zaig::BaseError
5
5
  end
6
6
  end
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.5"
11
+ VERSION = "1.0.8"
12
12
  end
data/lib/zaig.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "cpf_cnpj"
4
4
  require "flash_integration"
5
5
  require "i18n"
6
+ require "jwt"
6
7
  require "singleton"
7
8
 
8
9
  require "zaig/base_error"
@@ -48,6 +49,19 @@ module Zaig
48
49
 
49
50
  # Basic configuration settings
50
51
  class Configuration
51
- attr_accessor :access_token, :base_url, :registration_endpoint
52
+ attr_accessor :base_url, :jwt_secret, :jwt_user
53
+ attr_writer :jwt_algorithm, :jwt_exp_time, :registration_endpoint
54
+
55
+ def jwt_algorithm
56
+ @jwt_algorithm ||= "HS256"
57
+ end
58
+
59
+ def jwt_exp_time
60
+ @jwt_exp_time ||= 1_658_439_475
61
+ end
62
+
63
+ def registration_endpoint
64
+ @registration_endpoint ||= "zaig/consulta_de_credito"
65
+ end
52
66
  end
53
67
  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.5
4
+ version: 1.0.8
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-18 00:00:00.000000000 Z
11
+ date: 2022-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cpf_cnpj
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jwt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: singleton
43
57
  requirement: !ruby/object:Gem::Requirement