bigid_bgcheck 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  module Integration
4
6
  class MultipartFlat < Faraday::Request::Multipart
5
- self.mime_type = 'multipart/form-data'.freeze
6
- DEFAULT_BOUNDARY_PREFIX = "-----------RubyMultipartPost".freeze unless defined? DEFAULT_BOUNDARY_PREFIX
7
+ self.mime_type = "multipart/form-data"
8
+ DEFAULT_BOUNDARY_PREFIX = "-----------RubyMultipartPost" unless defined? DEFAULT_BOUNDARY_PREFIX
7
9
 
8
10
  def process_params(params, prefix = nil, pieces = nil, &block)
9
11
  params.inject(pieces || []) do |all, (key, value)|
10
- key = "#{prefix}" if prefix
12
+ key = prefix.to_s if prefix
11
13
 
12
14
  case value
13
15
  when Array
14
- values = value.inject([]) { |a,v| a << [nil, v] }
16
+ values = value.inject([]) { |a, v| a << [nil, v] }
15
17
  process_params(values, key, all, &block)
16
18
  when Hash
17
19
  process_params(value, key, all, &block)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  module Integration
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  module Integration
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  class InternalError < BaseError
4
6
  HTTP_STATUS = 400
5
- STATUS = 'internal_error'.freeze
6
- STATUS_CODE = '-1101'.freeze
7
+ STATUS = "internal_error"
8
+ STATUS_CODE = "-1101"
7
9
 
8
10
  def http_status
9
11
  HTTP_STATUS
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  class InvalidCredentialsError < BaseError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  class InvalidDocumentValueError < BaseError
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  class NoInfoError < BaseError
4
6
  HTTP_STATUS = 200
5
- STATUS = 'no_info_error'.freeze
6
- STATUS_CODE = '-1102'.freeze
7
+ STATUS = "no_info_error"
8
+ STATUS_CODE = "-1102"
7
9
 
8
10
  def http_status
9
11
  HTTP_STATUS
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  class NotPermissionError < BaseError
@@ -1,19 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  class Request < Bigid::Bgcheck::Auth::AuthenticatedResource
4
- VALID_DOCUMENTS = %w[CPF CNPJ].freeze
5
-
6
- APPROVED = 90.freeze
7
- NOT_APPROVED = -1100.freeze
8
- ERROR = -1101.freeze
9
- NO_INFO = -1102.freeze
10
-
11
6
  def call(document:, document_type:, group:)
12
- res = @connection.post(url: Bigid::Bgcheck::SRV_ENDPOINT, body: payload(document, document_type, group).to_json)
7
+ res = @connection.post(url: Bigid::Bgcheck::SRV_ENDPOINT,
8
+ body: payload(document, document_type, group).to_json)
13
9
 
14
10
  check_status(res.status)
15
11
 
16
- return parse_response_body(res.body)
12
+ parse_response_body(res.body)
17
13
  end
18
14
 
19
15
  def self.call(document:, document_type:, group:)
@@ -21,48 +17,43 @@ module Bigid
21
17
  end
22
18
 
23
19
  private
20
+ def check_status(status)
21
+ raise Bigid::Bgcheck::InternalError unless status
22
+ raise Bigid::Bgcheck::ServerError if status == 500
23
+ raise Bigid::Bgcheck::InvalidCredentialsError if status == 401
24
+ raise Bigid::Bgcheck::BadRequestError if status == 400
25
+ end
24
26
 
25
- def check_status(status)
26
- raise Bigid::Bgcheck::InternalError unless status
27
- raise Bigid::Bgcheck::ServerError if status == 500
28
- raise Bigid::Bgcheck::InvalidCredentialsError if status == 401
29
- raise Bigid::Bgcheck::BadRequestError if status == 400
30
- end
31
-
32
- def payload(document, document_type, group, parameters = {})
33
- login = Bigid::Bgcheck.configuration&.username || ENV['BIGID_USERNAME']
34
- document_type = document_type.upcase.strip
27
+ def payload(document, document_type, group, parameters = {})
28
+ login = Bigid::Bgcheck.configuration.username
29
+ document_type = document_type.upcase.strip
35
30
 
