close_encounters 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4954f6a9a487299009cb84b6af2571743e1d08d2703683edbcde73e0670a154b
4
- data.tar.gz: d05d458352582236246c0ceab9f556f8ca3c2df9c3531db3e35123e854cb6485
3
+ metadata.gz: c5a45c4e3e354b03b9079a8877bb298d552856412fb402378ea5070f2e904253
4
+ data.tar.gz: 11cc60f66488168126254be369d7a34e0a162d01c644c9f56f942685d232bb46
5
5
  SHA512:
6
- metadata.gz: 64c3973533245763a65dd67e466c6da6b7f425853aeaebeb4712d94eb115dfc36484a49ea146a93db1e77dd5848f4c8770d1768a76c5b435d43f4acdd2f196e0
7
- data.tar.gz: d2c569f12ce8da83cfc3d3eb8d437b0100436687212bbded9d10fee5989a74e5bd3c67d03b2f4d314c4903bdbcd90bc74990bba754cbc9ac5f9cfb5d2b612d46
6
+ metadata.gz: a560de8306f8e738f72fab9b070c2977301d07b4aa1e9b81ef959dbcc669d70a088f58af1fea9f640a8893ca559167758ab55ca2b8cab3cbaf2cf726d51180bd
7
+ data.tar.gz: 4e54a853db68694c94cbe3c339449ed460f6692a99df3c86a2aa11194bfd9520a0d29cf8f197e158202ff017132f2cf3de8c6a6483eed2b867976188bb3d880c
data/CHANGELOG.md CHANGED
@@ -5,15 +5,23 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
 
8
- ## [0.2.0] - 2025-02-26
8
+ ## [0.2.2] - 2025-09-04
9
9
 
10
10
  ### Changed
11
11
 
12
- - Test with Ruby 3.4
12
+ - Update handling of the same status for multiple requests when creating events.
13
+
14
+ ### Fixed
15
+
16
+ - Ensure that status is an integer when creating events and comparing results.
17
+
18
+ ## [0.2.1] - 2025-03-05
13
19
 
14
20
  ### Added
15
21
 
16
- - Ability to use CloseEncounters::Middleware and CloseEncounters.auto_contact! to automatically track responses from third-party services.
17
- - Add metadata to events for storing extra information about the event.
22
+ - Add alias for verify method as scan.
23
+ - Add configuration for auto_contact and verify_scan_statuses.
24
+
25
+ ### Changed
18
26
 
19
- ## [0.1.4] - Unreleased
27
+ - Ensure that events are only created if the status has changed or if the status is in the verify_scan_statuses list and verification fails.
@@ -10,5 +10,9 @@ module CloseEncounters
10
10
  unless ActiveRecord::Base.connection.adapter_name.downcase.include?("postgresql")
11
11
  serialize :metadata, coder: JSON
12
12
  end
13
+
14
+ def verified?
15
+ !!metadata.to_h.dig("verified")
16
+ end
13
17
  end
14
18
  end
@@ -1,3 +1,3 @@
1
1
  module CloseEncounters
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -7,6 +7,23 @@ module CloseEncounters
7
7
  autoload :ParticipantService, "close_encounters/participant_service"
8
8
  autoload :ParticipantEvent, "close_encounters/participant_event"
9
9
 
10
+ class Configuration
11
+ attr_accessor :auto_contact, :verify_scan_statuses
12
+
13
+ def initialize
14
+ @auto_contact = !!ENV["CLOSE_ENCOUNTERS_AUTO_CONTACT"]
15
+ @verify_scan_statuses = [200, 201]
16
+ end
17
+ end
18
+
19
+ def self.configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def self.configure
24
+ yield(configuration)
25
+ end
26
+
10
27
  # Record a contact with a third party service if the status has changed
11
28
  #
12
29
  # @param name [String] the name of the service
@@ -14,27 +31,43 @@ module CloseEncounters
14
31
  # @param response [String] the response object
15
32
  def contact(name, status:, response:)
16
33
  service = ParticipantService.find_by!(name:)
