perfect_audit 0.3.1 → 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: b2525916f152c32acfdf977f643690ea0c5485ddd61aaf0cc648d70fa8c471df
4
- data.tar.gz: 79995d6cf94966c52ecd07951224a36e5f77cbcf02edd564ccc39d2c445ec09f
3
+ metadata.gz: af50a107a40785baccddff517b7757b7d1c6f2c035d15ccb9cabc8bde78d2397
4
+ data.tar.gz: 86776b33d5af0c4e5416530932d80465e814dff38e0b62e296e337a77efa9caa
5
5
  SHA512:
6
- metadata.gz: 1b6e7b7e09e0f97cdb88e86f607b3faaffa0d1ac79041141ef6d549bd86dbaa7a7a017d2860e0f2391345ac0d7018627f89ba8e2f08a51bf8d59a4a9e0e140d1
7
- data.tar.gz: 9cf768ec14bc3767401b423da3def0969a5e826cc97b6811e1f5dc98b6dcd0c3c72649de40e9494a1530db483f20144e123370d487c844ce41d4ce16a88bc6d4
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
 
@@ -27,7 +27,7 @@ module PerfectAudit
27
27
  is_public: public.to_s
28
28
  })
29
29
 
30
- PerfectAudit::Book.new(response_parser.parse(response.body.to_s))
30
+ PerfectAudit::Book.new(**response_parser.parse(response.body.to_s))
31
31
  end
32
32
 
33
33
  # Get all books from Perfect Audit for account
@@ -38,7 +38,7 @@ module PerfectAudit
38
38
  response = connection.get(ALL_PATH)
39
39
 
40
40
  response_parser.parse(response.body.to_s).map { |item|
41
- PerfectAudit::Book.new(item)
41
+ PerfectAudit::Book.new(**item)
42
42
  }
43
43
  end
44
44
 
@@ -53,7 +53,7 @@ module PerfectAudit
53
53
  pk: id.to_s
54
54
  })
55
55
 
56
- PerfectAudit::Book.new(response_parser.parse(response.body.to_s))
56
+ PerfectAudit::Book.new(**response_parser.parse(response.body.to_s))
57
57
  end
58
58
 
59
59
  # Delete book in Perfect Audit
@@ -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.1'
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.1
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-03-14 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