zaig 1.0.2 → 1.0.5

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: 99e8d4eed3c1a4bbc45134a8aff616695b0a0fd7e4c9d9819788a3f13c96ba87
4
- data.tar.gz: 6a93abf41c8287aac05c195c7d77300b63bcb17174132fc3fb5b4e6b668cc06a
3
+ metadata.gz: 4d7e5e123adff444d932edaf15cf93f0d56c036e19a12391957736cdb5b3a0f1
4
+ data.tar.gz: 6dff02d64d11a052be1a9dc47f77b6a490d3208b1de8b84d487527259099e546
5
5
  SHA512:
6
- metadata.gz: 95f18c8313813486b95bb353ae378a982420ed45789114c05167608fe15e263022b6ab1ecf4fdb2b837d220a8c133714a69f7c6ca512283a01f67609e60a1c60
7
- data.tar.gz: b15e3c9ee02b971ec249f7368ace43d8b67a2ecb0b0af4558e7e14cc3b69d2ea71ecf614cb8a9cbdb7c4073039727368923fced0141ce258707c81d37064b99c
6
+ metadata.gz: 06ea034e23aef7a39e5dd7fe35260746b31d8095ce9f524bb45f60b02b92d6285fe24bd305e43c1f24b49b3d0cb9e61777d6612430741bc49de0f384c88d6f7f
7
+ data.tar.gz: f637d5ab308c48793bda590598a41fdff30d8103f395c03cfaf38e87eba2446b4ef77cfe30d3020d174f397e10990802477865559610d80859d4f14f6382bad8
data/lib/locales/en.yml CHANGED
@@ -2,5 +2,6 @@ en:
2
2
  name: Zaig
3
3
  zaig:
4
4
  errors:
5
+ validation_error: An validation error was got
5
6
  field_not_found: Field not found in the server response '{field_name}'
6
7
  unexpected_error: Integration returned an unexpected status code
@@ -2,5 +2,6 @@
2
2
  name: Zaig
3
3
  zaig:
4
4
  errors:
5
+ validation_error: Houve um problema na validação dos campos
5
6
  field_not_found: Campo não encontrado na resposta do servidor '{field_name}'
6
7
  unexpected_error: Integração retornou um estado inesperado
@@ -7,9 +7,18 @@ module Zaig
7
7
  attr_reader :analysis_status, :credit_proposal_legal_person_key, :message,
8
8
  :raw_data, :reason, :request, :status_code, :zaig_id
9
9
 
10
+ # Collection with all the approval statuses.
11
+ APPROVAL_STATUS = %i[automatically_approved manually_approved].freeze
12
+
13
+ # Collection with all the pending statuses.
14
+ PENDING_STATUS = %i[pending in_manual_analysis in_manual_analysis].freeze
15
+
16
+ # Colllection with all the reproval statuses.
17
+ REPROVAL_STATUS = %i[automatically_reproved manually_reproved].freeze
18
+
10
19
  def initialize(analysis_status: nil, credit_proposal_legal_person_key: nil, message: nil,
11
20
  raw_data: nil, reason: nil, request: nil, status_code: nil, zaig_id: nil)
12
- @analysis_status = analysis_status
21
+ @analysis_status = analysis_status.nil? ? nil : analysis_status.to_s.strip.downcase.to_sym
13
22
  @credit_proposal_legal_person_key = credit_proposal_legal_person_key
14
23
  @message = message
15
24
  @raw_data = raw_data
@@ -19,9 +28,27 @@ module Zaig
19
28
  @zaig_id = zaig_id
20
29
  end
21
30
 
31
+ # Retrieve all the available statuses.
32
+ def self.statuses
33
+ %i[automatically_approved automatically_reproved in_manual_analysis manually_approved manually_reproved waiting_for_data pending]
34
+ end
35
+
36
+ # Check if the registration is approved.
22
37
  def approved?
23
- analysis_status == "automatically_approved"
38
+ APPROVAL_STATUS.include?(analysis_status)
24
39
  end
