close_encounters 0.2.0 → 0.2.1

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: e7c56a469fc69cd3a15c93f84ac6a3789415a41d8499df1ea2fd28f5f1aeb07b
4
+ data.tar.gz: 8e0b47517ce53491205e3a89c65a56347bed80473e7cb7ac0cec908aaddd06d8
5
5
  SHA512:
6
- metadata.gz: 64c3973533245763a65dd67e466c6da6b7f425853aeaebeb4712d94eb115dfc36484a49ea146a93db1e77dd5848f4c8770d1768a76c5b435d43f4acdd2f196e0
7
- data.tar.gz: d2c569f12ce8da83cfc3d3eb8d437b0100436687212bbded9d10fee5989a74e5bd3c67d03b2f4d314c4903bdbcd90bc74990bba754cbc9ac5f9cfb5d2b612d46
6
+ metadata.gz: a376bbdf0a61509ec060f2624a97498493decb68f7f5655eae922e2980c802378e6c3550fff069dd7d9582cf3c3923befed034bfaa1e735d83375dcbe0156cdb
7
+ data.tar.gz: 5af33cc8325f9db5f3a8a5db639049e02509111c83c096f046c1248114e85ede0b743e69fa2f5f4eba57de998c8192ad592c5d91e39c82006c0dbe17e51014d4
data/CHANGELOG.md CHANGED
@@ -5,6 +5,17 @@ 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.1] - 2025-03-05
9
+
10
+ ### Added
11
+
12
+ - Add alias for verify method as scan.
13
+ - Add configuration for auto_contact and verify_scan_statuses.
14
+
15
+ ### Changed
16
+
17
+ - 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.
18
+
8
19
  ## [0.2.0] - 2025-02-26
9
20
 
10
21
  ### Changed
@@ -15,5 +26,3 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
15
26
 
16
27
  - Ability to use CloseEncounters::Middleware and CloseEncounters.auto_contact! to automatically track responses from third-party services.
17
28
  - Add metadata to events for storing extra information about the event.
18
-
19
- ## [0.1.4] - Unreleased
@@ -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.1"
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
@@ -22,19 +39,28 @@ module CloseEncounters
22
39
  # Record a verification of a contact with a third party service where the
23
40
  # verification is a callable which must also respond to to_s.
24
41
  #
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.
42
+ # Creates a new event if:
43
+ # 1. The status has changed from the last recorded status
44
+ # 2. OR the status is in the verify_scan list AND verification fails
27
45
  #
28
46
  # @param name [String] the name of the service
29
47
  # @param status [Integer] the HTTP status of the contact
30
48
  # @param response [String] the response object
31
49
  # @param verifier [Proc] the verification callable which must also respond to to_s
32
- def verify(name, status:, response:, verifier:)
50
+ def scan(name, status:, response:, verifier:)
33
51
  service = ParticipantService.find_by!(name:)
34
- unless service.events.newest.pick(:status) == status && (verified = verifier.call(response))
52
+ last_status = service.events.newest.pick(:status)
53
+
54
+ if last_status != status
55
+ verified = verifier.call(response)
35
56
  service.events.create!(status:, response:, metadata: {verified:, verification: verifier.to_s})
57
+ elsif verify_scan_statuses.include?(status)
58
+ verified = verifier.call(response)
59
+ service.events.create!(status:, response:, metadata: {verified:, verification: verifier.to_s}) if !verified
36
60
  end
37
61
  end
62
+ alias_method :verify, :scan
63
+ module_function :verify
38
64
 
39
65
  # Determine if contacts with third party services should be recorded automatically
40
66
  # using the Rack Middleware
@@ -44,14 +70,20 @@ module CloseEncounters
44
70
  #
45
71
  # @return [Boolean] whether or not to automatically record contacts
46
72
  def auto_contact?
47
- !!(ENV["CLOSE_ENCOUNTERS_AUTO_CONTACT"] || @auto_contact)
73
+ # If auto_contact is explicitly set, use that value
74
+ return configuration.auto_contact unless configuration.auto_contact.nil?
75
+ # Otherwise check the environment variable
76
+ !!ENV["CLOSE_ENCOUNTERS_AUTO_CONTACT"]
48
77
  end
49
78
 
50
79
  # Enable automatic contact recording in the Rack Middleware
51
80
  def auto_contact!
52
- @auto_contact = true
81
+ configuration.auto_contact = true
53
82
  end
54
83
 
84
+ # Get the statuses that should be verified
85
+ def verify_scan_statuses = configuration.verify_scan_statuses
86
+
55
87
  # Get the status of the most recent contact with a third party service
56
88
  #
57
89
  # @param name [String] the name of the service
metadata CHANGED
@@ -1,14 +1,14 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-26 00:00:00.000000000 Z
11
+ date: 2025-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties