easy_compliance 1.0.2 → 1.1.0

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: 69b54b25d1b74022e38061b4efdf13e80bd60e0e5d6fef95175f00fc04c781d2
4
- data.tar.gz: afb3c1aa893049af9cad6caa269383ee65ac1446bac94ba040674dd59ca6c6d1
3
+ metadata.gz: a18dc5645db8718da9f22831fcf6f458d74763536dfe8edaccf3f4439ae91b95
4
+ data.tar.gz: 9db9644054a7c647e2e0801bb958b6208943088d9012bf6e3f9be8600da7ef46
5
5
  SHA512:
6
- metadata.gz: 7dfc566c12036ae5c3881f46894b031b8f7f08a34ca140c6ee89196ab2b41f3af22311af4915ff030bc7cc845d89a4a2a9086547a1bfa4b20829a945fc0a1c8e
7
- data.tar.gz: da496a845d76caa9910097ad9d349ac9164f0ba1f98a3462701f4c3c59e8b96f51d4ba04c118d3584a333374845785d0929696c3ee1db004be6dd93aab7498ce
6
+ metadata.gz: 795a143c9b365c3f38d1794fb41f200ce0c6b9f5e5722061d31a6b27b7e9b577fb2bb747e9c8c03f26b21f68cde42ebc94ba4a3151c7c443734701c1c0a56286
7
+ data.tar.gz: c687ec1583a5ba413e09072adb60f206326921ce6ade5bb32a3e361aa7e5ba4fcb24bb96f786a57544b7419923ffb332526ac45dbe02e17555fc0c482fcf3cf0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_compliance (1.0.1)
4
+ easy_compliance (1.0.2)
5
5
  excon
6
6
 
7
7
  GEM
@@ -9,7 +9,7 @@ GEM
9
9
  specs:
10
10
  byebug (11.1.3)
11
11
  diff-lcs (1.4.4)
12
- excon (0.79.0)
12
+ excon (0.93.1)
13
13
  rake (13.0.3)
14
14
  rspec (3.10.0)
15
15
  rspec-core (~> 3.10.0)
@@ -27,6 +27,7 @@ GEM
27
27
 
28
28
  PLATFORMS
29
29
  x86_64-darwin-19
30
+ x86_64-darwin-20
30
31
 
31
32
  DEPENDENCIES
32
33
  bundler
@@ -36,4 +37,4 @@ DEPENDENCIES
36
37
  rspec
37
38
 
38
39
  BUNDLED WITH
39
- 2.2.8
40
+ 2.2.25
data/README.md CHANGED
@@ -9,13 +9,20 @@ Ruby toolkit for https://www.easycompliance.de
9
9
  Use `EasyCompliance::Ref` to build refs for the records.
10
10
 
11
11
  ### Keeping data in sync
12
+ When a request fails EasyCompliance will consider the request idempotent and by default retries 3 times with a delay of 5 seconds in between.
13
+ You can adjust this to fit your needs.
12
14
 
13
15
  ```ruby
14
16
  # config/initializers/easy_compliance.rb
17
+ # required
15
18
  EasyCompliance.api_key = 'my_key'
16
19
  EasyCompliance.api_url = 'https://example.com'
17
20
  EasyCompliance.app_name = 'my_app'
18
21
 
22
+ # optional
23
+ EasyCompliance.retry_limit = 3 # Max number of retries (this is the default)
24
+ EasyCompliance.retry_interval = 5 # Delay between retries in seconds (this is the default)
25
+
19
26
  # app/models/my_record.rb
20
27
  class MyRecord < ActiveRecord::Base
21
28
  after_save do
@@ -26,8 +33,18 @@ end
26
33
  # app/jobs/compliance_job.rb
27
34
  class ComplianceJob
28
35
  def perform(record, value)
29
- production_env? &&
30
- EasyCompliance::Client.submit(record: record, value: value)
36
+ return unless production_env?
37
+
38
+ check = EasyCompliance::Client.submit(record: record, value: value)
39
+ check.hit? # true/false
40
+
41
+ check.status # http response status e.g. 200
42
+ check.body # http response body
31
43
  end
32
44
  end
33
45
  ```
