onebusaway-sdk 1.0.6 → 1.0.8

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: 8196e7bca2f7d4276e4ccde0af2bb59545708e628e62003c5c5ffaab2e52f442
4
- data.tar.gz: f1c24942b576db3ffc526de4a655f52d7b3190e0f578a06db3db6287de8e0264
3
+ metadata.gz: cd6c45e5984494973169c3532e5020a97abe54feb6c2d526f547a4a63557b9b1
4
+ data.tar.gz: 592b70cb18d02e8301fa93874d37b27e591c073e00196ed4617d60c04cd2ab6a
5
5
  SHA512:
6
- metadata.gz: ee7e8b77eba1427bf23a76dd5e1921740d160ba95b273e7a02c029ed60ad711858dfc4080dadebd7c260cd1096570905b207f341a9b11a66f683cda1635d6d29
7
- data.tar.gz: 5f94bce706290ef1f15d959a8b054879564d3e624f5b5e7cde3cfe2966ba9c4d29bef7878a0987025c67f66595591cbe6510cfd253038bc1f419cf7f801ed7a9
6
+ metadata.gz: 0f86660ab1a97f514e151d982b8b6134d4edb583e95a6c88216f5274f5c4c769d560683b45b1c56bd4ef8fbbf558b793fecf0ff4534db843fd5bb75fd07d09f8
7
+ data.tar.gz: 86de9af710d48f585761fe2d7c886adbc5dc29b1839384174f3127e9f307f9a9f232b34e76fb125cc4be00af80482a5373a78bf21c5f02b1faa68b9ad6124f1c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.8 (2025-06-18)
4
+
5
+ Full Changelog: [v1.0.7...v1.0.8](https://github.com/OneBusAway/ruby-sdk/compare/v1.0.7...v1.0.8)
6
+
7
+ ### Bug Fixes
8
+
9
+ * issue where we cannot mutate arrays on base model derivatives ([55a5d96](https://github.com/OneBusAway/ruby-sdk/commit/55a5d9660d81a48dc9cdc1ea3e7c46a6c6471d95))
10
+
11
+
12
+ ### Chores
13
+
14
+ * **ci:** enable for pull requests ([91ac3d1](https://github.com/OneBusAway/ruby-sdk/commit/91ac3d152af0a13e43890803903fe9f02af838f5))
15
+ * **internal:** version bump ([64cc3cb](https://github.com/OneBusAway/ruby-sdk/commit/64cc3cb430a2dbacf7ba7e2463281d84100103e8))
16
+
17
+ ## 1.0.7 (2025-06-12)
18
+
19
+ Full Changelog: [v1.0.6...v1.0.7](https://github.com/OneBusAway/ruby-sdk/compare/v1.0.6...v1.0.7)
20
+
21
+ ### Chores
22
+
23
+ * **ci:** link to correct github repo ([1130a85](https://github.com/OneBusAway/ruby-sdk/commit/1130a854cb3c9d371432fd6693aa07159064783b))
24
+ * **internal:** version bump ([7bc49b1](https://github.com/OneBusAway/ruby-sdk/commit/7bc49b108f0850199278adaa6c155a9080b757e5))
25
+
3
26
  ## 1.0.6 (2025-06-06)
4
27
 
5
28
  Full Changelog: [v1.0.5...v1.0.6](https://github.com/OneBusAway/ruby-sdk/compare/v1.0.5...v1.0.6)
data/README.md CHANGED
@@ -17,7 +17,7 @@ To use this gem, install via Bundler by adding the following to your application
17
17
  <!-- x-release-please-start-version -->
18
18
 
19
19
  ```ruby
20
- gem "onebusaway-sdk", "~> 1.0.6"
20
+ gem "onebusaway-sdk", "~> 1.0.8"
21
21
  ```
22
22
 
23
23
  <!-- x-release-please-end -->
@@ -9,6 +9,28 @@ module OnebusawaySDK
9
9
  end
10
10
 
11
11
  class ConversionError < OnebusawaySDK::Errors::Error
12
+ # @return [StandardError, nil]
13
+ def cause = @cause.nil? ? super : @cause
14
+
15
+ # @api private
16
+ #
17
+ # @param on [Class<StandardError>]
18
+ # @param method [Symbol]
19
+ # @param target [Object]
20
+ # @param value [Object]
21
+ # @param cause [StandardError, nil]
22
+ def initialize(on:, method:, target:, value:, cause: nil)
23
+ cls = on.name.split("::").last
24
+
25
+ message = [
26
+ "Failed to parse #{cls}.#{method} from #{value.class} to #{target.inspect}.",
27
+ "To get the unparsed API response, use #{cls}[#{method.inspect}].",
28
+ cause && "Cause: #{cause.message}"
29
+ ].filter(&:itself).join(" ")
30
+
31
+ @cause = cause
32
+ super(message)
33
+ end
12
34
  end
13
35
 
14
36
  class APIError < OnebusawaySDK::Errors::Error
@@ -62,10 +62,14 @@ module OnebusawaySDK
62
62
  #
63
63
  # @param state [Hash{Symbol=>Object}] .
64
64
  #
65
- # @option state [Boolean, :strong] :strictness
65
+ # @option state [Boolean] :translate_names
66
+ #
67
+ # @option state [Boolean] :strictness
66
68
  #
67
69
  # @option state [Hash{Symbol=>Object}] :exactness
68
70
  #
71
+ # @option state [Class<StandardError>] :error
72
+ #
69
73
  # @option state [Integer] :branched
70
74
  #
71
75
  # @return [Array<Object>, Object]
@@ -74,6 +78,7 @@ module OnebusawaySDK
74
78
 
75
79
  unless value.is_a?(Array)
76
80
  exactness[:no] += 1
81
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Array}")
77
82
  return value
78
83
  end
79
84
 
@@ -60,7 +60,7 @@ module OnebusawaySDK
60
60
  [OnebusawaySDK::Internal::Type::Converter.type_info(type_info), type_info]
61
61
  end
62
62
 
63
- setter = "#{name_sym}="
63
+ setter = :"#{name_sym}="
64
64
  api_name = info.fetch(:api_name, name_sym)
65
65
  nilable = info.fetch(:nil?, false)
66
66
  const = if required && !nilable
@@ -84,28 +84,61 @@ module OnebusawaySDK
84
84
  type_fn: type_fn
85
85
  }
86
86
 
87
- define_method(setter) { @data.store(name_sym, _1) }
87
+ define_method(setter) do |value|
88
+ target = type_fn.call
89
+ state = OnebusawaySDK::Internal::Type::Converter.new_coerce_state(translate_names: false)
90
+ coerced = OnebusawaySDK::Internal::Type::Converter.coerce(target, value, state: state)
91
+ status = @coerced.store(name_sym, state.fetch(:error) || true)
92
+ stored =
93
+ case [target, status]
94
+ in [OnebusawaySDK::Internal::Type::Converter | Symbol, true]
95
+ coerced
96
+ else
97
+ value
98
+ end
99
+ @data.store(name_sym, stored)
100
+ end
88
101
 
102
+ # rubocop:disable Style/CaseEquality
103
+ # rubocop:disable Metrics/BlockLength
89
104
  define_method(name_sym) do
90
105
  target = type_fn.call
91
- value = @data.fetch(name_sym) { const == OnebusawaySDK::Internal::OMIT ? nil : const }
92
- state = {strictness: :strong, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
93
- if (nilable || !required) && value.nil?
94
- nil
95
- else
96
- OnebusawaySDK::Internal::Type::Converter.coerce(
97
- target, value, state: state
106
+
107
+ case @coerced[name_sym]
108
+ in true | false if OnebusawaySDK::Internal::Type::Converter === target
109
+ @data.fetch(name_sym)
110
+ in ::StandardError => e
111
+ raise OnebusawaySDK::Errors::ConversionError.new(
112
+ on: self.class,
113
+ method: __method__,
114
+ target: target,
115
+ value: @data.fetch(name_sym),
116
+ cause: e
98
117
  )
118
+ else
119
+ Kernel.then do
120
+ value = @data.fetch(name_sym) { const == OnebusawaySDK::Internal::OMIT ? nil : const }
121
+ state = OnebusawaySDK::Internal::Type::Converter.new_coerce_state(translate_names: false)
122
+ if (nilable || !required) && value.nil?
123
+ nil
124
+ else
125
+ OnebusawaySDK::Internal::Type::Converter.coerce(
126
+ target, value, state: state
127
+ )
128
+ end
129
+ rescue StandardError => e
130
+ raise OnebusawaySDK::Errors::ConversionError.new(
131
+ on: self.class,
132
+ method: __method__,
133
+ target: target,
134
+ value: value,
135
+ cause: e
136
+ )
137
+ end
99
138
  end
100
- rescue StandardError => e
101
- cls = self.class.name.split("::").last
102
- message = [
103
- "Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}.",
104
- "To get the unparsed API response, use #{cls}[#{__method__.inspect}].",
105
- "Cause: #{e.message}"
106
- ].join(" ")
107
- raise OnebusawaySDK::Errors::ConversionError.new(message)
108
139
  end
140
+ # rubocop:enable Metrics/BlockLength
141
+ # rubocop:enable Style/CaseEquality
109
142
  end
110
143
 
111
144
  # @api private
@@ -205,23 +238,28 @@ module OnebusawaySDK
205
238
  #
206
239
  # @param state [Hash{Symbol=>Object}] .
207
240
  #
208
- # @option state [Boolean, :strong] :strictness
241
+ # @option state [Boolean] :translate_names
242
+ #
243
+ # @option state [Boolean] :strictness
209
244
  #
210
245
  # @option state [Hash{Symbol=>Object}] :exactness
211
246
  #
247
+ # @option state [Class<StandardError>] :error
248
+ #
212
249
  # @option state [Integer] :branched
213
250
  #
214
251
  # @return [self, Object]
215
252
  def coerce(value, state:)
216
253
  exactness = state.fetch(:exactness)
217
254
 
218
- if value.is_a?(self.class)
255
+ if value.is_a?(self)
219
256
  exactness[:yes] += 1
220
257
  return value
221
258
  end
222
259
 
223
260
  unless (val = OnebusawaySDK::Internal::Util.coerce_hash(value)).is_a?(Hash)
224
261
  exactness[:no] += 1
262
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
225
263
  return value
226
264
  end
227
265
  exactness[:yes] += 1
@@ -229,13 +267,15 @@ module OnebusawaySDK
229
267
  keys = val.keys.to_set
230
268
  instance = new
231
269
  data = instance.to_h
270
+ status = instance.instance_variable_get(:@coerced)
232
271
 
233
272
  # rubocop:disable Metrics/BlockLength
234
273
  fields.each do |name, field|
235
274
  mode, required, target = field.fetch_values(:mode, :required, :type)
236
275
  api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
276
+ src_name = state.fetch(:translate_names) ? api_name : name
237
277
 
238
- unless val.key?(api_name)
278
+ unless val.key?(src_name)
239
279
  if required && mode != :dump && const == OnebusawaySDK::Internal::OMIT
240
280
  exactness[nilable ? :maybe : :no] += 1
241
281
  else
@@ -244,9 +284,10 @@ module OnebusawaySDK
244
284
  next
245
285
  end
246
286
 
247
- item = val.fetch(api_name)
248
- keys.delete(api_name)
287
+ item = val.fetch(src_name)
288
+ keys.delete(src_name)
249
289
 
290
+ state[:error] = nil
250
291
  converted =
251
292
  if item.nil? && (nilable || !required)
252
293
  exactness[nilable ? :yes : :maybe] += 1
@@ -260,6 +301,8 @@ module OnebusawaySDK
260
301
  item
261
302
  end
262
303
  end
304
+
305
+ status.store(name, state.fetch(:error) || true)
263
306
  data.store(name, converted)
264
307
  end
265
308
  # rubocop:enable Metrics/BlockLength
@@ -438,7 +481,18 @@ module OnebusawaySDK
438
481
  # Create a new instance of a model.
439
482
  #
440
483
  # @param data [Hash{Symbol=>Object}, self]
441
- def initialize(data = {}) = (@data = OnebusawaySDK::Internal::Util.coerce_hash!(data).to_h)
484
+ def initialize(data = {})
485
+ @data = {}
486
+ @coerced = {}
487
+ OnebusawaySDK::Internal::Util.coerce_hash!(data).each do
488
+ if self.class.known_fields.key?(_1)
489
+ public_send(:"#{_1}=", _2)
490
+ else
491
+ @data.store(_1, _2)
492
+ @coerced.store(_1, false)
493
+ end
494
+ end
495
+ end
442
496
 
443
497
  class << self
444
498
  # @api private
@@ -31,14 +31,20 @@ module OnebusawaySDK
31
31
  class << self
32
32
  # @api private
33
33
  #
34
+ # Coerce value to Boolean if possible, otherwise return the original value.
35
+ #
34
36
  # @param value [Boolean, Object]
35
37
  #
36
38
  # @param state [Hash{Symbol=>Object}] .
37
39
  #
38
- # @option state [Boolean, :strong] :strictness
40
+ # @option state [Boolean] :translate_names
41
+ #
42
+ # @option state [Boolean] :strictness
39
43
  #
40
44
  # @option state [Hash{Symbol=>Object}] :exactness
41
45
  #
46
+ # @option state [Class<StandardError>] :error
47
+ #
42
48
  # @option state [Integer] :branched
43
49
  #
44
50
  # @return [Boolean, Object]
@@ -15,10 +15,14 @@ module OnebusawaySDK
15
15
  #
16
16
  # @param state [Hash{Symbol=>Object}] .
17
17
  #
18
- # @option state [Boolean, :strong] :strictness
18
+ # @option state [Boolean] :translate_names
19
+ #
20
+ # @option state [Boolean] :strictness
19
21
  #
20
22
  # @option state [Hash{Symbol=>Object}] :exactness
21
23
  #
24
+ # @option state [Class<StandardError>] :error
25
+ #
22
26
  # @option state [Integer] :branched
23
27
  #
24
28
  # @return [Object]
@@ -94,6 +98,21 @@ module OnebusawaySDK
94
98
  end
95
99
  end
96
100
 
101
+ # @api private
102
+ #
103
+ # @param translate_names [Boolean]
104
+ #
105
+ # @return [Hash{Symbol=>Object}]
106
+ def new_coerce_state(translate_names: true)
107
+ {
108
+ translate_names: translate_names,
109
+ strictness: true,
110
+ exactness: {yes: 0, no: 0, maybe: 0},
111
+ error: nil,
112
+ branched: 0
113
+ }
114
+ end
115
+
97
116
  # @api private
98
117
  #
99
118
  # Based on `target`, transform `value` into `target`, to the extent possible:
@@ -110,14 +129,11 @@ module OnebusawaySDK
110
129
  #
111
130
  # @param value [Object]
112
131
  #
113
- # @param state [Hash{Symbol=>Object}] The `strictness` is one of `true`, `false`, or `:strong`. This informs the
114
- # coercion strategy when we have to decide between multiple possible conversion
115
- # targets:
132
+ # @param state [Hash{Symbol=>Object}] The `strictness` is one of `true`, `false`. This informs the coercion strategy
133
+ # when we have to decide between multiple possible conversion targets:
116
134
  #
117
135
  # - `true`: the conversion must be exact, with minimum coercion.
118
136
  # - `false`: the conversion can be approximate, with some coercion.
119
- # - `:strong`: the conversion must be exact, with no coercion, and raise an error
120
- # if not possible.
121
137
  #
122
138
  # The `exactness` is `Hash` with keys being one of `yes`, `no`, or `maybe`. For
123
139
  # any given conversion attempt, the exactness will be updated based on how closely
@@ -130,21 +146,20 @@ module OnebusawaySDK
130
146
  #
131
147
  # See implementation below for more details.
132
148
  #
133
- # @option state [Boolean, :strong] :strictness
149
+ # @option state [Boolean] :translate_names
150
+ #
151
+ # @option state [Boolean] :strictness
134
152
  #
135
153
  # @option state [Hash{Symbol=>Object}] :exactness
136
154
  #
155
+ # @option state [Class<StandardError>] :error
156
+ #
137
157
  # @option state [Integer] :branched
138
158
  #
139
159
  # @return [Object]
140
- def coerce(
141
- target,
142
- value,
143
- state: {strictness: true, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
144
- )
145
- # rubocop:disable Lint/SuppressedException
160
+ def coerce(target, value, state: OnebusawaySDK::Internal::Type::Converter.new_coerce_state)
146
161
  # rubocop:disable Metrics/BlockNesting
147
- strictness, exactness = state.fetch_values(:strictness, :exactness)
162
+ exactness = state.fetch(:exactness)
148
163
 
149
164
  case target
150
165
  in OnebusawaySDK::Internal::Type::Converter
@@ -160,29 +175,26 @@ module OnebusawaySDK
160
175
  exactness[value.nil? ? :yes : :maybe] += 1
161
176
  return nil
162
177
  in -> { _1 <= Integer }
163
- if value.is_a?(Integer)
178
+ case value
179
+ in Integer
164
180
  exactness[:yes] += 1
165
181
  return value
166
- elsif strictness == :strong && Integer(value, exception: false) != value
167
- message = "no implicit conversion of #{value.class} into #{target.inspect}"
168
- raise value.is_a?(Numeric) ? ArgumentError.new(message) : TypeError.new(message)
169
182
  else
170
183
  Kernel.then do
171
184
  return Integer(value).tap { exactness[:maybe] += 1 }
172
- rescue ArgumentError, TypeError
185
+ rescue ArgumentError, TypeError => e
186
+ state[:error] = e
173
187
  end
174
188
  end
175
189
  in -> { _1 <= Float }
176
190
  if value.is_a?(Numeric)
177
191
  exactness[:yes] += 1
178
192
  return Float(value)
179
- elsif strictness == :strong
180
- message = "no implicit conversion of #{value.class} into #{target.inspect}"
181
- raise TypeError.new(message)
182
193
  else
183
194
  Kernel.then do
184
195
  return Float(value).tap { exactness[:maybe] += 1 }
185
- rescue ArgumentError, TypeError
196
+ rescue ArgumentError, TypeError => e
197
+ state[:error] = e
186
198
  end
187
199
  end
188
200
  in -> { _1 <= String }
@@ -194,16 +206,13 @@ module OnebusawaySDK
194
206
  exactness[:yes] += 1
195
207
  return value.string
196
208
  else
197
- if strictness == :strong
198
- message = "no implicit conversion of #{value.class} into #{target.inspect}"
199
- raise TypeError.new(message)
200
- end
209
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{String}")
201
210
  end
202
211
  in -> { _1 <= Date || _1 <= Time }
203
212
  Kernel.then do
204
213
  return target.parse(value).tap { exactness[:yes] += 1 }
205
214
  rescue ArgumentError, TypeError => e
206
- raise e if strictness == :strong
215
+ state[:error] = e
207
216
  end
208
217
  in -> { _1 <= StringIO } if value.is_a?(String)
209
218
  exactness[:yes] += 1
@@ -221,10 +230,8 @@ module OnebusawaySDK
221
230
  return value
222
231
  end
223
232
  else
224
- if strictness == :strong
225
- message = "cannot convert non-matching #{value.class} into #{target.inspect}"
226
- raise ArgumentError.new(message)
227
- end
233
+ message = "cannot convert non-matching #{value.class} into #{target.inspect}"
234
+ state[:error] = ArgumentError.new(message)
228
235
  end
229
236
  else
230
237
  end
@@ -232,7 +239,6 @@ module OnebusawaySDK
232
239
  exactness[:no] += 1
233
240
  value
234
241
  # rubocop:enable Metrics/BlockNesting
235
- # rubocop:enable Lint/SuppressedException
236
242
  end
237
243
 
238
244
  # @api private
@@ -277,8 +283,10 @@ module OnebusawaySDK
277
283
  define_sorbet_constant!(:CoerceState) do
278
284
  T.type_alias do
279
285
  {
280
- strictness: T.any(T::Boolean, Symbol),
286
+ translate_names: T::Boolean,
287
+ strictness: T::Boolean,
281
288
  exactness: {yes: Integer, no: Integer, maybe: Integer},
289
+ error: T::Class[StandardError],
282
290
  branched: Integer
283
291
  }
284
292
  end
@@ -56,10 +56,14 @@ module OnebusawaySDK
56
56
  #
57
57
  # @param state [Hash{Symbol=>Object}] .
58
58
  #
59
- # @option state [Boolean, :strong] :strictness
59
+ # @option state [Boolean] :translate_names
60
+ #
61
+ # @option state [Boolean] :strictness
60
62
  #
61
63
  # @option state [Hash{Symbol=>Object}] :exactness
62
64
  #
65
+ # @option state [Class<StandardError>] :error
66
+ #
63
67
  # @option state [Integer] :branched
64
68
  #
65
69
  # @return [Symbol, Object]
@@ -70,8 +74,12 @@ module OnebusawaySDK
70
74
  if values.include?(val)
71
75
  exactness[:yes] += 1
72
76
  val
77
+ elsif values.first&.class == val.class
78
+ exactness[:maybe] += 1
79
+ value
73
80
  else
74
- exactness[values.first&.class == val.class ? :maybe : :no] += 1
81
+ exactness[:no] += 1
82
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{self}")
75
83
  value
76
84
  end
77
85
  end
@@ -45,10 +45,14 @@ module OnebusawaySDK
45
45
  #
46
46
  # @param state [Hash{Symbol=>Object}] .
47
47
  #
48
- # @option state [Boolean, :strong] :strictness
48
+ # @option state [Boolean] :translate_names
49
+ #
50
+ # @option state [Boolean] :strictness
49
51
  #
50
52
  # @option state [Hash{Symbol=>Object}] :exactness
51
53
  #
54
+ # @option state [Class<StandardError>] :error
55
+ #
52
56
  # @option state [Integer] :branched
53
57
  #
54
58
  # @return [StringIO, Object]
@@ -62,6 +66,7 @@ module OnebusawaySDK
62
66
  exactness[:yes] += 1
63
67
  value
64
68
  else
69
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{StringIO}")
65
70
  exactness[:no] += 1
66
71
  value
67
72
  end
@@ -77,10 +77,14 @@ module OnebusawaySDK
77
77
  #
78
78
  # @param state [Hash{Symbol=>Object}] .
79
79
  #
80
- # @option state [Boolean, :strong] :strictness
80
+ # @option state [Boolean] :translate_names
81
+ #
82
+ # @option state [Boolean] :strictness
81
83
  #
82
84
  # @option state [Hash{Symbol=>Object}] :exactness
83
85
  #
86
+ # @option state [Class<StandardError>] :error
87
+ #
84
88
  # @option state [Integer] :branched
85
89
  #
86
90
  # @return [Hash{Symbol=>Object}, Object]
@@ -89,6 +93,7 @@ module OnebusawaySDK
89
93
 
90
94
  unless value.is_a?(Hash)
91
95
  exactness[:no] += 1
96
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
92
97
  return value
93
98
  end
94
99
 
@@ -115,14 +115,23 @@ module OnebusawaySDK
115
115
 
116
116
  # @api private
117
117
  #
118
+ # Tries to efficiently coerce the given value to one of the known variants.
119
+ #
120
+ # If the value cannot match any of the known variants, the coercion is considered
121
+ # non-viable and returns the original value.
122
+ #
118
123
  # @param value [Object]
119
124
  #
120
125
  # @param state [Hash{Symbol=>Object}] .
121
126
  #
122
- # @option state [Boolean, :strong] :strictness
127
+ # @option state [Boolean] :translate_names
128
+ #
129
+ # @option state [Boolean] :strictness
123
130
  #
124
131
  # @option state [Hash{Symbol=>Object}] :exactness
125
132
  #
133
+ # @option state [Class<StandardError>] :error
134
+ #
126
135
  # @option state [Integer] :branched
127
136
  #
128
137
  # @return [Object]
@@ -133,7 +142,6 @@ module OnebusawaySDK
133
142
 
134
143
  strictness = state.fetch(:strictness)
135
144
  exactness = state.fetch(:exactness)
136
- state[:strictness] = strictness == :strong ? true : strictness
137
145
 
138
146
  alternatives = []
139
147
  known_variants.each do |_, variant_fn|
@@ -152,13 +160,10 @@ module OnebusawaySDK
152
160
  end
153
161
  end
154
162
 
155
- case alternatives.sort_by(&:first)
163
+ case alternatives.sort_by!(&:first)
156
164
  in []
157
165
  exactness[:no] += 1
158
- if strictness == :strong
159
- message = "no possible conversion of #{value.class} into a variant of #{target.inspect}"
160
- raise ArgumentError.new(message)
161
- end
166
+ state[:error] = ArgumentError.new("no matching variant for #{value.inspect}")
162
167
  value
163
168
  in [[_, exact, coerced], *]
164
169
  exact.each { exactness[_1] += _2 }
@@ -33,14 +33,20 @@ module OnebusawaySDK
33
33
  class << self
34
34
  # @api private
35
35
  #
36
+ # No coercion needed for Unknown type.
37
+ #
36
38
  # @param value [Object]
37
39
  #
38
40
  # @param state [Hash{Symbol=>Object}] .
39
41
  #
40
- # @option state [Boolean, :strong] :strictness
42
+ # @option state [Boolean] :translate_names
43
+ #
44
+ # @option state [Boolean] :strictness
41
45
  #
42
46
  # @option state [Hash{Symbol=>Object}] :exactness
43
47
  #
48
+ # @option state [Class<StandardError>] :error
49
+ #
44
50
  # @option state [Integer] :branched
45
51
  #
46
52
  # @return [Object]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OnebusawaySDK
4
- VERSION = "1.0.6"
4
+ VERSION = "1.0.8"
5
5
  end
@@ -8,6 +8,22 @@ module OnebusawaySDK
8
8
  end
9
9
 
10
10
  class ConversionError < OnebusawaySDK::Errors::Error
11
+ sig { returns(T.nilable(StandardError)) }
12
+ def cause
13
+ end
14
+
15
+ # @api private
16
+ sig do
17
+ params(
18
+ on: T::Class[StandardError],
19
+ method: Symbol,
20
+ target: T.anything,
21
+ value: T.anything,
22
+ cause: T.nilable(StandardError)
23
+ ).returns(T.attached_class)
24
+ end
25
+ def self.new(on:, method:, target:, value:, cause: nil)
26
+ end
11
27
  end
12
28
 
13
29
  class APIError < OnebusawaySDK::Errors::Error
@@ -22,6 +22,8 @@ module OnebusawaySDK
22
22
 
23
23
  class << self
24
24
  # @api private
25
+ #
26
+ # Coerce value to Boolean if possible, otherwise return the original value.
25
27
  sig do
26
28
  override
27
29
  .params(
@@ -18,12 +18,14 @@ module OnebusawaySDK
18
18
  CoerceState =
19
19
  T.type_alias do
20
20
  {
21
- strictness: T.any(T::Boolean, Symbol),
21
+ translate_names: T::Boolean,
22
+ strictness: T::Boolean,
22
23
  exactness: {
23
24
  yes: Integer,
24
25
  no: Integer,
25
26
  maybe: Integer
26
27
  },
28
+ error: T::Class[StandardError],
27
29
  branched: Integer
28
30
  }
29
31
  end
@@ -93,6 +95,15 @@ module OnebusawaySDK
93
95
  def self.type_info(spec)
94
96
  end
95
97
 
98
+ # @api private
99
+ sig do
100
+ params(translate_names: T::Boolean).returns(
101
+ OnebusawaySDK::Internal::Type::Converter::CoerceState
102
+ )
103
+ end
104
+ def self.new_coerce_state(translate_names: true)
105
+ end
106
+
96
107
  # @api private
97
108
  #
98
109
  # Based on `target`, transform `value` into `target`, to the extent possible:
@@ -114,14 +125,11 @@ module OnebusawaySDK
114
125
  def self.coerce(
115
126
  target,
116
127
  value,
117
- # The `strictness` is one of `true`, `false`, or `:strong`. This informs the
118
- # coercion strategy when we have to decide between multiple possible conversion
119
- # targets:
128
+ # The `strictness` is one of `true`, `false`. This informs the coercion strategy
129
+ # when we have to decide between multiple possible conversion targets:
120
130
  #
121
131
  # - `true`: the conversion must be exact, with minimum coercion.
122
132
  # - `false`: the conversion can be approximate, with some coercion.
123
- # - `:strong`: the conversion must be exact, with no coercion, and raise an error
124
- # if not possible.
125
133
  #
126
134
  # The `exactness` is `Hash` with keys being one of `yes`, `no`, or `maybe`. For
127
135
  # any given conversion attempt, the exactness will be updated based on how closely
@@ -133,15 +141,7 @@ module OnebusawaySDK
133
141
  # - `no`: the value cannot be converted to the target type.
134
142
  #
135
143
  # See implementation below for more details.
136
- state: {
137
- strictness: true,
138
- exactness: {
139
- yes: 0,
140
- no: 0,
141
- maybe: 0
142
- },
143
- branched: 0
144
- }
144
+ state: OnebusawaySDK::Internal::Type::Converter.new_coerce_state
145
145
  )
146
146
  end
147
147
 
@@ -78,6 +78,11 @@ module OnebusawaySDK
78
78
  end
79
79
 
80
80
  # @api private
81
+ #
82
+ # Tries to efficiently coerce the given value to one of the known variants.
83
+ #
84
+ # If the value cannot match any of the known variants, the coercion is considered
85
+ # non-viable and returns the original value.
81
86
  sig do
82
87
  override
83
88
  .params(
@@ -22,6 +22,8 @@ module OnebusawaySDK
22
22
 
23
23
  class << self
24
24
  # @api private
25
+ #
26
+ # No coercion needed for Unknown type.
25
27
  sig do
26
28
  override
27
29
  .params(
@@ -5,6 +5,15 @@ module OnebusawaySDK
5
5
  end
6
6
 
7
7
  class ConversionError < OnebusawaySDK::Errors::Error
8
+ def cause: -> StandardError?
9
+
10
+ def initialize: (
11
+ on: Class,
12
+ method: Symbol,
13
+ target: top,
14
+ value: top,
15
+ ?cause: StandardError?
16
+ ) -> void
8
17
  end
9
18
 
10
19
  class APIError < OnebusawaySDK::Errors::Error
@@ -8,8 +8,10 @@ module OnebusawaySDK
8
8
 
9
9
  type coerce_state =
10
10
  {
11
- strictness: bool | :strong,
11
+ translate_names: bool,
12
+ strictness: bool,
12
13
  exactness: { yes: Integer, no: Integer, maybe: Integer },
14
+ error: Class,
13
15
  branched: Integer
14
16
  }
15
17
 
@@ -37,6 +39,10 @@ module OnebusawaySDK
37
39
  | OnebusawaySDK::Internal::Type::Converter::input spec
38
40
  ) -> (^-> top)
39
41
 
42
+ def self.new_coerce_state: (
43
+ ?translate_names: bool
44
+ ) -> OnebusawaySDK::Internal::Type::Converter::coerce_state
45
+
40
46
  def self.coerce: (
41
47
  OnebusawaySDK::Internal::Type::Converter::input target,
42
48
  top value,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onebusaway-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Onebusaway SDK
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-06 00:00:00.000000000 Z
11
+ date: 2025-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool