njtransit 1.3.0 → 1.3.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 +4 -4
- data/CHANGELOG.md +7 -0
- data/CLAUDE.md +2 -2
- data/README.md +1 -1
- data/lib/njtransit/client.rb +77 -36
- data/lib/njtransit/version.rb +1 -1
- data/lib/njtransit.rb +18 -8
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0744ef0708cac894bc5237e89d8c18c96a353d4438b9c643fe8c6c6fee345c67
|
|
4
|
+
data.tar.gz: 0c340f088b80f303a975ee939158bdce1c19e0f9e0fbf7db29fdc176caec8aa0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cef2f399403237043e87f210a0db65fde5ed1851119701a22b3c32dfa06c2d086b70efddaeb2f6c6022febd0e5ed9565aa7939ff91f840f082f8b69aab41166c
|
|
7
|
+
data.tar.gz: b6f400fdab3e3856705ea8fcdac5300a1de21c27ac9ddf949874ac8cff743e54cb9ff0a58a8b335a68638b247f4299092b8a224090fa02600988388f12e02820
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Threads sharing one client no longer mint a token each when NJ Transit rejects the token they all sent. Renewal is single-flight per API (one lock per token key, shared by every client in the process): the first thread mints, the others retry with its token. A rejection deletes the stored token only if it is still the one that was sent, and a token another thread or client already stored is reused without minting. Applies on both the JSON form and raw (GTFS-RT) paths
|
|
10
|
+
- `Client#api_client`, `NJTransit.client` and `NJTransit.rail_client` are memoized under a lock, so threads racing on first use share one instance
|
|
11
|
+
|
|
12
|
+
The token store contract (`read`/`write`/`delete`) is unchanged. Races between separate processes sharing a store are not covered: each process can still mint once per rejection
|
|
13
|
+
|
|
7
14
|
## [1.3.0] - 2026-11-14
|
|
8
15
|
|
|
9
16
|
### Fixed
|
data/CLAUDE.md
CHANGED
|
@@ -56,13 +56,13 @@ SQLite-backed offline schedule data. Import from GTFS zip, then query via `NJTra
|
|
|
56
56
|
- **`enrich` flag**: Bus API methods default to `enrich: true`, which joins GTFS static data (lat/lon, route names). Use `enrich: false` if GTFS isn't imported or you don't need it.
|
|
57
57
|
- **`mode` parameter**: Bus methods default to `mode: "BUS"`. Pass `"HBLR"`, `"NLR"`, `"RL"`, or `"ALL"` for light rail.
|
|
58
58
|
- **`stops_nearby` radius**: The radius parameter is in **feet**, not miles.
|
|
59
|
-
- **Token store**: Auth tokens go through a pluggable `token_store` (`lib/njtransit/token_stores.rb`): any object with `read`/`write`/`delete`, such as `TokenStores::File` or `Rails.cache`. The default is one shared `TokenStores::Memory` per process. Tokens are per API: each GTFS-RT resource (`bus_gtfs`, `bus_gtfs_g2`, `rail_gtfs`) gets its own client via `Client#api_client` with its own auth endpoint, and keys include the auth path. A rejected token (401/403, or a JSON error matching /invalid token/i at any status — NJ Transit uses HTTP 500) is deleted from the store and the request retried once, on both the JSON and raw (GTFS-RT) paths.
|
|
59
|
+
- **Token store**: Auth tokens go through a pluggable `token_store` (`lib/njtransit/token_stores.rb`): any object with `read`/`write`/`delete`, such as `TokenStores::File` or `Rails.cache`. The default is one shared `TokenStores::Memory` per process. Tokens are per API: each GTFS-RT resource (`bus_gtfs`, `bus_gtfs_g2`, `rail_gtfs`) gets its own client via `Client#api_client` with its own auth endpoint, and keys include the auth path. A rejected token (401/403, or a JSON error matching /invalid token/i at any status — NJ Transit uses HTTP 500) is deleted from the store and the request retried once, on both the JSON and raw (GTFS-RT) paths. Renewal (`renew_token!`) is single-flight under a per-token-key `Monitor` shared by every client in the process: the store entry is deleted only if it still holds the rejected token, and a token another thread or client already stored is reused without minting. Cross-process races are not coordinated. `api_client`, `NJTransit.client` and `NJTransit.rail_client` memoize under a lock.
|
|
60
60
|
- **Raw responses**: GTFS-RT methods (`bus_gtfs`, `rail_gtfs`) return binary protobuf data via `post_form_raw`.
|
|
61
61
|
|
|
62
62
|
## Testing
|
|
63
63
|
|
|
64
64
|
```bash
|
|
65
|
-
bundle exec rspec # Run all
|
|
65
|
+
bundle exec rspec # Run all specs
|
|
66
66
|
bundle exec rspec spec/file.rb # Run a specific spec file
|
|
67
67
|
```
|
|
68
68
|
|
data/README.md
CHANGED
|
@@ -77,7 +77,7 @@ NJTransit.configure do |config|
|
|
|
77
77
|
end
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
-
A token store is any object with `read(key)`, `write(key, token)` and `delete(key)`; anything else raises `ArgumentError` when you assign it. Tokens are keyed by API host, auth endpoint and username (`njtransit:token:<base_url><auth_path>:<username>`), so clients and processes sharing a store share one token per API per account. When the API rejects a token (a 401/403, or a JSON error mentioning an invalid token at any status; NJ Transit answers HTTP 500), the gem deletes it from the store (unless another client already replaced it), authenticates, stores the new one and retries the request once. If the store itself fails, the gem warns on stderr and falls back to authenticating, so a broken cache never breaks requests. `TokenStores::File` locks a `<path>.lock` file around each write, so several processes can share one file.
|
|
80
|
+
A token store is any object with `read(key)`, `write(key, token)` and `delete(key)`; anything else raises `ArgumentError` when you assign it. Tokens are keyed by API host, auth endpoint and username (`njtransit:token:<base_url><auth_path>:<username>`), so clients and processes sharing a store share one token per API per account. When the API rejects a token (a 401/403, or a JSON error mentioning an invalid token at any status; NJ Transit answers HTTP 500), the gem deletes it from the store (unless another client already replaced it), authenticates, stores the new one and retries the request once. If the store itself fails, the gem warns on stderr and falls back to authenticating, so a broken cache never breaks requests. Within one process, renewal is single-flight per API: when several threads are rejected at once, one mints a token and the rest retry with it. Separate processes sharing a store are not coordinated, so each may mint once per rejection. `TokenStores::File` locks a `<path>.lock` file around each write, so several processes can share one file.
|
|
81
81
|
|
|
82
82
|
## Two Clients, One Gem
|
|
83
83
|
|
data/lib/njtransit/client.rb
CHANGED
|
@@ -4,6 +4,7 @@ require "faraday"
|
|
|
4
4
|
require "faraday/typhoeus"
|
|
5
5
|
require "faraday/multipart"
|
|
6
6
|
require "json"
|
|
7
|
+
require "monitor"
|
|
7
8
|
|
|
8
9
|
require_relative "resources/base"
|
|
9
10
|
require_relative "resources/bus"
|
|
@@ -20,6 +21,10 @@ module NJTransit
|
|
|
20
21
|
GTFS_G2_AUTH_PATH = "/api/GTFSG2/authenticateUser"
|
|
21
22
|
RAIL_GTFS_AUTH_PATH = "/api/GTFSRT/getToken"
|
|
22
23
|
|
|
24
|
+
TOKEN_LOCKS = {} # rubocop:disable Style/MutableConstant -- guarded by TOKEN_LOCKS_MUTEX
|
|
25
|
+
TOKEN_LOCKS_MUTEX = Mutex.new
|
|
26
|
+
private_constant :TOKEN_LOCKS, :TOKEN_LOCKS_MUTEX
|
|
27
|
+
|
|
23
28
|
attr_reader :username, :password, :log_level, :base_url, :timeout, :auth_path, :token_store
|
|
24
29
|
|
|
25
30
|
def initialize(username:, password:, log_level: "silent", base_url: Configuration::DEFAULT_BASE_URL,
|
|
@@ -32,6 +37,9 @@ module NJTransit
|
|
|
32
37
|
@timeout = timeout
|
|
33
38
|
@auth_path = auth_path
|
|
34
39
|
@token = nil
|
|
40
|
+
@api_clients = {}
|
|
41
|
+
@api_clients_lock = Mutex.new
|
|
42
|
+
@token_lock = self.class.token_lock(token_key)
|
|
35
43
|
end
|
|
36
44
|
|
|
37
45
|
def bus
|
|
@@ -83,39 +91,21 @@ module NJTransit
|
|
|
83
91
|
end
|
|
84
92
|
|
|
85
93
|
def authenticate!
|
|
86
|
-
|
|
87
|
-
if cached
|
|
88
|
-
@token = cached
|
|
89
|
-
return @token
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
response = form_connection.post(auth_path) do |req|
|
|
93
|
-
req.body = { username: username, password: password }
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
result = parse_body(response.body)
|
|
97
|
-
|
|
98
|
-
unless result.is_a?(Hash) && result["Authenticated"] == "True"
|
|
99
|
-
message = result.is_a?(Hash) && result["errorMessage"] ? result["errorMessage"] : "Authentication failed"
|
|
100
|
-
raise AuthenticationError, message
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
@token = result["UserToken"]
|
|
104
|
-
write_stored_token(@token)
|
|
105
|
-
@token
|
|
94
|
+
token_lock.synchronize { authenticate_unlocked! }
|
|
106
95
|
end
|
|
107
96
|
|
|
108
97
|
def token
|
|
109
|
-
|
|
110
|
-
@token
|
|
98
|
+
@token || token_lock.synchronize { @token || authenticate! }
|
|
111
99
|
end
|
|
112
100
|
|
|
113
101
|
# Forget this client's token. The stored copy is deleted only if it is still the
|
|
114
102
|
# token this client held, so a fresh token another client just stored survives.
|
|
115
103
|
def clear_token!
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
104
|
+
token_lock.synchronize do
|
|
105
|
+
stale = @token
|
|
106
|
+
@token = nil
|
|
107
|
+
store_call(:delete, token_key) if stale.nil? || read_stored_token == stale
|
|
108
|
+
end
|
|
119
109
|
end
|
|
120
110
|
|
|
121
111
|
# Store key for this client's token: one per host, auth endpoint (i.e. API) and username.
|
|
@@ -124,21 +114,29 @@ module NJTransit
|
|
|
124
114
|
end
|
|
125
115
|
|
|
126
116
|
# A client for another API on this host (its own auth endpoint and token), sharing
|
|
127
|
-
# this client's credentials, timeout, logging and token store.
|
|
117
|
+
# this client's credentials, timeout, logging and token store. Memoized: every
|
|
118
|
+
# caller (and thread) gets the same instance per auth path.
|
|
128
119
|
def api_client(api_auth_path)
|
|
129
120
|
return self if api_auth_path == auth_path
|
|
130
121
|
|
|
131
|
-
@
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
122
|
+
@api_clients_lock.synchronize do
|
|
123
|
+
@api_clients[api_auth_path] ||= self.class.new(
|
|
124
|
+
username: username, password: password, log_level: log_level, base_url: base_url,
|
|
125
|
+
timeout: timeout, auth_path: api_auth_path, token_store: token_store
|
|
126
|
+
)
|
|
127
|
+
end
|
|
136
128
|
end
|
|
137
129
|
|
|
138
130
|
def self.default_token_store
|
|
139
131
|
@default_token_store ||= TokenStores::Memory.new
|
|
140
132
|
end
|
|
141
133
|
|
|
134
|
+
# One reentrant lock per token key, shared by every client in this process, so reading,
|
|
135
|
+
# minting and replacing a token for one API is single-flight across threads.
|
|
136
|
+
def self.token_lock(key)
|
|
137
|
+
TOKEN_LOCKS_MUTEX.synchronize { TOKEN_LOCKS[key] ||= Monitor.new }
|
|
138
|
+
end
|
|
139
|
+
|
|
142
140
|
# Deprecated: use default_token_store, or configure a token_store.
|
|
143
141
|
def self.token_cache
|
|
144
142
|
warn "[njtransit] Client.token_cache is deprecated; use Client.default_token_store " \
|
|
@@ -152,6 +150,51 @@ module NJTransit
|
|
|
152
150
|
|
|
153
151
|
private
|
|
154
152
|
|
|
153
|
+
attr_reader :token_lock
|
|
154
|
+
|
|
155
|
+
# Callers hold token_lock.
|
|
156
|
+
def authenticate_unlocked!
|
|
157
|
+
cached = read_stored_token
|
|
158
|
+
if cached
|
|
159
|
+
@token = cached
|
|
160
|
+
return @token
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
response = form_connection.post(auth_path) do |req|
|
|
164
|
+
req.body = { username: username, password: password }
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
result = parse_body(response.body)
|
|
168
|
+
|
|
169
|
+
unless result.is_a?(Hash) && result["Authenticated"] == "True"
|
|
170
|
+
message = result.is_a?(Hash) && result["errorMessage"] ? result["errorMessage"] : "Authentication failed"
|
|
171
|
+
raise AuthenticationError, message
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
@token = result["UserToken"]
|
|
175
|
+
write_stored_token(@token)
|
|
176
|
+
@token
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# The token `rejected` was refused: return the token to retry with. Under the token
|
|
180
|
+
# lock, so concurrent rejections mint once. If this client or the store already holds
|
|
181
|
+
# a different token (another thread or client replaced it), use that without minting;
|
|
182
|
+
# otherwise delete the stored copy only if it is still the rejected one, and mint.
|
|
183
|
+
def renew_token!(rejected)
|
|
184
|
+
token_lock.synchronize do
|
|
185
|
+
return @token if @token && @token != rejected
|
|
186
|
+
|
|
187
|
+
@token = nil
|
|
188
|
+
stored = read_stored_token
|
|
189
|
+
if stored && stored != rejected
|
|
190
|
+
@token = stored
|
|
191
|
+
else
|
|
192
|
+
store_call(:delete, token_key) if stored
|
|
193
|
+
authenticate_unlocked!
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
155
198
|
def request(method, path, params_or_body = {})
|
|
156
199
|
response = json_connection.public_send(method) do |req|
|
|
157
200
|
req.url(path)
|
|
@@ -176,9 +219,7 @@ module NJTransit
|
|
|
176
219
|
end
|
|
177
220
|
|
|
178
221
|
if retry_auth && auth_failure?(response)
|
|
179
|
-
|
|
180
|
-
authenticate!
|
|
181
|
-
params[:token] = @token
|
|
222
|
+
params[:token] = renew_token!(params[:token])
|
|
182
223
|
return request_form(method, path, params, retry_auth: false)
|
|
183
224
|
end
|
|
184
225
|
|
|
@@ -192,14 +233,14 @@ module NJTransit
|
|
|
192
233
|
end
|
|
193
234
|
|
|
194
235
|
def request_form_raw(method, path, params = {}, retry_auth: true)
|
|
195
|
-
params[:token] = token
|
|
236
|
+
params[:token] = token if retry_auth
|
|
196
237
|
response = raw_connection.public_send(method) do |req|
|
|
197
238
|
req.url(path)
|
|
198
239
|
req.body = params
|
|
199
240
|
end
|
|
200
241
|
|
|
201
242
|
if retry_auth && auth_failure?(response)
|
|
202
|
-
|
|
243
|
+
params[:token] = renew_token!(params[:token])
|
|
203
244
|
return request_form_raw(method, path, params, retry_auth: false)
|
|
204
245
|
end
|
|
205
246
|
|
data/lib/njtransit/version.rb
CHANGED
data/lib/njtransit.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "monitor"
|
|
4
|
+
|
|
3
5
|
require_relative "njtransit/version"
|
|
4
6
|
require_relative "njtransit/configuration"
|
|
5
7
|
require_relative "njtransit/error"
|
|
@@ -8,6 +10,9 @@ require_relative "njtransit/client"
|
|
|
8
10
|
require_relative "njtransit/gtfs"
|
|
9
11
|
|
|
10
12
|
module NJTransit
|
|
13
|
+
CLIENTS_LOCK = Monitor.new
|
|
14
|
+
private_constant :CLIENTS_LOCK
|
|
15
|
+
|
|
11
16
|
class << self
|
|
12
17
|
def configure
|
|
13
18
|
yield(configuration)
|
|
@@ -18,22 +23,27 @@ module NJTransit
|
|
|
18
23
|
end
|
|
19
24
|
|
|
20
25
|
# Bus API client (pcsdata.njtransit.com)
|
|
26
|
+
# Memoized under a lock, so threads share one client (and its per-API clients).
|
|
21
27
|
def client
|
|
22
|
-
@client ||= Client.new(**configuration.to_h)
|
|
28
|
+
@client || CLIENTS_LOCK.synchronize { @client ||= Client.new(**configuration.to_h) }
|
|
23
29
|
end
|
|
24
30
|
|
|
25
31
|
# Rail API client (raildata.njtransit.com)
|
|
26
32
|
def rail_client
|
|
27
|
-
@rail_client
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
@rail_client || CLIENTS_LOCK.synchronize do
|
|
34
|
+
@rail_client ||= Client.new(
|
|
35
|
+
**configuration.to_h, base_url: Configuration::DEFAULT_RAIL_BASE_URL,
|
|
36
|
+
auth_path: "/api/TrainData/getToken"
|
|
37
|
+
)
|
|
38
|
+
end
|
|
31
39
|
end
|
|
32
40
|
|
|
33
41
|
def reset!
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
CLIENTS_LOCK.synchronize do
|
|
43
|
+
@configuration = nil
|
|
44
|
+
@client = nil
|
|
45
|
+
@rail_client = nil
|
|
46
|
+
end
|
|
37
47
|
end
|
|
38
48
|
end
|
|
39
49
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: njtransit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.3.
|
|
4
|
+
version: 1.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jay Ravaliya
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: csv
|