bigid_bgcheck 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +21 -20
  5. data/Rakefile +1 -1
  6. data/lib/bigid/bgcheck/request.rb +8 -4
  7. data/lib/bigid/bgcheck/result.rb +14 -8
  8. data/lib/bigid/bgcheck/version.rb +1 -1
  9. data/lib/bigid_bgcheck.rb +20 -41
  10. data/lib/locales/en.yml +17 -0
  11. data/lib/locales/pt-BR.yml +17 -0
  12. data/spec/bigid/bgcheck/bad_request_error_spec.rb +7 -0
  13. data/spec/bigid/bgcheck/base_error_spec.rb +7 -0
  14. data/spec/{lib → bigid}/bgcheck/doc_type_spec.rb +0 -1
  15. data/spec/bigid/bgcheck/document_not_supported_error_spec.rb +7 -0
  16. data/spec/bigid/bgcheck/internal_error_spec.rb +7 -0
  17. data/spec/bigid/bgcheck/invalid_credentials_error_spec.rb +7 -0
  18. data/spec/bigid/bgcheck/invalid_document_value_error_spec.rb +7 -0
  19. data/spec/bigid/bgcheck/no_info_error_spec.rb +7 -0
  20. data/spec/bigid/bgcheck/request_spec.rb +99 -0
  21. data/spec/{lib → bigid}/bgcheck/result_code_spec.rb +0 -1
  22. data/spec/bigid/bgcheck/server_error_spec.rb +7 -0
  23. data/spec/bigid_bgcheck_spec.rb +4 -23
  24. data/spec/factories/result.rb +45 -0
  25. data/spec/spec_helper.rb +29 -7
  26. data/spec/support/shared_examples/errors_shared_examples.rb +53 -0
  27. metadata +83 -45
  28. data/lib/bigid/bgcheck/auth/authenticated_connection.rb +0 -45
  29. data/lib/bigid/bgcheck/auth/authenticated_resource.rb +0 -13
  30. data/lib/bigid/bgcheck/auth/authentication.rb +0 -38
  31. data/lib/bigid/bgcheck/auth/connection.rb +0 -20
  32. data/lib/bigid/bgcheck/integration/connection.rb +0 -120
  33. data/lib/bigid/bgcheck/integration/multipart_flat.rb +0 -28
  34. data/lib/bigid/bgcheck/integration/request.rb +0 -20
  35. data/lib/bigid/bgcheck/integration/response.rb +0 -27
  36. data/lib/bigid/bgcheck/not_permission_error.rb +0 -8
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- module Auth
6
- class AuthenticatedConnection < Connection
7
- COOKIE_CACHE_KEY = "BIG_ID_AUTHENTICATION_COOKIE_CACHE_KEY"
8
-
9
- def initialize(authentication: Bigid::Bgcheck::Auth::Authentication.new,
10
- cache: defined?(Rails) ? Rails.cache : nil,
11
- request_class: Faraday,
12
- base_url: Bigid::Bgcheck::BASE_URL,
13
- token_expiration_time_in_seconds: Bigid::Bgcheck::TOKEN_EXPIRATION)
14
- super(request_class: request_class, base_url: base_url)
15
- @authentication = authentication
16
- @cache = cache
17
- @token_expiration_time_in_seconds = token_expiration_time_in_seconds
18
- end
19
-
20
- def default_headers
21
- super.merge(Authorization: (@cache ? cached_token : auth_token))
22
- end
23
-
24
- private
25
- def auth_token
26
- response = @authentication.login
27
- extract_token(response.body)
28
- end
29
-
30
- def cached_token
31
- @cache.fetch(COOKIE_CACHE_KEY, expires_in: @token_expiration_time_in_seconds.seconds) do
32
- auth_token
33
- end
34
- end
35
-
36
- def extract_token(value)
37
- raise Bigid::Bgcheck::AuthenticationError unless value
38
-
39
- value_json = JSON.parse(value)
40
- "Bearer #{value_json['token']}"
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- module Auth
6
- class AuthenticatedResource
7
- def initialize(connection: AuthenticatedConnection.new)
8
- @connection = connection
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- module Auth
6
- class Authentication
7
- def initialize(connection: Connection.new,
8
- username: Bigid::Bgcheck.configuration.username,
9
- password: Bigid::Bgcheck.configuration.password,
10
- token_expiration_time_in_seconds: Bigid::Bgcheck::AUTH_ENDPOINT)
11
- @connection = connection
12
- @token_expiration_time_in_seconds = Bigid::Bgcheck::TOKEN_EXPIRATION
13
- @username = username
14
- @password = password
15
- end
16
-
17
- def login
18
- res = @connection.post(url: "Generate", body: login_body)
19
-
20
- return res if res.status == 200
21
-
22
- raise Bigid::Bgcheck::InvalidCredentialsError if res.status == 401
23
- raise Bigid::Bgcheck::BadRequestError if res.status == 400
24
- raise Bigid::Bgcheck::ServerError if res.status == 500
25
- end
26
-
27
- private
28
- def login_body
29
- {
30
- login: @username,
31
- password: @password,
32
- expires: @token_expiration_time_in_seconds.to_i
33
- }.to_json
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- module Auth
6
- class Connection < Bigid::Bgcheck::Integration::Connection
7
- def initialize(request_class: Faraday, base_url: Bigid::Bgcheck::AUTH_ENDPOINT)
8
- super(request_class: request_class, base_url: base_url)
9
- end
10
-
11
- def default_headers
12
- {
13
- 'Content-Type': "application/json",
14
- 'Accept': "application/json"
15
- }
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- module Integration
6
- class Connection
7
- attr_reader :base_url
8
-
9
- def initialize(base_url:, request_class: Faraday)
10
- @request_class = request_class
11
- @base_url = base_url
12
- end
13
-
14
- def get(url: "", params: {}, headers: {})
15
- send_request(
16
- method: :get,
17
- url: url,
18
- params: params,
19
- headers: headers
20
- )
21
- end
22
-
23
- def post(url: "", params: {}, headers: {}, body: {}, multipart: false)
24
- send_request(
25
- method: :post,
26
- url: url,
27
- params: params,
28
- headers: headers,
29
- body: body,
30
- multipart: multipart
31
- )
32
- end
33
-
34
- def put(url: "", params: {}, headers: {}, body: {})
35
- send_request(
36
- method: :put,
37
- url: url,
38
- params: params,
39
- headers: headers,
40
- body: body
41
- )
42
- end
43
-
44
- def patch(url: "", params: {}, headers: {}, body: {})
45
- send_request(
46
- method: :patch,
47
- url: url,
48
- params: params,
49
- headers: headers,
50
- body: body
51
- )
52
- end
53
-
54
- def delete(url: "", params: {}, headers: {}, body: {})
55
- send_request(
56
- method: :delete,
57
- url: url,
58
- params: params,
59
- headers: headers,
60
- body: body
61
- )
62
- end
63
-
64
- def default_headers
65
- {}
66
- end
67
-
68
- private
69
- def send_request(method:, url:, params:, headers:, body: nil, multipart: false)
70
- connection = multipart ? multipart_connection : @request_class.new(url: @base_url)
71
-
72
- merged_headers = default_headers.merge(headers)
73
-
74
- request = build_request(
75
- method, connection.build_url(url).to_s, params, merged_headers, body
76
- )
77
-
78
- result =
79
- connection.send(method) do |request|
80
- request.url(url)
81
- request.params = params
82
- request.headers = merged_headers
83
- request.body = body if body
84
- end
85
-
86
- build_response(request, result.status, result.headers, result.body)
87
- end
88
-
89
- def multipart_connection
90
- @request_class.new(url: @base_url) do |conn|
91
- conn.use Bigid::Bgcheck::Integration::MultipartFlat
92
- conn.request :url_encoded
93
- conn.adapter @request_class.default_adapter
94
- end
95
- end
96
-
97
- def build_request(method, url, params, headers, body)
98
- Bigid::Bgcheck::Integration::Request.new(
99
- method: method,
100
- url: url,
101
- params: params,
102
- headers: headers,
103
- body: body,
104
- time: Time.now.utc
105
- )
106
- end
107
-
108
- def build_response(request, status, headers, body)
109
- Bigid::Bgcheck::Integration::Response.new(
110
- request: request,
111
- status: status,
112
- headers: headers,
113
- body: body,
114
- time: Time.now.utc
115
- )
116
- end
117
- end
118
- end
119
- end
120
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- module Integration
6
- class MultipartFlat < Faraday::Request::Multipart
7
- self.mime_type = "multipart/form-data"
8
- DEFAULT_BOUNDARY_PREFIX = "-----------RubyMultipartPost" unless defined? DEFAULT_BOUNDARY_PREFIX
9
-
10
- def process_params(params, prefix = nil, pieces = nil, &block)
11
- params.inject(pieces || []) do |all, (key, value)|
12
- key = prefix.to_s if prefix
13
-
14
- case value
15
- when Array
16
- values = value.inject([]) { |a, v| a << [nil, v] }
17
- process_params(values, key, all, &block)
18
- when Hash
19
- process_params(value, key, all, &block)
20
- else
21
- all << block.call(key, value)
22
- end
23
- end
24
- end
25
- end
26
- end
27
- end
28
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- module Integration
6
- class Request
7
- attr_reader :method, :url, :params, :headers, :body, :time
8
-
9
- def initialize(attributes)
10
- @method = attributes.fetch(:method)
11
- @url = attributes.fetch(:url)
12
- @params = attributes[:params] || {}
13
- @headers = attributes[:headers] || {}
14
- @body = attributes[:body] || {}
15
- @time = attributes[:time] || Time.current
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- module Integration
6
- class Response
7
- attr_reader :request, :status, :headers, :body, :time
8
-
9
- def initialize(attributes)
10
- @request = attributes.fetch(:request)
11
- @status = attributes.fetch(:status)
12
- @headers = attributes[:headers] || {}
13
- @body = attributes[:body] || {}
14
- @time = attributes[:time] || Time.current
15
- end
16
-
17
- def success?
18
- @status.between?(200, 299)
19
- end
20
-
21
- def error?
22
- !success?
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bigid
4
- module Bgcheck
5
- class NotPermissionError < BaseError
6
- end
7
- end
8
- end