40
+
41
+ # Check if the registration still not done.
42
+ def pending?
43
+ PENDING_STATUS.include?(analysis_status)
44
+ end
45
+
46
+ # Check if the registration is reproved.
47
+ def rejected?
48
+ REPROVAL_STATUS.include?(analysis_status)
49
+ end
50
+
51
+ alias not_approved? rejected?
25
52
  end
26
53
  end
27
54
  end
@@ -2,5 +2,6 @@
2
2
 
3
3
  module Zaig
4
4
  class FieldValidationError < BaseError
5
+ attr_accessor :detail
5
6
  end
6
7
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "singleton"
4
-
5
3
  module Zaig
6
4
  # Class to request a registration to the remote server.
7
5
  class Registration
@@ -45,11 +43,18 @@ module Zaig
45
43
  def verify_response(res)
46
44
  return if res.status.between?(200, 299)
47
45
 
48
- raise ::Zaig::UnexpectedError, I18n.t("zaig.errors.unexpected_error") if res.status != 500
46
+ raise ::Zaig::ServerError, I18n.t("zaig.errors.unexpected_error") if res.status.between?(500, 599)
49
47
 
50
48
  parsed_res = JSON.parse(res.body, symbolize_names: true)
51
49
 
52
- raise ::Zaig::ServerError, I18n.t("zaig.errors.field_not_found", field_name: "resposta_zaig") unless parsed_res.key?(:resposta_zaig)
50
+ unless parsed_res.key?(:resposta_zaig)
51
+ detail = JSON.parse(res.body, symbolize_names: true)[:detail]
52
+
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]
55
+
56
+ raise error
57
+ end
53
58
 
54
59
  zaig_msg = parsed_res[:resposta_zaig][:msg]
55
60
  zaig_http_status = parsed_res[:resposta_zaig][:status]
@@ -59,12 +64,12 @@ module Zaig
59
64
 
60
65
  def raise_error_by_zaig_status(msg, status)
61
66
  case status
62
- when 209
63
- raise ::Zaig::AlreadyExistsError, msg
64
67
  when 400
65
68
  raise ::Zaig::FieldValidationError, msg
66
69
  when 408
67
70
  raise ::Zaig::ExternalTimeoutError, msg
71
+ when 409
72
+ raise ::Zaig::AlreadyExistsError, msg
68
73
  when 422
69
74
  raise ::Zaig::UnprocessableEntityError, msg
70
75
  else
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "singleton"
4
-
5
3
  module Zaig
6
4
  # Service class to build a registration payload request.
7
5
  class RegistrationPayload
@@ -9,48 +7,52 @@ module Zaig
9
7
 
10
8
  def call(obj)
11
9
  {
12
- address: build_address(obj["address"]),
13
- client_category: obj["client_category"],
14
- constitution_date: obj["constitution_date"],
15
- constitution_type: obj["constitution_type"],
16
- credit_request_date: obj["credit_request_date"],
17
- credit_type: obj["credit_type"] || "clean",
18
- document_number: CNPJ.new(obj["document_number"]).number,
19
- email: obj["email"],
20
- financial: obj.key?("financial") ? build_financial(obj["financial"]) : nil,
21
- guarantors: obj.key?("guarantors") ? build_shareholders(obj["guarantors"]) : nil,
22
- id: obj["id"],
23
- legal_name: obj["legal_name"],
24
- monthly_revenue: obj["monthly_revenue"].to_f,
25
- phones: build_phones(obj["phones"]),
26
- shareholders: build_shareholders(obj["shareholders"]),
27
- source: obj.key?("source") ? build_source(obj["source"]) : nil,
28
- scr_parameters: obj.key?("scr_parameters") ? build_scr_parameters(obj["scr_parameters"]) : nil,
29
- trading_name: obj["trading_name"],
30
- warrants: obj.key?("warrants") ? build_warrants(obj["warrants"]) : nil
10
+ address: build_address(obj[:address]),
11
+ client_category: obj[:client_category],
12
+ constitution_date: obj[:constitution_date],
13
+ constitution_type: obj[:constitution_type],
14
+ credit_request_date: obj[:credit_request_date],
15
+ credit_type: obj[:credit_type] || "clean",
16
+ document_number: CNPJ.new(obj[:document_number]).formatted,
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
+ id: obj[:id],
21
+ legal_name: obj[:legal_name],
22
+ monthly_revenue: format_money(obj[:monthly_revenue], require_positive: true),
23
+ phones: build_phones(obj[:phones]),
24
+ 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
31
29
  }