36
- if !VALID_DOCUMENTS.include?(document_type)
37
- raise Bigid::Bgcheck::DocumentNotSupportedError
38
- end
31
+ raise Bigid::Bgcheck::DocumentNotSupportedError unless DocType.valid?(document_type)
39
32
 
40
- parameters[:"#{document_type}"] = document
33
+ parameters[:"#{document_type}"] = document
41
34
 
42
- { Login: login, parameters: parameters, Group: group }
43
- end
35
+ { Login: login, parameters: parameters, Group: group }
36
+ end
44
37
 
45
- def check_result_code(code)
46
- raise Bigid::Bgcheck::InvalidDocumentValueError unless code
47
- raise Bigid::Bgcheck::NoInfoError if code == NO_INFO
48
- end
38
+ def check_result_code(code)
39
+ raise Bigid::Bgcheck::InvalidDocumentValueError unless code
40
+ raise Bigid::Bgcheck::NoInfoError if ResultCode.no_info?(code)
41
+ end
49
42
 
50
- def parse_response_body(body)
51
- body = JSON.parse(body)
43
+ def parse_response_body(body)
44
+ body = JSON.parse(body)
52
45
 
53
- check_result_code(body['ResultCode'])
46
+ check_result_code(body["ResultCode"])
54
47
 
55
- Bigid::Bgcheck::Result.new(
56
- approved: body['ResultCode'] == APPROVED,
57
- ticket_id: body['TicketId'],
58
- code: body['ResultCode'],
59
- message: body['ResultMessage'],
60
- score: body['Score'],
61
- limit_score: body['LimitScore']
62
- )
63
- rescue StandardError
64
- raise Bigid::Bgcheck::InternalError
65
- end
48
+ Bigid::Bgcheck::Result.new(
49
+ approved: ResultCode.approved?(body["ResultCode"]),
50
+ ticket_id: body["TicketId"],
51
+ code: body["ResultCode"],
52
+ message: body["ResultMessage"],
53
+ score: body["Score"],
54
+ limit_score: body["LimitScore"]
55
+ )
56
+ end
66
57
  end
67
58
  end
68
59
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  class Result
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bigid
4
+ module Bgcheck
5
+ class ResultCode
6
+ APPROVED = 90
7
+ ERROR = -1101
8
+ NO_INFO = -1102
9
+ NOT_APPROVED = -1100
10
+
11
+ VALUES = [APPROVED, NOT_APPROVED, ERROR, NO_INFO].freeze
12
+
13
+ def self.valid?(code)
14
+ VALUES.include?(code.to_i)
15
+ end
16
+
17
+ def self.values
18
+ VALUES
19
+ end
20
+
21
+ def self.approved?(code)
22
+ code.to_i == APPROVED
23
+ end
24
+
25
+ def self.no_info?(code)
26
+ code.to_i == NO_INFO
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  class ServerError < BaseError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bigid
2
4
  module Bgcheck
3
5
  # When updating version, keep in mind Semantic Versioning http://semver.org/
@@ -7,6 +9,6 @@ module Bigid
7
9
  # Major - Incremented for incompatible changes with previous release (or big enough new features)
8
10
  # Minor - Incremented for new backwards-compatible features + deprecations
9
11
  # Patch - Incremented for backwards-compatible bug fixes
10
- VERSION = "0.1.0"
12
+ VERSION = "0.1.1"
11
13
  end
12
14
  end
data/lib/bigid_bgcheck.rb CHANGED
@@ -1,43 +1,48 @@
1
- require 'faraday'
2
- require 'i18n'
3
- require 'json'
1
+ # frozen_string_literal: true
4
2
 
5
- require 'bigid/bgcheck/version'
3
+ require "faraday"
4
+ require "i18n"
5
+ require "json"
6
6
 
