zaig 1.0.3 → 1.0.6

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: 00531b692d17cf8b508317410d44fd193c247f49b73579f1c836199e457a43c1
4
- data.tar.gz: b156aa4e304031c1c65397ac458e9430038d84c7223352b7add307551ee9348d
3
+ metadata.gz: ab0151c32beb57d31bb2e3ccd10ff6408091c79c61dbbb9f85b03fed94b6498b
4
+ data.tar.gz: dc88f4a1dbb7601abddeb3c8812948a1f3c6b9633d1b6899168530831b125b4b
5
5
  SHA512:
6
- metadata.gz: 4da7e1c681ab55b6ace31c91cda4514076ffb414c8cbfffb98325514c2dafb46b9528c7608169082aa7b4ef89b2caf95a7210835f56fce796b4f7c7d55df022e
7
- data.tar.gz: 1e7ef1819184ec0c8a208e7b7b546f4fdd14ac58ef926d029a9ee9c55f3a3d1834b31273a84f32d2d254fb5c122a499dadb8207548c3b1ca03e2a57cd80c146f
6
+ metadata.gz: 12d241a7a4b5124573f7b45d5a5818075fe45b117bce2ee3d21b404c009eeb9d2f4dbeeff52f6386841c204b568b71811cad6376f23295876645a9a011fd9a1c
7
+ data.tar.gz: d761bbadde669244ad80a1644d31350796f2d1e77ea6d2e01b29cf243b7f691f26d28a49675f37567a74a5d88d1c23403fbaa42044b49afbef2121aa918885aa
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
@@ -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
@@ -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
@@ -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,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zaig
4
- class FieldValidationError < BaseError
4
+ class FieldValidationError < Zaig::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,13 +43,22 @@ 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
- zaig_msg = parsed_res[:resposta_zaig][:msg]
59
+ title = parsed_res[:resposta_zaig][:conteudo][:title]
60
+ description = parsed_res[:resposta_zaig][:conteudo][:description]
61
+ zaig_msg = "#{title}: #{description}"
55
62
  zaig_http_status = parsed_res[:resposta_zaig][:status]
56
63
 
57
64
  raise_error_by_zaig_status(zaig_msg, zaig_http_status)
@@ -59,16 +66,16 @@ module Zaig
59
66
 
60
67
  def raise_error_by_zaig_status(msg, status)
61
68
  case status
62
- when 209
63
- raise ::Zaig::AlreadyExistsError, msg
64
69
  when 400
65
- raise ::Zaig::FieldValidationError, msg
70
+ raise ::Zaig::FieldValidationError.new msg
66
71
  when 408
67
- raise ::Zaig::ExternalTimeoutError, msg
72
+ raise ::Zaig::ExternalTimeoutError.new msg
73
+ when 409
74
+ raise ::Zaig::AlreadyExistsError.new msg
68
75
  when 422
69
- raise ::Zaig::UnprocessableEntityError, msg
76
+ raise ::Zaig::UnprocessableEntityError.new msg
70
77
  else
71
- raise ::Zaig::UnexpectedError, msg
78
+ raise ::Zaig::UnexpectedError.new msg
72
79
  end
73
80
  end
74
81
  end
@@ -1,56 +1,59 @@
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
8
6
  include Singleton
9
7
 
10
8
  def call(obj)
