nice_hash 1.12.8 → 1.12.9

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -3
  3. data/lib/nice_hash.rb +34 -4
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90b8f9fc57bbbfad89f5c5545609e05486240881cbad12f64226cd2e1cc1f5ea
4
- data.tar.gz: 1942366e5103e57ad221c08b0766cfdf135a70422574fe89ee64c6992c5b650e
3
+ metadata.gz: 3bff78c12a5f0b46e46eb9f2af18c66d570dfa51b9c45ac2a6600939c8387f28
4
+ data.tar.gz: d92ba9acd53a8f288a1be13a9d50c6f0924ae72168bdafb32dcf4e097e12e269
5
5
  SHA512:
6
- metadata.gz: a56520db1ad240c8eddf0a0f6dda03f47736b94792b442d1cb8e26457da36277a0244640b82dc7729e7cb6ef536feec2db2b2bc2eabf11e389de39af8d7c972f
7
- data.tar.gz: 173cd64e2ca12b2040ea9f9d124b041601ae60052c44614073b08e34657975a3403133c6b0042a380b17e84c7c6284a19199d8bef576a023559940107708ab1b
6
+ metadata.gz: e89e0412ce972946f6b2ec5e2df538849d492d45c716cf23ee538ddb44082004db33f9b23ea778ce249fcc44140d896e1ec82f972fcf80e71bcffe7926385e39
7
+ data.tar.gz: 77a9745af901aafc25463b3569d7870fa4621307a107870c3a71bc40086a060840587f1096ecc66ae0eb0205db8255c8a901f0c6ffa8f2b3fc2ee411d83f21cc
data/README.md CHANGED
@@ -53,6 +53,8 @@ my_hash={
53
53
  loginame: :"5-10:/xn/",
54
54
  [:pwd1, :pwd2, :pwd3] => :"5-10:L/n/",
55
55
  name: :"10-20:T_/x/",
56
+ age: 18..120,
57
+ euros: 0.0..3000.0,
56
58
  draws: [
57
59
  {
58
60
  drawId: :"5:N",
@@ -92,6 +94,8 @@ Explanations of the different fields:
92
94
  loginname: from 5 to 10 characters, mandatory to have lower letters and numbers
93
95
  pwd, pwd2, pwd3: will have the same value. The value from 5 to 10 chars, optional capital and lower letters, necessary to contain numbers
94
96
  name: from 10 to 20 chars. Optional national characters and space, necessary lower letters.
97
+ age: number from 18 to 120
98
+ euros: float number from 0.0 to 3000.0
95
99
  drawId: 5 numbers
96
100
  drawName: 10 letters and/or numbers
97
101
  type: 'Weekely' or 'Daily'
@@ -236,6 +240,8 @@ On this example new_hash will contain:
236
240
  loginame: :"5-10:/xn/",
237
241
  [:pwd1, :pwd2, :pwd3] => :"5-10:L/n/",
238
242
  name: :"10-20:T_/x/",
243
+ age: 18..120,
244
+ euros: 0.0..3000.0,
239
245
  draws: [
240
246
  {
241
247
  drawId: :"5:N",
@@ -294,6 +300,8 @@ In case of filtering by :correct new_hash would have a value like this for examp
294
300
  :pwd2=>"E6hz9YS7",
295
301
  :pwd3=>"E6hz9YS7",
296
302
  :name=>"OyTQNfEyPOzVYMxPym",
303
+ :age=> 19,
304
+ :euros=> 2133.34,
297
305
  :draws=>
298
306
  [{:drawId=>"54591",
299
307
  :drawName=>"cr5Q7pq4G8",
@@ -351,6 +359,8 @@ On this example wrong_min_length will contain something like:
351
359
  :pwd2=>"4XDx",
352
360
  :pwd3=>"4XDx",
353
361
  :name=>"bU",
362
+ :age=> 5,
363
+ :euros=> -452.311,
354
364
  :draws=>
355
365
  [{:drawId=>"", :drawName=>"P03AgdMqV", :type=>"Dail", :owner=>"dYzLRMCnVc"},
356
366
  {:drawId=>"", :drawName=>"qw", :type=>"Dail", :owner=>"zkHhTEzM"}],
@@ -485,6 +495,8 @@ After using the bury method default_values will contain:
485
495
  :pwd2=>"x33exx",
486
496
  :pwd3=>"x33exx",
487
497
  :name=>"HdmsjLxlEgYIFY",
498
+ :age=> 20,
499
+ :euros=> 155.11,
488
500
  :draws=>
489
501
  [{:drawId=>"12318",
490
502
  :drawName=>"FirstDraw",
@@ -508,16 +520,19 @@ After using the bury method default_values will contain:
508
520
 
509
521
  If you have a Hash that should follow the patterns you specified (in this example declared on my_hash) and you want to validate, then use the 'validate' method.
510
522
 
511
- This is particulary useful to test REST APIs responses in JSON
523
+ This is specially useful to test REST APIs responses in JSON
512
524
 
513
525
  If we have a hash with these values:
514
526
 
515
527
  ```ruby
516
- {:loginame=>"rdewvqur",
528
+ values = {
529
+ :loginame=>"rdewvqur",
517
530
  :pwd1=>"d3ulo",
518
531
  :pwd2=>"d3ulo",
519
532
  :pwd3=>"d3ulo",
520
533
  :name=>"LTqVKxxFCTqpkdjFkxU",
534
+ :age=> 20,
535
+ :euros=> 155.11,
521
536
  :draws=>
522
537
  [{:drawId=>"54a43",
523
538
  :drawName=>"h3F24yjMWp",
@@ -537,7 +552,7 @@ If we have a hash with these values:
537
552
  :display=>true}
538
553
  ```
539
554
 
540
- To validate those values taking in consideration was is stated on my_hash:
555
+ To validate those values against the patterns defined on my_hash:
541
556
 
542
557
  ```ruby
543
558
  results_all_fields = my_hash.validate :correct, values
@@ -359,9 +359,7 @@ class NiceHash
359
359
  else
360
360
  value = NiceHash.generate(value, select_hash_key, expected_errors: expected_errors)
361
361
  end
362
- end
363
-
364
- if value.kind_of?(String) or value.kind_of?(Symbol)
362
+ elsif value.kind_of?(String) or value.kind_of?(Symbol)
365
363
  if ((StringPattern.optimistic and value.kind_of?(String)) or value.kind_of?(Symbol)) and value.to_s.scan(/^!?\d+-?\d*:.+/).size > 0
366
364
  hashv[key] = StringPattern.generate(value, expected_errors: expected_errors)
367
365
  elsif ((StringPattern.optimistic and value.kind_of?(String)) or value.kind_of?(Symbol)) and value.to_s.scan(/^([\w\s\-]+\|)+[\w\s\-]+$/).size > 0
@@ -415,10 +413,31 @@ class NiceHash
415
413
  }
416
414
  hashv[key] = value_ret
417
415
  end
416
+ elsif value.kind_of?(Range)
417
+ if expected_errors.empty?
418
+ hashv[key] = rand(value)
419
+ else
420
+ hashv[key] = rand(value)
421
+ expected_errors.each do |er|
422
+ if er == :min_length
423
+ hashv[key] = rand((value.first-value.last)..value.first-1)
424
+ elsif er == :max_length
425
+ hashv[key] = rand((value.last+1)..(value.last*2))
426
+ elsif er == :length
427
+ if rand.round==1
428
+ hashv[key] = rand((value.first-value.last)..value.first-1)
429
+ else
430
+ hashv[key] = rand((value.last+1)..(value.last*2))
431
+ end
432
+ elsif er == :value
433
+ hashv[key] = :"1-10:N/L/".gen
434
+ end
435
+ end
436
+ end
418
437
  elsif value.kind_of?(Proc)
419
438
  hashv[key] = value.call
420
439
  elsif value.kind_of?(Regexp)
421
- hashv[key] = value.generate
440
+ hashv[key] = value.generate(expected_errors: expected_errors)
422
441
  else
423
442
  hashv[key] = value
424
443
  end
@@ -523,6 +542,17 @@ class NiceHash
523
542
  elsif !only_patterns
524
543
  results[key] = false unless value.to_s == values[key].to_s
525
544
  end
545
+ elsif value.kind_of?(Range)
546
+ if values[key].class != value.first.class or values[key].class != value.last.class
547
+ results[key] = false
548
+ elsif values[key] < value.first or values[key] > value.last
549
+ results[key] = false
550
+ end
551
+ elsif value.kind_of?(Regexp)
552
+ rex = Regexp.new("^#{value}$")
553
+ unless values[key].match?(rex)
554
+ results[key] = false
555
+ end
526
556
  elsif value.kind_of?(Array)
527
557
  array_pattern = false
528
558
  complex_data = false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nice_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.8
4
+ version: 1.12.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-07 00:00:00.000000000 Z
11
+ date: 2019-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: string_pattern