7
- require 'bigid/bgcheck/integration/multipart_flat'
8
- require 'bigid/bgcheck/integration/response'
9
- require 'bigid/bgcheck/integration/request'
10
- require 'bigid/bgcheck/integration/connection'
7
+ require "bigid/bgcheck/version"
11
8
 
12
- require 'bigid/bgcheck/auth/connection'
13
- require 'bigid/bgcheck/auth/authentication'
14
- require 'bigid/bgcheck/auth/authenticated_resource'
15
- require 'bigid/bgcheck/auth/authenticated_connection'
9
+ require "bigid/bgcheck/integration/multipart_flat"
10
+ require "bigid/bgcheck/integration/response"
11
+ require "bigid/bgcheck/integration/request"
12
+ require "bigid/bgcheck/integration/connection"
16
13
 
17
- require 'bigid/bgcheck/base_error'
18
- require 'bigid/bgcheck/bad_request_error'
19
- require 'bigid/bgcheck/document_not_supported_error'
20
- require 'bigid/bgcheck/internal_error'
21
- require 'bigid/bgcheck/invalid_credentials_error'
22
- require 'bigid/bgcheck/no_info_error'
23
- require 'bigid/bgcheck/result'
24
- require 'bigid/bgcheck/server_error'
14
+ require "bigid/bgcheck/auth/connection"
15
+ require "bigid/bgcheck/auth/authentication"
16
+ require "bigid/bgcheck/auth/authenticated_resource"
17
+ require "bigid/bgcheck/auth/authenticated_connection"
25
18
 
26
- require 'bigid/bgcheck/request'
19
+ require "bigid/bgcheck/base_error"
20
+ require "bigid/bgcheck/bad_request_error"
21
+ require "bigid/bgcheck/document_not_supported_error"
22
+ require "bigid/bgcheck/internal_error"
23
+ require "bigid/bgcheck/invalid_credentials_error"
24
+ require "bigid/bgcheck/no_info_error"
25
+ require "bigid/bgcheck/server_error"
26
+
27
+ require "bigid/bgcheck/doc_type"
28
+ require "bigid/bgcheck/result"
29
+ require "bigid/bgcheck/result_code"
30
+
31
+ require "bigid/bgcheck/request"
27
32
 
28
33
  module Bigid
29
34
  module Bgcheck
30
- BASE_URL = 'https://bigid.bigdatacorp.com.br'
31
- AUTH_ENDPOINT = 'https://accesstoken.bigdatacorp.com.br'
32
- SRV_ENDPOINT = 'backgroundcheck'
33
- TOKEN_EXPIRATION = 60000.freeze
34
- # Welcome to your new gem!
35
- # This file is the entry point for your gem. Logic can go in this file, though most gems are big enough to warrant splitting
36
- # functionality out into multiple files namespaced under a module named the same as a gem.
37
- # Create those files as lib/bigid_bgcheck/foo as BigidBgcheck::Foo remembering to require each file at the top of this file,
38
- # unless you want your users to manually require a specific file (for example opt-in feature in a distinct file)
35
+ BASE_URL = "https://bigid.bigdatacorp.com.br"
36
+ AUTH_ENDPOINT = "https://accesstoken.bigdatacorp.com.br"
37
+ SRV_ENDPOINT = "backgroundcheck"
38
+ TOKEN_EXPIRATION = 60_000
39
+
39
40
  class << self
40
- attr_accessor :configuration
41
+ attr_writer :configuration
42
+
43
+ def configuration
44
+ @configuration ||= Configuration.new
45
+ end
41
46
  end
42
47
 
43
48
  def self.configure
@@ -47,16 +52,19 @@ module Bigid
47
52
  end
48
53
 
49
54
  class Configuration
50
- attr_accessor :username, :password
55
+ attr_writer :username, :password
56
+
57
+ def username
58
+ @username ||= ENV["BIGID_USERNAME"]
59
+ end
51
60
 