11
- {
12
- address: build_address(obj["address"]),
13
- client_category: obj["client_category"],
14
- constitution_date: Date.parse(obj["constitution_date"]).to_s,
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
9
+ payload = {
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
+ id: obj[:id],
19
+ legal_name: obj[:legal_name],
20
+ monthly_revenue: format_money(obj[:monthly_revenue], require_positive: true),
21
+ phones: build_phones(obj[:phones]),
22
+ shareholders: build_shareholders(obj[:shareholders]),
23
+ trading_name: obj[:trading_name]
31
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
32
31
  end
33
32
 
34
33
  private
34
+ def format_money(value, require_positive: false)
35
+ require_positive && !value.to_f.positive? ? 1.0 : value.to_f
36
+ end
37
+
35
38
  def build_address(obj_address)
36
39
  {
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"]
40
+ city: obj_address[:city],
41
+ complement: obj_address[:complement] || "",
42
+ country: obj_address[:country] || "BRA",
43
+ neighborhood: obj_address[:neighborhood],
44
+ number: obj_address[:number],
45
+ postal_code: format_cep(obj_address[:postal_code]),
46
+ street: obj_address[:street],
47
+ uf: obj_address[:uf]
45
48
  }
46
49
  end
47
50
 
48
51
  def build_phone(obj)
49
52
  {
50
- area_code: obj["area_code"],
51
- international_dial_code: obj["international_dial_code"],
52
- number: obj["number"],
53
- type: obj["type"]
53
+ area_code: obj[:area_code],
54
+ international_dial_code: obj[:international_dial_code],
55
+ number: obj[:number],
56
+ type: obj[:type]
54
57
  }
55
58
  end
56
59
 
@@ -69,19 +72,19 @@ module Zaig
69
72
 
70
73
  obj_shareholders.each do |s|
71
74
  shareholders << {
72
- address: build_address(s["address"]),
73
- birthdate: Date.parse(s["birthdate"]).to_s,
74
- declared_assets: s["declared_assets"],
75
- document_number: CPF.new(s["document_number"]).number,
76
- email: s["email"],
77
- father_name: s["father_name"],
78
- gender: s["gender"],
79
- monthly_income: s["monthly_income"].to_f,
80
- mother_name: s["mother_name"],
81
- name: s["name"],
82
- nationality: s["nationality"],
83
- occupation: s["occupation"],
84
- phones: build_phones(s["phones"])
75
+ address: build_address(s[:address]),
76
+ birthdate: s[:birthdate],
77
+ declared_assets: format_money(s[:declared_assets]),
78
+ document_number: CPF.new(s[:document_number]).formatted,
79
+ email: s[:email],
80
+ father_name: filter_blank(s[:father_name]),
81
+ gender: s[:gender],
82
+ monthly_income: format_money(s[:monthly_income]),
83
+ mother_name: filter_blank(s[:mother_name]),
84
+ name: s[:name],
85
+ nationality: s[:nationality],
86
+ occupation: s[:occupation],
87
+ phones: build_phones(s[:phones])
85
88
  }
86
89
  end
87
90
  shareholders
@@ -91,12 +94,12 @@ module Zaig
91
94
  return nil if fin.nil?
92
95
 
93
96
  {
94
- amount: fin["amount"],
95
- annual_interest_rate: fin["annual_interest_rate"],
96
- cdi_percentage: fin["cdi_percentage"],
97
- currency: fin["currency"] || "BRL",
98
- interest_type: fin["interest_type"],
99
- number_of_installments: fin.key?("number_of_installments") ? fin["number_of_installments"].to_i : 0
97
+ amount: fin[:amount],
98
+ annual_interest_rate: fin[:annual_interest_rate],
99
+ cdi_percentage: fin[:cdi_percentage],
100
+ currency: fin[:currency] || "BRL",
101
+ interest_type: fin[:interest_type],
102
+ number_of_installments: fin.key?("number_of_installments") ? fin[:number_of_installments].to_i : 0
100
103
  }
101
104
  end
102
105
 
@@ -107,11 +110,11 @@ module Zaig
107
110
 
108
111
  obj_warrants.each do |w|
109
112
  warrants << {
110
- warrant_type: w["warrant_type"],
111
- address: build_address(w["address"]),
112
- property_type: w["property_type"],
113
- estimated_value: w["estimated_value"],
114
- forced_selling_value: w["forced_selling_value"]
113
+ warrant_type: w[:warrant_type],
114
+ address: build_address(w[:address]),
115
+ property_type: w[:property_type],
116
+ estimated_value: w[:estimated_value],
117
+ forced_selling_value: w[:forced_selling_value]
115
118
  }
116
119
  end
117
120
  warrants
@@ -121,9 +124,9 @@ module Zaig
121
124
  return nil if obj_source.nil?
122
125
 
123
126
  {
124
- channel: obj_source["channel"],
125
- ip: obj_source["ip"],
126
- session_id: obj_source["session_id"]
127
+ channel: obj_source[:channel],
128
+ ip: obj_source[:ip],
129
+ session_id: obj_source[:session_id]
127
130
  }
128
131
  end
129
132
 
@@ -131,14 +134,14 @@ module Zaig
131
134
  return nil if obj_src.nil?
132
135
 
133
136
  {
134
- signers: build_signers(obj_src["signers"]),
137
+ signers: build_signers(obj_src[:signers]),
135
138
  signature_evidence: {
136
- access_token: obj_src["signature_evidence"]["access_token"],
137
- additional_data: obj_src["signature_evidence"]["additional_data"],
138
- ip_address: obj_src["signature_evidence"]["ip_address"],
139
- session_id: obj_src["signature_evidence"]["session_id"],
139
+ access_token: obj_src[:signature_evidence][:access_token],
140
+ additional_data: obj_src[:signature_evidence][:additional_data],
141
+ ip_address: obj_src[:signature_evidence][:ip_address],
142
+ session_id: obj_src[:signature_evidence][:session_id],
140
143
  signed_term: {
141
- raw_text: obj_src["signature_evidence"]["signed_term"]["raw_text"]
144
+ raw_text: obj_src[:signature_evidence][:signed_term][:raw_text]
142
145
  }
143
146
  }
144
147
  }
@@ -150,13 +153,24 @@ module Zaig
150
153
  signers = []
151
154
  obj_signers.each do |s|
152
155
  signers << {
153
- document_number: CNPJ.new(s["document_number"]).number,
154
- name: s["name"],
155
- email: s["email"],
156
- phone: build_phone(s["phone"])
156
+ document_number: CNPJ.new(s[:document_number]).number,
157
+ name: s[:name],
158
+ email: s[:email],
159
+ phone: build_phone(s[:phone])
157
160
  }
158
161
  end
159
162
  signers
160
163
  end
164
+
165
+ def filter_blank(value)
166
+ return value if value.nil?
167
+
168
+ value.strip == "" ? nil : value
169
+ end
170
+
171
+ def format_cep(cep)
172
+ cep = cep.delete("-")
173
+ cep.gsub(/[^0-9]/, "").insert(5, "-")
174
+ end
161
175
  end
162
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.3"
11
+ VERSION = "1.0.6"
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.3
4
+ version: 1.0.6
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-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cpf_cnpj