32
30
  end
33
31
 
34
32
  private
33
+ def format_money(value, require_positive: false)
34
+ require_positive && !value.to_f.positive? ? 1.0 : value.to_f
35
+ end
36
+
35
37
  def build_address(obj_address)
36
38
  {
37
- city: obj_address["city"],
38
- complement: obj_address["complement"] || "",
39
- country: obj_address["country"] || "BRA",
40
- neighborhood: obj_address["neighborhood"],
41
- number: obj_address["number"],
42
- postal_code: obj_address["postal_code"],
43
- street: obj_address["street"],
44
- uf: obj_address["uf"]
39
+ city: obj_address[:city],
40
+ complement: obj_address[:complement] || "",
41
+ country: obj_address[:country] || "BRA",
42
+ neighborhood: obj_address[:neighborhood],
43
+ number: obj_address[:number],
44
+ postal_code: format_cep(obj_address[:postal_code]),
45
+ street: obj_address[:street],
46
+ uf: obj_address[:uf]
45
47
  }
46
48
  end
47
49
 
48
50
  def build_phone(obj)
49
51
  {
50
- area_code: obj["area_code"],
51
- international_dial_code: obj["international_dial_code"],
52
- number: obj["number"],
53
- type: obj["type"]
52
+ area_code: obj[:area_code],
53
+ international_dial_code: obj[:international_dial_code],
54
+ number: obj[:number],
55
+ type: obj[:type]
54
56
  }
55
57
  end
56
58
 
@@ -63,88 +65,104 @@ module Zaig
63
65
  end
64
66
 
65
67
  def build_shareholders(obj_shareholders)
68
+ return nil if obj_shareholders.nil?
69
+
66
70
  shareholders = []
67
71
 
68
72
  obj_shareholders.each do |s|
69
73
  shareholders << {
70
- address: build_address(s["address"]),
71
- birthdate: Date.parse(s["birthdate"]).to_s,
72
- declared_assets: s["declared_assets"],
73
- document_number: CPF.new(s["document_number"]).number,
74
- email: s["email"],
75
- father_name: s["father_name"],
76
- gender: s["gender"],
77
- monthly_income: s["monthly_income"].to_f,
78
- mother_name: s["mother_name"],
79
- name: s["name"],
80
- nationality: s["nationality"],
81
- occupation: s["occupation"],
82
- phones: build_phones(s["phones"])
74
+ address: build_address(s[:address]),
75
+ birthdate: s[:birthdate],
76
+ declared_assets: format_money(s[:declared_assets]),
77
+ document_number: CPF.new(s[:document_number]).formatted,
78
+ email: s[:email],
79
+ father_name: s[:father_name],
80
+ gender: s[:gender],
81
+ monthly_income: format_money(s[:monthly_income]),
82
+ mother_name: s[:mother_name],
83
+ name: s[:name],
84
+ nationality: s[:nationality],
85
+ occupation: s[:occupation],
86
+ phones: build_phones(s[:phones])
83
87
  }
84
88
  end
85
89
  shareholders
86
90
  end
87
91
 
88
92
  def build_financial(fin)