52
- def initialize
53
- @username = ''
54
- @password = ''
61
+ def password
62
+ @password ||= ENV["BIGID_PASSWORD"]
55
63
  end
56
64
  end
57
65
 
58
- I18n.load_path << Dir[File.expand_path("config/locales") + "/*.yml"]
59
- I18n.config.available_locales = :en
66
+ I18n.load_path << Dir["#{File.expand_path('config/locales')}/*.yml"]
67
+ I18n.config.available_locales = :en, :'pt-BR'
60
68
  I18n.default_locale = :en
61
69
  end
62
70
  end
@@ -1,7 +1,99 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe Bigid::Bgcheck do
4
- it 'should have some tests' do
5
- flunk "Please write some tests"
3
+ require "spec_helper"
4
+ require "bigid_bgcheck"
5
+
6
+ RSpec.describe Bigid::Bgcheck do
7
+ describe "BASE_URL" do
8
+ subject { defined? Bigid::Bgcheck::BASE_URL }
9
+
10
+ it { expect(subject).to be_truthy }
11
+ end
12
+
13
+ describe "AUTH_ENDPOINT" do
14
+ subject { defined? Bigid::Bgcheck::AUTH_ENDPOINT }
15
+
16
+ it { expect(subject).to be_truthy }
17
+ end
18
+
19
+ describe "SRV_ENDPOINT" do
20
+ subject { defined? Bigid::Bgcheck::SRV_ENDPOINT }
21
+
22
+ it { expect(subject).to be_truthy }
23
+ end
24
+
25
+ describe "TOKEN_EXPIRATION" do
26
+ subject { defined? Bigid::Bgcheck::TOKEN_EXPIRATION }
27
+
28
+ it { expect(subject).to be_truthy }
29
+ end
30
+
31
+ describe ".configure" do
32
+ before do
33
+ described_class.configuration = nil
34
+ ENV.clear
35
+ end
36
+
37
+ subject { described_class.configuration }
38
+
39
+ context "when configuration is defined" do
40
+ before do
41
+ described_class.configure do |config|
42
+ config.username = "username_value"
43
+ config.password = "password_value"
44
+ end
45
+ end
46
+
47
+ it { expect(subject).not_to be_nil }
48
+
49
+ it { expect(subject.username).to eq("username_value") }
50
+
51
+ it { expect(subject.password).to eq("password_value") }
52
+ end
53
+
54
+ context "when configuration is not defined" do
55
+ it { expect(subject).not_to be_nil }
56
+
57
+ it { expect(subject.username).to be_nil }
58
+
59
+ it { expect(subject.password).to be_nil }
60
+ end
61
+
62
+ context "when its configured by envs" do
63
+ before do
64
+ ENV["BIGID_USERNAME"] = "username_value"
65
+ ENV["BIGID_PASSWORD"] = "password_value"
66
+ end
67
+
68
+ it { expect(subject).not_to be_nil }
69
+
70
+ it { expect(subject.username).to eq("username_value") }
71
+
72
+ it { expect(subject.password).to eq("password_value") }
73
+ end
74
+
75
+ context "when its envs configured and configs setted" do
76
+ before do
77
+ ENV["BIGID_USERNAME"] = "username_value"
78
+ ENV["BIGID_PASSWORD"] = "password_value"
79
+
80
+ described_class.configure do |config|
81
+ config.username = "username_value2"
82
+ config.password = "password_value2"
83
+ end
84
+ end
85
+
86
+ it { expect(subject).not_to be_nil }
87
+
88
+ it { expect(subject.username).to eq("username_value2") }
89
+
90
+ it { expect(subject.password).to eq("password_value2") }
91
+ end
92
+
93
+ describe "I18n calls" do
94
+ it { expect(I18n.default_locale).to eq(:en) }
95
+
96
+ it { expect(I18n.config.available_locales).to contain_exactly(:en, :'pt-BR') }
97
+ end
6
98
  end
7
99
  end