zaig 1.0.4 → 1.0.7

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: 445f2312bf3306d59b548373e139522dbea660d2078c7c617837ffbff8429dc7
4
- data.tar.gz: 2dfd8b0ac9f20729abf3df0447a5a3a3d349371bd3558d86e62ff84355f89f20
3
+ metadata.gz: 8772408f0511d1fe3a5de65ddf6344af3d32754bdb7ce95252a05cb3e3cb144c
4
+ data.tar.gz: d01ca6b7f88430671f09a61a1af872650438113fac5bce59aa74838808b267cc
5
5
  SHA512:
6
- metadata.gz: c055883e1eac329a7249f79542eb10186fd2a508575e90a1e4dfd10f68e95ab2819f05386213ee4a06e37158440245a588f1d3ee260f8790a5759abb856652c7
7
- data.tar.gz: b0ea93f0f3a76a3e7b4a698096e284dc5734c4cfd915f28a2de38cc4882bab5309065854061f07d1004e311c4b20a52ec0d1c034828ff054c7979233ef16c51d
6
+ metadata.gz: bacc2f06a07747edfff29022645d937cca069244b7857604bcebb0926578a498a3224c62153e8b5d18d40bc5754ed61dc29701db2bae3cab247db95b73cee06d
7
+ data.tar.gz: f1d7438aae92727b67056b38ddea0e39760ecc9ae925883682faed8b5ef4740cbdc0d9fd5b691740087b72b4555b95e0f45c915658ba8e14eb7f0d9450b06bf2
@@ -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
@@ -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,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
 
@@ -56,7 +54,9 @@ module Zaig
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
@@ -76,10 +77,10 @@ module Zaig
76
77
  declared_assets: format_money(s[:declared_assets]),
77
78
  document_number: CPF.new(s[:document_number]).formatted,
78
79
  email: s[:email],
79
- father_name: s[:father_name],
80
+ father_name: filter_blank(s[:father_name]),
80
81
  gender: s[:gender],
81
82
  monthly_income: format_money(s[:monthly_income]),
82
- mother_name: s[:mother_name],
83
+ mother_name: filter_blank(s[:mother_name]),
83
84
  name: s[:name],
84
85
  nationality: s[:nationality],
85
86
  occupation: s[:occupation],
@@ -161,7 +162,14 @@ module Zaig
161
162
  signers
162
163
  end
163
164
 
165
+ def filter_blank(value)
166
+ return value if value.nil?
167
+
168
+ value.strip == "" ? nil : value
169
+ end
170
+
164
171
  def format_cep(cep)
172
+ cep = cep.delete("-")
165
173
  cep.gsub(/[^0-9]/, "").insert(5, "-")
166
174
  end
167
175
  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.4"
11
+ VERSION = "1.0.7"
12
12
  end
data/lib/zaig.rb CHANGED
@@ -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 ||= 1_658_439_475
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.4
4
+ version: 1.0.7
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-20 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