spaceborne 0.1.37 → 0.1.41

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: 8713e4121c4170d47c98acc0c85efbd98bdc8d891f7f977fc792cbeddacc7435
4
- data.tar.gz: 147f591492eb63348d02e0c88cb19086fd39e8d61851e6d82d86d0f3d45ae62d
3
+ metadata.gz: 30bd85963db86e76c6f72a21fa703b090ba6cb4573863a2d107e218b2bfe4ee3
4
+ data.tar.gz: 495ea111c1e2205cc369f29ad28132f6a208d0547ff31b9894459f61eb87be17
5
5
  SHA512:
6
- metadata.gz: a29ff847e0b2c67e3d55799b3b933e6d448cc817a6a2e1365b23f0b92a1cb65fc8ece56d6142cb8eaf13baf1e84e090875b17fc641137942669174b78761ce66
7
- data.tar.gz: 77b947419bc8c04318e0cb07ac47d122844f5e0a3ad1c0ceb993264f4966e99e54b0ca38b974a112dc9d94578b8a29547b0acaa9a09f2083546899d04d5b3276
6
+ metadata.gz: 65f0d4a71631c24f05793e5a7b78654e2a5b66054f11342dd398614d2eccee8998ef1573ec673e1c245844b68cc7f3040f015f1a719899aac3c8da6eac9163aa
7
+ data.tar.gz: dcca75d582460de8f3566a5de65bd6bc200446d493bbb9b2efc462008e7928c86bc450fc2caf2bfbef2203842b983143e478d0807e6bcc66ae7ea4efd65da620
data/.rubocop.yml CHANGED
@@ -1,3 +1,6 @@
1
+ AllCops:
2
+ SuggestExtensions: false
3
+
1
4
  Style/FrozenStringLiteralComment:
2
5
  Enabled: false
3
6
 
@@ -23,7 +26,7 @@ Metrics/ClassLength:
23
26
  Exclude:
24
27
  - 'bin/*'
25
28
 
26
- Metrics/LineLength:
29
+ Layout/LineLength:
27
30
  Exclude:
28
31
  - 'spec/airborne/**/*.rb'
29
32
 
data/README.md CHANGED
@@ -226,15 +226,16 @@ The following example shows how extensions # 6 & 8 work
226
226
  { "array": [] },
227
227
  { "foo": "bar" }],
228
228
  "highest_array": [ null,
229
- { "array": [{ "present": "name" }]}]
229
+ { "array": [{ "present": "name" }]}],
230
+ "not_an_array": null
230
231
  }
