close_encounters 0.1.3 → 0.2.0

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: ad2eb14acac02e5a5795ffd9508b0bb7dd19987265e398bcdbb73fe48ff42407
4
- data.tar.gz: 123e788ca23010056255b21bc9a71a4fd27e4bdd1e340586b22147f8fe10e8cb
3
+ metadata.gz: 4954f6a9a487299009cb84b6af2571743e1d08d2703683edbcde73e0670a154b
4
+ data.tar.gz: d05d458352582236246c0ceab9f556f8ca3c2df9c3531db3e35123e854cb6485
5
5
  SHA512:
6
- metadata.gz: fdf6130ae5170dc17f1af37cc16e7405c334b722d0ced759c8012782a1b314a73a518886bbd5f7d9199384097bf8554373379da16cb0a92dea781190095ff028
7
- data.tar.gz: a652c2aaa5215275695811da24003816689a19de14407ba053186ef7c007f9825010d5053b00e2cd9421b6399f74f4b44d7255994ee22bab98f57056f360a897
6
+ metadata.gz: 64c3973533245763a65dd67e466c6da6b7f425853aeaebeb4712d94eb115dfc36484a49ea146a93db1e77dd5848f4c8770d1768a76c5b435d43f4acdd2f196e0
7
+ data.tar.gz: d2c569f12ce8da83cfc3d3eb8d437b0100436687212bbded9d10fee5989a74e5bd3c67d03b2f4d314c4903bdbcd90bc74990bba754cbc9ac5f9cfb5d2b612d46
data/CHANGELOG.md CHANGED
@@ -5,24 +5,15 @@ 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.1.3] - 2024-07-11
8
+ ## [0.2.0] - 2025-02-26
9
9
 
10
- ## [] -
10
+ ### Changed
11
11
 
12
- ###
13
-
14
- - [] -
15
-
16
- ### Removed
17
-
18
- - Remove serialize connection_info for manual methods
19
-
20
- ## [0.1.2] - 2024-07-11
12
+ - Test with Ruby 3.4
21
13
 
22
14
  ### Added
23
15
 
24
- - Check if the table exits before conditionally setting up the serialize column
25
-
26
- ### Removed
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.
27
18
 
28
- - Appending engine migrations to the main app's migration path
19
+ ## [0.1.4] - Unreleased
data/README.md CHANGED
@@ -27,6 +27,23 @@ CloseEncounters.status("SomeThirdPartyService") # => 200
27
27
  CloseEncounters.status("SomeThirdPartyService") # => 500