46
+
47
+ ## License
48
+
49
+ `EasyCompliance` is licensed under the [Apache 2.0 license](LICENSE.txt) and
50
+ Copyright 2021,2022 [betterplace / gut.org gAG](https://gut.org).
@@ -29,20 +29,37 @@ module EasyCompliance
29
29
  end
30
30
 
31
31
  HEADERS = {
32
- 'Content-Type': 'application/x-www-form-urlencoded',
32
+ 'Content-Type': 'application/x-www-form-urlencoded'
33
33
  }
34
34
 
35
35
  # @return [ EasyCompliance::Result ]
36
36
  def post(**body)
37
- url = EasyCompliance.api_url or raise Error, "must set api_url"
38
- body[:api_key] = EasyCompliance.api_key or raise Error, "must set api_key"
39
-
40
- res = Excon.post(url, body: URI.encode_www_form(body), headers: HEADERS)
37
+ body[:api_key] = EasyCompliance.api_key or raise Error, 'must set api_key'
38
+ res = Excon.post(
39
+ api_url,
40
+ body: URI.encode_www_form(body),
41
+ headers: HEADERS,
42
+ idempotent: true,
43
+ retry_limit: retry_limit,
44
+ retry_interval: retry_interval
45
+ )
41
46
  res.status < 300 or raise Error, "#{res.status}: #{res.body}"
42
47
 
43
48
  EasyCompliance::Result.new(status: res.status, body: res.body)
44
49
  rescue Excon::Error, OpenSSL::OpenSSLError => e
45
50
  raise Error, "Network error: #{e.class.name} - #{e.message}"
46
51
  end
52
+
53
+ def retry_limit
54
+ EasyCompliance.retry_limit || 3
55
+ end
56
+
57
+ def retry_interval
58
+ EasyCompliance.retry_interval || 5
59
+ end
60
+
61
+ def api_url
62
+ EasyCompliance.api_url || raise(Error, 'must set api_url')
63
+ end
47
64
  end
48
65
  end
@@ -9,6 +9,7 @@ module EasyCompliance
9
9
  end
10
10
 
11
11
  # For methods 1-3. 204 means no hit.
12
+ # returns true if record was found on a sanctions lists, details in body.
12
13
  def hit?
13
14
  status == 200
14
15
  end
@@ -1,3 +1,3 @@
1
1
  module EasyCompliance
2
- VERSION = '1.0.2'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -4,7 +4,7 @@ module EasyCompliance
4
4
  class Error < StandardError; end
5
5
 
6
6
  class << self
7
- attr_accessor :api_key, :api_url, :app_name
7
+ attr_accessor :api_key, :api_url, :app_name, :retry_limit, :retry_interval
8
8
  end
9
9
  end
10
10
 
@@ -5,12 +5,21 @@ describe EasyCompliance::Client do
5
5
  before do
6
6
  EasyCompliance.api_key = 'key'
7
7
  EasyCompliance.api_url = 'url'
8
+ EasyCompliance.retry_limit = nil
9
+ EasyCompliance.retry_interval = nil
8
10
  end
9
11
 
10
12
  describe '#post' do
11
13
  it 'posts to the API URL and returns an EasyCompliance::Result' do
12
14
  expect(Excon).to receive(:post)
13
- .with('url', body: 'method=2&api_key=key', headers: anything)
15
+ .with(
16
+ 'url',
17
+ body: 'method=2&api_key=key',
18
+ headers: anything,
19
+ idempotent: true,
20
+ retry_limit: 3,
21
+ retry_interval: 5
22
+ )
14
23
  .and_return(double(body: '{}', status: 200))
15
24
  result = client.post(method: 2)
16
25
  expect(result).to be_an EasyCompliance::Result
@@ -19,21 +28,42 @@ describe EasyCompliance::Client do
19
28
 
20
29
  it 'raises if the API returned a bad status code' do
21
30
  expect(Excon).to receive(:post)
22
- .with('url', body: 'method=2&api_key=key', headers: anything)
31
+ .with(
32
+ 'url',
33
+ body: 'method=2&api_key=key',
34
+ headers: anything,
35
+ idempotent: true,
36
+ retry_limit: 3,
37
+ retry_interval: 5
38
+ )
23
39
  .and_return(double(body: '{}', status: 500))
24
40
  expect { client.post(method: 2) }.to raise_error(client::Error, /500/)
25
41
  end
26
42
 
27
43
  it 'raises if the connection failed' do
28
44
  expect(Excon).to receive(:post)
29
- .with('url', body: 'method=2&api_key=key', headers: anything)
45
+ .with(
46
+ 'url',
47
+ body: 'method=2&api_key=key',
48
+ headers: anything,
49
+ idempotent: true,
50
+ retry_limit: 3,
51
+ retry_interval: 5
52
+ )
30
53
  .and_raise(Excon::Errors::SocketError.new)
31
54
  expect { client.post(method: 2) }.to raise_error(client::Error, /Socket/)
32
55
  end
33
56
 
34
57
  it 'raises if there is any OpenSSL::OpenSSLError' do
35
58
  expect(Excon).to receive(:post)
36
- .with('url', body: 'method=2&api_key=key', headers: anything)
59
+ .with(
60
+ 'url',
61
+ body: 'method=2&api_key=key',
62
+ headers: anything,
63
+ idempotent: true,
64
+ retry_limit: 3,
65
+ retry_interval: 5
66
+ )
37
67
  .and_raise(OpenSSL::SSL::SSLErrorWaitReadable.new)
38
68
  expect { client.post(method: 2) }.to raise_error(client::Error, /SSLError/)
39
69
  end
@@ -47,5 +77,52 @@ describe EasyCompliance::Client do
47
77
  EasyCompliance.api_url = nil
48
78
  expect { client.post(method: 2) }.to raise_error(client::Error, /api_url/)
49
79
  end
80
+
81
+ it 'uses a default value of 3 for retry_limit unless it is set' do
82
+ expect(Excon).to receive(:post).with(
83
+ 'url',
84
+ body: 'method=2&api_key=key',
85
+ headers: anything,
86
+ idempotent: true,
87
+ retry_limit: 3,
88
+ retry_interval: 5
89
+ ).and_return(double(body: '{}', status: 200))
90
+ client.post(method: 2)
91
+
92
+ EasyCompliance.retry_limit = 0
93
+ expect(Excon).to receive(:post).with(
94
+ 'url',
95
+ body: 'method=2&api_key=key',
96
+ headers: anything,
97
+ idempotent: true,
98
+ retry_limit: 0,
99
+ retry_interval: 5
100
+ ).and_return(double(body: '{}', status: 200))
101
+ client.post(method: 2)
102
+ end
103
+
104
+ it 'uses a default value of 5 for retry_interval unless it is set' do
105
+ EasyCompliance.retry_interval = nil
106
+ expect(Excon).to receive(:post).with(
107
+ 'url',
108
+ body: 'method=2&api_key=key',
109
+ headers: anything,
110
+ idempotent: true,
111
+ retry_limit: 3,
112
+ retry_interval: 5
113
+ ).and_return(double(body: '{}', status: 200))
114
+ client.post(method: 2)
115
+
116
+ EasyCompliance.retry_interval = 0
117
+ expect(Excon).to receive(:post).with(
118
+ 'url',
119
+ body: 'method=2&api_key=key',
120
+ headers: anything,
121
+ idempotent: true,
122
+ retry_limit: 3,
123
+ retry_interval: 0
124
+ ).and_return(double(body: '{}', status: 200))
125
+ client.post(method: 2)
126
+ end
50
127
  end
51
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_compliance
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - betterplace development team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-01 00:00:00.000000000 Z
11
+ date: 2022-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon
@@ -127,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  - !ruby/object:Gem::Version
128
128
  version: '0'
129
129
  requirements: []
130
- rubygems_version: 3.2.3
130
+ rubygems_version: 3.2.25
131
131
  signing_key:
132
132
  specification_version: 4
133
133
  summary: Ruby toolkit for https://www.easycompliance.de