231
232
  ```
232
233
  You can now validate the fact that each element in the collection has a key which is variant, but a value that has a defined format (the first and last field values are strings).
233
234
 
234
235
  expect_json_types('array_of_hashes.*.*', first: :string, last: :string)
235
236
  expect_json_types('hash_of_hashes.*', first: :string, last: :string)
236
- expect_json_types(optional('lowest_array.*.array.*'), present: :string)
237
- expect_json_types(optional('highest_array.*.array.*'), present: :string)
237
+ expect_json_types(optional('lowest_array.*.array.*'), present: :string)
238
+ expect_json_types(optional('highest_array.*.array.*'), present: :string)
238
239
 
239
240
 
240
241
  ## Development
@@ -1,3 +1,3 @@
1
1
  module Spaceborne
2
- VERSION = '0.1.37'.freeze
2
+ VERSION = '0.1.41'.freeze
3
3
  end
data/lib/spaceborne.rb CHANGED
@@ -156,6 +156,24 @@ module Airborne
156
156
 
157
157
  # Extend airborne's expectations
158
158
  module RequestExpectations
159
+ # class used for holding an optional path
160
+ class OptionalPathExpectations
161
+ def initialize(string)
162
+ @string = string
163
+ end
164
+
165
+ def to_s
166
+ @string.to_s
167
+ end
168
+ end
169
+
170
+ def do_process_json(part, json)
171
+ json = process_json(part, json)
172
+ rescue StandardError
173
+ raise PathError,
174
+ "Expected #{json.class}\nto be an object with property #{part}"
175
+ end
176
+
159
177
  def call_with_relative_path(data, args)
160
178
  if args.length == 2
161
179
  get_by_path(args[0], data) do |json_chunk|
@@ -204,17 +222,18 @@ module Airborne
204
222
  end
205
223
  end
206
224
  end
225
+
226
+ def optional(data)
227
+ if data.is_a?(Hash)
228
+ OptionalHashTypeExpectations.new(data)
229
+ else
230
+ Airborne::OptionalPathExpectations.new(data)
231
+ end
232
+ end
207
233
  end
208
234
 
209
235
  # extension to handle hash value checking
210
236
  module PathMatcher
211
- def do_process_json(part, json)
212
- json = process_json(part, json)
213
- rescue StandardError
214
- raise PathError,
215
- "Expected #{json.class}\nto be an object with property #{part}"
216
- end
217
-
218
237
  def handle_container(json, &block)
219
238
  case json.class.name
220
239
  when 'Array'
@@ -227,7 +246,7 @@ module Airborne
227
246
  end
228
247
 
229
248
  def handle_type(type, path, json, &block)
230
- case type
249
+ case type.to_s
231
250
  when '*'
232
251
  handle_container(json, &block)
233
252
  when '?'
@@ -237,38 +256,39 @@ module Airborne
237
256
  end
238
257
  end
239
258
 
240
- def path_to_s(path)
241
- path.is_a?(Airborne::OptionalHashTypeExpectations) ? path.hash.to_s : path
242
- end
243
-
244
- def make_path_optional(path, sub_path)
245
- if path.is_a?(Airborne::OptionalHashTypeExpectations)
246
- Airborne::OptionalHashTypeExpectations.new(sub_path)
259
+ def make_sub_path_optional(path, sub_path)
260
+ if path.is_a?(Airborne::OptionalPathExpectations)
261
+ Airborne::OptionalPathExpectations.new(sub_path)
247
262
  else
248
263
  sub_path
249
264
  end
250
265
  end
251
266
 
252
267
  def iterate_path(path)
253
- raise PathError, "Invalid Path, contains '..'" if /\.\./ =~ path_to_s(path)
268
+ raise PathError, "Invalid Path, contains '..'" if /\.\./ =~ path.to_s
254
269
 
255
- parts = path_to_s(path).to_s.split('.')
270
+ parts = path.to_s.split('.')
256
271
  parts.each_with_index do |part, index|
257
- use_part = make_path_optional(path, part)
272
+ use_part = make_sub_path_optional(path, part)
258
273
  yield(parts, use_part, index)
259
274
  end
260
275
  end
261
276
 
277
+ def shortcut_validation(path, json)
278
+ json.nil? && path.is_a?(Airborne::OptionalPathExpectations)
279
+ end
280
+
262
281
  def get_by_path(path, json, type: false, &block)
263
282
  iterate_path(path) do |parts, part, index|
264
- if %w[* ?].include?(path_to_s(part))
265
- ensure_array_or_hash(path, json)
283
+ return if shortcut_validation(path, json)
284
+
285
+ if %w[* ?].include?(part.to_s)
266
286
  type = part
267
- walk_with_path(type, index, path, parts, json, &block) && return if index < parts.length.pred
287
+ walk_with_path(type.to_s, index, path, parts, json, &block) && return if index < parts.length.pred
268
288
 
269
289
  next
270
290
  end
271
- json = do_process_json(path_to_s(part), json)
291
+ json = do_process_json(part.to_s, json)
272
292
  end
273
293
  handle_type(type, path, json, &block)
274
294
  end
@@ -288,7 +308,7 @@ module Airborne
288
308
  json.each do |element|
289
309
  begin
290
310
  sub_path = parts[(index.next)...(parts.length)].join('.')
291
- get_by_path(make_path_optional(path, sub_path), element, &block)
311
+ get_by_path(make_sub_path_optional(path, sub_path), element, &block)
292
312
  rescue Exception => e
293
313
  last_error = e
294
314
  error_count += 1
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spaceborne
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.37
4
+ version: 0.1.41
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Williams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-20 00:00:00.000000000 Z
11
+ date: 2021-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport