perfect_audit 0.3.2 → 0.4.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: 9fdb4bba9cd516c21f66eb05cb8dc231ce1426ac6ce5aa3885b66db22a57d189
4
- data.tar.gz: 385d53469a6ce0bfb8e0d16356b765e5f87a8bb0841127bce9a5c6340dd0ce01
3
+ metadata.gz: af50a107a40785baccddff517b7757b7d1c6f2c035d15ccb9cabc8bde78d2397
4
+ data.tar.gz: 86776b33d5af0c4e5416530932d80465e814dff38e0b62e296e337a77efa9caa
5
5
  SHA512:
6
- metadata.gz: ece8d169ba87b7fb3d3513589409cc0c639867d0cce456cc9f266ae54c1b77fbd1a151c48dc59a99bba7cba16b74a73b5d347ffdd9ab53a765774a515ac0b446
7
- data.tar.gz: c745ff5299de3fa4daf41ff636122dc2a09502cd0913ff328389e1d84220033783ca6e4994affb6043c84526181c70edb8ea5e5ec07047d9bf7be6246a030fdb
6
+ metadata.gz: '0818afd972cd9f63e19945fb25d4d6c07a08dd9f1d12af6d8257af808504a0f503d4f599036fe6401d8e4f7c7b9b30fd9bb329e347b5f78fc2ae33b08fd196a7'
7
+ data.tar.gz: 5b38f1e894206d53182d1f7e2d7925ffc9af252dbadd37490ad435bcea58701dc5cf053736e299fdfb9598ed575a1487311619d25d23702851393cfe556338a7
data/.gitignore CHANGED
@@ -8,4 +8,7 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
 
11
- *.gem
11
+ *.gem
12
+
13
+ *.swp
14
+ *.swo
data/README.md CHANGED
@@ -29,8 +29,8 @@ Or install it yourself as:
29
29
 
30
30
  ``` ruby
31
31
  PerfectAudit.configure do |config|
32
- config.api_key = 'your_api_key'
33
- config.api_secret = 'your_api_secret'
32
+ config.client_id = 'your_client_id'
33
+ config.client_secret = 'your_client_secret'
34
34
  end
35
35
  ```
36
36
 
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-initializer'
4
+ require 'http'
5
+
6
+ module PerfectAudit
7
+ class AuthToken
8
+ extend Dry::Initializer
9
+
10
+ AUTH_PATH = 'https://auth.ocrolus.com/oauth/token'
11
+
12
+ option :client_id
13
+ option :client_secret
14
+
15
+ def get
16
+ request if expired?
17
+
18
+ @token
19
+ end
20
+
21
+ def expired?
22
+ @token.nil? || Time.now >= @expires_at
23
+ end
24
+
25
+ private
26
+
27
+ def request
28
+ response = HTTP.post(AUTH_PATH, json: credentials)
29
+
30
+ raise AuthError if response.status.client_error?
31
+ raise ServerError if response.status.server_error?
32
+
33
+ parse(response)
34
+ end
35
+
36
+ def credentials
37
+ {
38
+ client_id: client_id,
39
+ client_secret: client_secret,
40
+ grant_type: 'client_credentials',
41
+ audience: 'https://api.ocrolus.com/'
42
+ }
43
+ end
44
+
45
+ def parse(response)
46
+ response = JSON.parse(response)
47
+
48
+ @token = response['access_token']
49
+ @expires_at = Time.now + response['expires_in']
50
+ end
51
+ end
52
+ end
@@ -9,21 +9,29 @@ module PerfectAudit
9
9
 
10
10
  BASE_PATH = 'https://api.ocrolus.com/v1/'
11
11
 
12
- option :api_key
13
- option :api_secret
12
+ option :client_id
13
+ option :client_secret
14
14
 
15
15
  def post(path, params = {})
16
- HTTP.basic_auth(user: api_key, pass: api_secret).post(url(path), params)
16
+ HTTP.auth(auth).post(url(path), params)
17
17
  end
18
18
 
19
19
  def get(path, params = {})
20
- HTTP.basic_auth(user: api_key, pass: api_secret).get(url(path), params)
20
+ HTTP.auth(auth).get(url(path), params)
21
21
  end
22
22
 
23
23
  private
24
24
 
25
+ def auth
26
+ "Bearer #{auth_token.get}"
27
+ end
28
+
25
29
  def url(path)
26
30
  URI.join(BASE_PATH, path)
27
31
  end
32
+
33
+ def auth_token
34
+ @auth_token ||= AuthToken.new(client_id: client_id, client_secret: client_secret)
35
+ end
28
36
  end
29
37
  end
@@ -13,4 +13,16 @@ module PerfectAudit
13
13
  super + " [#{@code}]"
14
14
  end
15
15
  end
16
+
17
+ class AuthError < StandardError
18
+ def message
19
+ 'Your credentials are invalid or have been revoked'
20
+ end
21
+ end
22
+
23
+ class ServerError < StandardError
24
+ def message
25
+ 'Ocrolus is unavailable. Please try again later.'
26
+ end
27
+ end
16
28
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ostruct'
4
- require 'perfect_audit/error'
5
4
 
6
5
  module PerfectAudit
7
6
  class ResponseParser
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PerfectAudit
4
- VERSION = '0.3.2'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/perfect_audit.rb CHANGED
@@ -3,7 +3,9 @@
3
3
  require 'dry-container'
4
4
  require 'dry-auto_inject'
5
5
 
6
+ require 'perfect_audit/auth_token'
6
7
  require 'perfect_audit/connection'
8
+ require 'perfect_audit/error'
7
9
  require 'perfect_audit/response_parser'
8
10
  require 'perfect_audit/version'
9
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perfect_audit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Alexandrov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-27 00:00:00.000000000 Z
11
+ date: 2023-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-auto_inject
@@ -247,6 +247,7 @@ files:
247
247
  - lib/perfect_audit/api/document.rb
248
248
  - lib/perfect_audit/api/period.rb
249
249
  - lib/perfect_audit/api/repositories.rb
250
+ - lib/perfect_audit/auth_token.rb
250
251
  - lib/perfect_audit/connection.rb
251
252
  - lib/perfect_audit/error.rb
252
253
  - lib/perfect_audit/response_parser.rb