93
+ return nil if fin.nil?
94
+
89
95
  {
90
- amount: fin["amount"],
91
- annual_interest_rate: fin["annual_interest_rate"],
92
- cdi_percentage: fin["cdi_percentage"],
93
- currency: fin["currency"] || "BRL",
94
- interest_type: fin["interest_type"],
95
- number_of_installments: fin.key?("number_of_installments") ? fin["number_of_installments"].to_i : 0
96
+ amount: fin[:amount],
97
+ annual_interest_rate: fin[:annual_interest_rate],
98
+ cdi_percentage: fin[:cdi_percentage],
99
+ currency: fin[:currency] || "BRL",
100
+ interest_type: fin[:interest_type],
101
+ number_of_installments: fin.key?("number_of_installments") ? fin[:number_of_installments].to_i : 0
96
102
  }
97
103
  end
98
104
 
99
105
  def build_warrants(obj_warrants)
106
+ return nil if obj_warrants.nil?
107
+
100
108
  warrants = []
101
109
 
102
110
  obj_warrants.each do |w|
103
111
  warrants << {
104
- warrant_type: w["warrant_type"],
105
- address: build_address(w["address"]),
106
- property_type: w["property_type"],
107
- estimated_value: w["estimated_value"],
108
- forced_selling_value: w["forced_selling_value"]
112
+ warrant_type: w[:warrant_type],
113
+ address: build_address(w[:address]),
114
+ property_type: w[:property_type],
115
+ estimated_value: w[:estimated_value],
116
+ forced_selling_value: w[:forced_selling_value]
109
117
  }
110
118
  end
111
119
  warrants
112
120
  end
113
121
 
114
122
  def build_source(obj_source)
123
+ return nil if obj_source.nil?
124
+
115
125
  {
116
- channel: obj_source["channel"],
117
- ip: obj_source["ip"],
118
- session_id: obj_source["session_id"]
126
+ channel: obj_source[:channel],
127
+ ip: obj_source[:ip],
128
+ session_id: obj_source[:session_id]
119
129
  }
120
130
  end
121
131
 
122
132
  def build_scr_parameters(obj_src)
133
+ return nil if obj_src.nil?
134
+
123
135
  {
124
- signers: build_signers(obj_src["signers"]),
136
+ signers: build_signers(obj_src[:signers]),
125
137
  signature_evidence: {
126
- access_token: obj_src["signature_evidence"]["access_token"],
127
- additional_data: obj_src["signature_evidence"]["additional_data"],
128
- ip_address: obj_src["signature_evidence"]["ip_address"],
129
- session_id: obj_src["signature_evidence"]["session_id"],
138
+ access_token: obj_src[:signature_evidence][:access_token],
139
+ additional_data: obj_src[:signature_evidence][:additional_data],
140
+ ip_address: obj_src[:signature_evidence][:ip_address],
141
+ session_id: obj_src[:signature_evidence][:session_id],
130
142
  signed_term: {
131
- raw_text: obj_src["signature_evidence"]["signed_term"]["raw_text"]
143
+ raw_text: obj_src[:signature_evidence][:signed_term][:raw_text]
132
144
  }
133
145
  }
134
146
  }
135
147
  end
136
148
 
137
149
  def build_signers(obj_signers)
150
+ return nil if obj_signers.nil?
151
+
138
152
  signers = []
139
153
  obj_signers.each do |s|
140
154
  signers << {
141
- document_number: CNPJ.new(s["document_number"]).number,
142
- name: s["name"],
143
- email: s["email"],
144
- phone: build_phone(s["phone"])
155
+ document_number: CNPJ.new(s[:document_number]).number,
156
+ name: s[:name],
157
+ email: s[:email],
158
+ phone: build_phone(s[:phone])
145
159
  }
146
160
  end
147
161
  signers
148
162
  end
163
+
164
+ def format_cep(cep)
165
+ cep.gsub(/[^0-9]/, "").insert(5, "-")
166
+ end
149
167
  end
150
168
  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.2"
11
+ VERSION = "1.0.5"
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 "singleton"
6
7
 
7
8
  require "zaig/base_error"
8
9
  require "zaig/already_exists_error"
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.2
4
+ version: 1.0.5
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-15 00:00:00.000000000 Z
11
+ date: 2022-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cpf_cnpj