28
28
  ```
29
29
 
30
+ ### Rack Middleware
31
+
32
+ Setup your middleware in your `config/application.rb` or `config/environments/production.rb`.
33
+
34
+ ```ruby
35
+ config.middleware.use CloseEncounters::Middleware
36
+ ```
37
+
38
+ This will automatically track responses from third-party services when you store a "domain" key in the
39
+ `connection_info` in the ParticipantService records.
40
+
41
+ Alternatively, you can use the `auto_contact!` method to automatically turn on the rack middleware:
42
+
43
+ ```ruby
44
+ CloseEncounters.auto_contact!
45
+ ```
46
+
30
47
  ### TODO
31
48
 
32
49
  - [ ] Add JS to the gem to track events on the front-end.
@@ -6,5 +6,9 @@ module CloseEncounters
6
6
  foreign_key: "close_encounters_participant_service_id"
7
7
 
8
8
  scope :newest, -> { order(created_at: :desc).limit(1) }
9
+
10
+ unless ActiveRecord::Base.connection.adapter_name.downcase.include?("postgresql")
11
+ serialize :metadata, coder: JSON
12
+ end
9
13
  end
10
14
  end
@@ -0,0 +1,9 @@
1
+ class AddMetadataToCloseEncountersParticipantEvents < ActiveRecord::Migration[7.1]
2
+ def change
3
+ if ActiveRecord::Base.connection.adapter_name.downcase.include?("postgresql")
4
+ add_column :close_encounters_participant_events, :metadata, :jsonb
5
+ else
6
+ add_column :close_encounters_participant_events, :metadata, :text
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,4 @@
1
+ require_relative "middleware"
1
2
  module CloseEncounters
2
3
  class Engine < ::Rails::Engine
3
4
  isolate_namespace CloseEncounters
@@ -6,6 +7,10 @@ module CloseEncounters
6
7
  g.test_framework :minitest, spec: true
7
8
  end
8
9
 
10
+ initializer "close_encounters.middleware" do |app|
11
+ app.middleware.use CloseEncounters::Middleware if CloseEncounters.auto_contact?
12
+ end
13
+
9
14
  if defined?(Importmap)
10
15
  initializer "close_encounters.importmap", before: "importmap" do |app|
11
16
  app.config.importmap.paths << root.join("config/importmap.rb")
@@ -0,0 +1,31 @@
1
+ module CloseEncounters
2
+ class Middleware
3
+ def initialize(app, tracker: CloseEncounters)
4
+ @app = app
5
+ @tracker = tracker
6
+ end
7
+
8
+ def call(env)
9
+ status, headers, response = @app.call(env)
10
+
11
+ record_contact(env["SERVER_NAME"], status, response)
12
+
13
+ [status, headers, response]
14
+ end
15
+
16
+ private
17
+
18
+ def record_contact(host, status, response)
19
+ if (name = participant_services[host])
20
+ @tracker.contact(name, status:, response:)
21
+ end
22
+ end
23
+
24
+ def participant_services
25
+ @participant_services ||= CloseEncounters::ParticipantService.all
26
+ .map do |service|
27
+ [service.connection_info["domain"], service.name]
28
+ end.to_h
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module CloseEncounters
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -19,6 +19,39 @@ module CloseEncounters
19
19
  end
20
20
  end
21
21
 
22
+ # Record a verification of a contact with a third party service where the
23
+ # verification is a callable which must also respond to to_s.
24
+ #
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.
27
+ #
28
+ # @param name [String] the name of the service
29
+ # @param status [Integer] the HTTP status of the contact
30
+ # @param response [String] the response object
31
+ # @param verifier [Proc] the verification callable which must also respond to to_s
32
+ def verify(name, status:, response:, verifier:)
33
+ 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})
36
+ end
37
+ end
38
+
39
+ # Determine if contacts with third party services should be recorded automatically
40
+ # using the Rack Middleware
41
+ #
42
+ # Set the CLOSE_ENCOUNTERS_AUTO_CONTACT environment variable to enable this feature
43
+ # or call CloseEncounters.auto_contact! in an initializer
44
+ #
45
+ # @return [Boolean] whether or not to automatically record contacts
46
+ def auto_contact?
47
+ !!(ENV["CLOSE_ENCOUNTERS_AUTO_CONTACT"] || @auto_contact)
48
+ end
49
+
50
+ # Enable automatic contact recording in the Rack Middleware
51
+ def auto_contact!
52
+ @auto_contact = true
53
+ end
54
+
22
55
  # Get the status of the most recent contact with a third party service
23
56
  #
24
57
  # @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.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-11 00:00:00.000000000 Z
11
+ date: 2025-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -112,13 +112,14 @@ files:
112
112
  - app/models/close_encounters/application_record.rb
113
113
  - app/models/close_encounters/participant_event.rb
114
114
  - app/models/close_encounters/participant_service.rb
115
- - app/views/layouts/close_encounters/application.html.erb
116
115
  - config/importmap.rb
117
116
  - db/migrate/20240430173723_create_close_encounters_participant_services.rb
118
117
  - db/migrate/20240430173725_create_close_encounters_participant_events.rb
119
118
  - db/migrate/20240508190642_add_close_ecounters_indexes.rb
119
+ - db/migrate/20250225232551_add_metadata_to_close_encounters_participant_events.rb
120
120
  - lib/close_encounters.rb
121
121
  - lib/close_encounters/engine.rb
122
+ - lib/close_encounters/middleware.rb
122
123
  - lib/close_encounters/version.rb
123
124
  - lib/tasks/close_encounters.rake
124
125
  homepage: https://github.com/SOFware/close_encounters
@@ -1,15 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Close encounters</title>
5
- <%= csrf_meta_tags %>
6
- <%= csp_meta_tag %>
7
-
8
- <%= stylesheet_link_tag "close_encounters/application", media: "all" %>
9
- </head>
10
- <body>
11
-
12
- <%= yield %>
13
-
14
- </body>
15
- </html>