spaceborne 0.1.37 → 0.1.38

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8713e4121c4170d47c98acc0c85efbd98bdc8d891f7f977fc792cbeddacc7435
4
- data.tar.gz: 147f591492eb63348d02e0c88cb19086fd39e8d61851e6d82d86d0f3d45ae62d
3
+ metadata.gz: d742ea06ad022126a7a61fb008131b02d0f0f1db485d832570b85f845299c655
4
+ data.tar.gz: d7d452c83c164adebc69df71aef6aa2f12d50375be2e354463f7b7789b07c290
5
5
  SHA512:
6
- metadata.gz: a29ff847e0b2c67e3d55799b3b933e6d448cc817a6a2e1365b23f0b92a1cb65fc8ece56d6142cb8eaf13baf1e84e090875b17fc641137942669174b78761ce66
7
- data.tar.gz: 77b947419bc8c04318e0cb07ac47d122844f5e0a3ad1c0ceb993264f4966e99e54b0ca38b974a112dc9d94578b8a29547b0acaa9a09f2083546899d04d5b3276
6
+ metadata.gz: 7e16b9d1bc60eabc5257afb91510d4a715bd8ce76acccc820a255a96ad4b19f87732360d98e3dfbfcfd0f53bd2d7524a72868602df29b2bc5fa871a15128e40e
7
+ data.tar.gz: 0bac75438755e532b9d39c10864d91430d9a7d281f7d89b9ee1d1943ab7951381432fb0e48ccd39bbc95725dafb8db93a99ba4edc9a53eebec3d6c4bc8e15e59
data/lib/spaceborne.rb CHANGED
@@ -156,6 +156,22 @@ module Airborne
156
156
 
157
157
  # Extend airborne's expectations
158
158
  module RequestExpectations
159
+ class OptionalPathExpectations
160
+ def initialize(string)
161
+ @string = string
162
+ end
163
+ def to_s
164
+ @string.to_s
165
+ end
166
+ end
167
+
168
+ def do_process_json(part, json)
169
+ json = process_json(part, json)
170
+ rescue StandardError
171
+ raise PathError,
172
+ "Expected #{json.class}\nto be an object with property #{part}"
173
+ end
174
+
159
175
  def call_with_relative_path(data, args)
160
176
  if args.length == 2
161
177
  get_by_path(args[0], data) do |json_chunk|
@@ -204,17 +220,18 @@ module Airborne
204
220
  end
205
221
  end
206
222
  end
223
+
224
+ def optional(data)
225
+ if data.is_a?(Hash)
226
+ OptionalHashTypeExpectations.new(data)
227
+ else
228
+ OptionalPathExpectations.new(data)
229
+ end
230
+ end
207
231
  end
208
232
 
209
233
  # extension to handle hash value checking
210
234
  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
235
  def handle_container(json, &block)
219
236
  case json.class.name
220
237
  when 'Array'
@@ -237,38 +254,34 @@ module Airborne
237
254
  end
238
255
  end
239
256
 
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)
257
+ def make_sub_path_optional(path, sub_path)
258
+ if path.is_a?(Airborne::OptionalPathExpectations)
259
+ Airborne::OptionalPathExpectations.new(sub_path)
247
260
  else
248
261
  sub_path
249
262
  end
250
263
  end
251
264
 
252
265
  def iterate_path(path)
253
- raise PathError, "Invalid Path, contains '..'" if /\.\./ =~ path_to_s(path)
266
+ raise PathError, "Invalid Path, contains '..'" if /\.\./ =~ path.to_s
254
267
 
255
- parts = path_to_s(path).to_s.split('.')
268
+ parts = path.to_s.split('.')
256
269
  parts.each_with_index do |part, index|
257
- use_part = make_path_optional(path, part)
270
+ use_part = make_sub_path_optional(path, part)
258
271
  yield(parts, use_part, index)
259
272
  end
260
273
  end
261
274
 
262
275
  def get_by_path(path, json, type: false, &block)
263
276
  iterate_path(path) do |parts, part, index|
264
- if %w[* ?].include?(path_to_s(part))
277
+ if %w[* ?].include?(part.to_s)
265
278
  ensure_array_or_hash(path, json)
266
279
  type = part
267
280
  walk_with_path(type, index, path, parts, json, &block) && return if index < parts.length.pred
268
281
 
269
282
  next
270
283
  end
271
- json = do_process_json(path_to_s(part), json)
284
+ json = do_process_json(part.to_s, json)
272
285
  end
273
286
  handle_type(type, path, json, &block)
274
287
  end
@@ -288,7 +301,7 @@ module Airborne
288
301
  json.each do |element|
289
302
  begin
290
303
  sub_path = parts[(index.next)...(parts.length)].join('.')
291
- get_by_path(make_path_optional(path, sub_path), element, &block)
304
+ get_by_path(make_sub_path_optional(path, sub_path), element, &block)
292
305
  rescue Exception => e
293
306
  last_error = e
294
307
  error_count += 1
@@ -1,3 +1,3 @@
1
1
  module Spaceborne
2
- VERSION = '0.1.37'.freeze
2
+ VERSION = '0.1.38'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.38
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Williams