17
- unless service.events.newest.pick(:status) == status
18
- service.events.create!(status: status, response:)
34
+ status = status.to_i # Ensure status is always an integer
35
+
36
+ # Use a transaction with a lock to prevent race conditions
37
+ service.with_lock do
38
+ unless service.events.newest.pick(:status) == status
39
+ service.events.create!(status: status, response:)
40
+ end
19
41
  end
20
42
  end
21
43
 
22
44
  # Record a verification of a contact with a third party service where the
23
45
  # verification is a callable which must also respond to to_s.
24
46
  #
25
- # For example, provide a callable which checks the JSON Schema for a response body
26
- # and will record an event if calling the verification returns false.
47
+ # Creates a new event if:
48
+ # 1. The status has changed from the last recorded status
49
+ # 2. OR the status is in the verify_scan list AND verification fails
27
50
  #
28
51
  # @param name [String] the name of the service
29
52
  # @param status [Integer] the HTTP status of the contact
30
53
  # @param response [String] the response object
31
54
  # @param verifier [Proc] the verification callable which must also respond to to_s
32
- def verify(name, status:, response:, verifier:)
55
+ def scan(name, status:, response:, verifier:)
33
56
  service = ParticipantService.find_by!(name:)
34
- unless service.events.newest.pick(:status) == status && (verified = verifier.call(response))
35
- service.events.create!(status:, response:, metadata: {verified:, verification: verifier.to_s})
57
+ status = status.to_i # Ensure status is always an integer
58
+
59
+ service.with_lock do
60
+ if service.events.newest.pick(:status) != status
61
+ verified = verifier.call(response)
62
+ service.events.create!(status:, response:, metadata: {verified:, verification: verifier.to_s})
63
+ elsif verify_scan_statuses.include?(status)
64
+ verified = verifier.call(response)
65
+ service.events.create!(status:, response:, metadata: {verified:, verification: verifier.to_s}) if !verified
66
+ end
36
67
  end
37
68
  end
69
+ alias_method :verify, :scan
70
+ module_function :verify
38
71
 
39
72
  # Determine if contacts with third party services should be recorded automatically
40
73
  # using the Rack Middleware
@@ -44,14 +77,20 @@ module CloseEncounters
44
77
  #
45
78
  # @return [Boolean] whether or not to automatically record contacts
46
79
  def auto_contact?
47
- !!(ENV["CLOSE_ENCOUNTERS_AUTO_CONTACT"] || @auto_contact)
80
+ # If auto_contact is explicitly set, use that value
81
+ return configuration.auto_contact unless configuration.auto_contact.nil?
82
+ # Otherwise check the environment variable
83
+ !!ENV["CLOSE_ENCOUNTERS_AUTO_CONTACT"]
48
84
  end
49
85
 
50
86
  # Enable automatic contact recording in the Rack Middleware
51
87
  def auto_contact!
52
- @auto_contact = true
88
+ configuration.auto_contact = true
53
89
  end
54
90
 
91
+ # Get the statuses that should be verified
92
+ def verify_scan_statuses = configuration.verify_scan_statuses
93
+
55
94
  # Get the status of the most recent contact with a third party service
56
95
  #
57
96
  # @param name [String] the name of the service
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: close_encounters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-02-26 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: railties
@@ -129,7 +128,6 @@ metadata:
129
128
  homepage_uri: https://github.com/SOFware/close_encounters
130
129
  source_code_uri: https://github.com/SOFware/close_encounters
131
130
  changelog_uri: https://github.com/SOFware/close_encounters/blob/main/CHANGELOG.md
132
- post_install_message:
133
131
  rdoc_options: []
134
132
  require_paths:
135
133
  - lib
@@ -144,8 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
142
  - !ruby/object:Gem::Version
145
143
  version: '0'
146
144
  requirements: []
147
- rubygems_version: 3.5.9
148
- signing_key:
145
+ rubygems_version: 3.6.7
149
146
  specification_version: 4
150
147
  summary: Close Encounters of the Third Party
151
148
  test_files: []