nice_hash 1.2.0 → 1.3.0

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: 9a55c7666de081ceed78de43ec3817b7dadee88a43778db3c95276717697be39
4
- data.tar.gz: b44dcdf7403cc64651333533e67ca21ec580d13eca9328a6da4529023290e730
3
+ metadata.gz: b547b217f676c7ddc188941e8ca40a075e96296719f2105b20dd6c3dda055195
4
+ data.tar.gz: dd9de32c94ecc5b6c2088f8f2a7816693b3030ddc3a4f545467c98944240eb19
5
5
  SHA512:
6
- metadata.gz: fcbd50ee4edaf0ebf8031f3cc507abf67994968b2cb06475f32d6d5ac08d744872f2a7ecd1134eb8212de770a59b122728382cd24b1ceb81ec9b90ee7a57ebb8
7
- data.tar.gz: 559c22fde98d99ae59287e1b3be892481772950e92ad3bdb3fc4116e8b89c407862c4e14b25deb2d86060279e895a21c0694e5d39a55edd61777ec3ca4b5b55f
6
+ metadata.gz: 1d4fdfc2239e82ccb4549b6f2e793fe1a85347c849f2fea3dbe7c0ae7b1296c6abdaa2b6192a026f5b0a8dc1884e505edeca23c62b97f9d9bcaa7c2461426f49
7
+ data.tar.gz: 179e667f98084f40178e3308c991d61247b8a6538fe58a80b88c3f561a250b4dbbc94d6d80877cef52a046f8baebc17f8c93e696acb3b5334a053aaf8f161f9b
data/README.md CHANGED
@@ -8,6 +8,8 @@ You can easily generates all the hashes you want following the criteria you spec
8
8
 
9
9
  Many other features coming to Hash class like the methods 'bury' or select_key, access the keys like methods: my_hash.my_key.other_key. You will be able to generate thousands of different hashes just declaring one and test easily APIs based on JSON for example.
10
10
 
11
+ You can also parse and filter a json string very easily.
12
+
11
13
  To generate the strings following a pattern take a look at the documentation for string_pattern gem: https://github.com/MarioRuiz/string_pattern
12
14
 
13
15
  ## Installation
@@ -103,6 +105,16 @@ In case you want one pattern to be generated with unique values, so never repeat
103
105
 
104
106
  ```
105
107
 
108
+ Also if you have a JSON string you want to parse it and get the values of certain keys you can use the json method we added to nice_hash:
109
+
110
+ ```ruby
111
+ my_json_string="{\"id\":344,\"customer\":{\"name\":\"Peter Smith\",\"phone\":334334333},\"tickets\":[{\"idt\":345,\"name\":\"myFavor1\"},{\"idt\":3123},{\"idt\":3145,\"name\":\"Special ticket\"}]}"
112
+ puts my_json_string.json(:idt)
113
+ #> [345, 3123, 3145]
114
+
115
+ puts my_json_string.json(:idt, :name)
116
+ #> {:name=>["Peter Smith", ["myFavor1", "Special ticket"]], :idt=>[345, 3123, 3145]}
117
+ ```
106
118
 
107
119
  ### How to access the different keys
108
120
 
@@ -137,6 +149,15 @@ By using the string_pattern gem you can generate single strings following the sp
137
149
  puts my_hash._zip._correct.gen # gen is an alias for generate method #>84584
138
150
  ```
139
151
 
152
+ If you want to search for all the values of one or more keys use get_values method:
153
+
154
+ ```ruby
155
+ new_hash = my_hash.generate
156
+ puts new_hash.get_values(:address) #> {:address=>"21 Doom Av"}
157
+ puts new_hash.get_values(:address, :zip) #> {:zip=>{:default=>"00000", :correct=>"42782"}, :address=>"21 Doom Av"}
158
+ puts new_hash.get_values(:drawId) #> {:drawId=>["84914", "21158"]}
159
+ ```
160
+
140
161
  ### Filtering / Selecting an specific key on the hash and subhashes
141
162
 
142
163
  In case you supply different possibilities to be used like for example on fields: owner, zip, city and mobilePhone, and you one to use a concrete one, use the method select_key
@@ -1,5 +1,49 @@
1
- class Array
1
+ class String
2
+ ###########################################################################
3
+ # In case the string is a json it will return the keys specified. the keys need to be provided as symbols
4
+ # input:
5
+ # keys:
6
+ # 1 value with key or an array of keys
7
+ # In case the key supplied doesn't exist in the hash then it will be returned nil for that one
8
+ # output:
9
+ # if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash op arrays
10
+ # if no keys given, an empty hash
11
+ ###########################################################################
12
+ def json(*keys)
13
+ require 'json'
14
+ feed_symbols = JSON.parse(self, symbolize_names: true)
15
+ result = {}
16
+ if !keys.empty?
17
+ result_tmp = if keys[0].is_a?(Symbol)
18
+ NiceHash.get_values(feed_symbols, keys)
19
+ else
20
+ {}
21
+ end
22
+
23
+ if result_tmp.size == 1
24
+ result = if result_tmp.values.is_a?(Array) && (result_tmp.values.size == 1)
25
+ result_tmp.values[0]
26
+ else
27
+ result_tmp.values
28
+ end
29
+ else
30
+ result_tmp.each do |key, value|
31
+ result[key] = if (value.is_a?(Array) || value.is_a?(Hash)) && (value.size == 1)
32
+ value[0]
33
+ else
34
+ value
35
+ end
36
+ end
37
+ end
38
+
39
+ else
40
+ result = feed_symbols
41
+ end
42
+ result
43
+ end
44
+ end
2
45
 
46
+ class Array
3
47
  ###########################################################################
4
48
  # Stores a value on the location indicated
5
49
  # input:
@@ -10,14 +54,28 @@ class Array
10
54
  # my_array.bury([2, 1, :original],"the value to set") #array of array of hash
11
55
  ###########################################################################
12
56
  def bury(where, value)
13
- me=self
14
- where[0..-2].each {|key|
15
- me=me[key]
16
- }
17
- me[where[-1]]=value
57
+ me = self
58
+ where[0..-2].each do |key|
59
+ me = me[key]
60
+ end
61
+ me[where[-1]] = value
18
62
  end
19
- end
20
63
 
64
+ ###########################################################################
65
+ # In case of an array of json strings will return the keys specified. The keys need to be provided as symbols
66
+ # input:
67
+ # keys:
68
+ # 1 value with key or an array of keys
69
+ # In case the key supplied doesn't exist in the hash then it will be return nil for that one
70
+ # output:
71
+ # if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash of arrays
72
+ # if no keys given, an empty hash
73
+ ###########################################################################
74
+ def json(*keys)
75
+ json_string = "[#{join(',')}]"
76
+ json_string.json(*keys)
77
+ end
78
+ end
21
79
 
22
80
  class Hash
23
81
  ###########################################################################
@@ -33,13 +91,11 @@ class Hash
33
91
  # my_hash.products[1].price.wrong="AAAAA"
34
92
  ###########################################################################
35
93
  def method_missing(m, *arguments, &block)
36
- if m[0]=='_'
37
- m=m[1..-1].to_sym
38
- end
39
- if self.keys.include?(m)
94
+ m = m[1..-1].to_sym if m[0] == '_'
95
+ if key?(m)
40
96
  self[m]
41
- elsif m.to_s[-1]=="="
42
- self[m.to_s.chop.to_sym]=arguments[0]
97
+ elsif m.to_s[-1] == '='
98
+ self[m.to_s.chop.to_sym] = arguments[0]
43
99
  else
44
100
  super
45
101
  end
@@ -55,15 +111,15 @@ class Hash
55
111
  # my_hash.bury([:original, 1, :doom],"the value to set") #hash of array of hash
56
112
  ###########################################################################
57
113
  def bury(where, value)
58
- me=self
59
- where[0..-2].each {|key|
60
- me=me[key]
61
- }
62
- key=where[-1]
63
- key=[key] unless where[-1].kind_of?(Array) # for the case same value for different keys, for example pwd1, pwd2, pwd3
64
- key.each {|k|
65
- me[k]=value
66
- }
114
+ me = self
115
+ where[0..-2].each do |key|
116
+ me = me[key]
117
+ end
118
+ key = where[-1]
119
+ key = [key] unless where[-1].is_a?(Array) # for the case same value for different keys, for example pwd1, pwd2, pwd3
120
+ key.each do |k|
121
+ me[k] = value
122
+ end
67
123
  end
68
124
 
69
125
  ###########################################################################
@@ -82,7 +138,7 @@ class Hash
82
138
  # More info: NiceHash.generate
83
139
  # alias: gen
84
140
  ###########################################################################
85
- def generate(select_hash_key=nil, expected_errors: [], **synonyms)
141
+ def generate(select_hash_key = nil, expected_errors: [], **synonyms)
86
142
  NiceHash.generate(self, select_hash_key, expected_errors: expected_errors, **synonyms)
87
143
  end
88
144
 
@@ -91,7 +147,7 @@ class Hash
91
147
  # More info: NiceHash.validate
92
148
  # alias: val
93
149
  ###########################################################################
94
- def validate(select_hash_key=nil, values_hash_to_validate)
150
+ def validate(select_hash_key = nil, values_hash_to_validate)
95
151
  NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: false)
96
152
  end
97
153
 
@@ -99,7 +155,7 @@ class Hash
99
155
  # Validates a given values_hash_to_validate with string patterns
100
156
  # More info: NiceHash.validate
101
157
  ###########################################################################
102
- def validate_patterns(select_hash_key=nil, values_hash_to_validate)
158
+ def validate_patterns(select_hash_key = nil, values_hash_to_validate)
103
159
  NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: true)
104
160
  end
105
161
 
@@ -119,9 +175,15 @@ class Hash
119
175
  NiceHash.select_fields(self, *select_hash_key)
120
176
  end
121
177
 
178
+ ###########################################################################
179
+ # Get values of the keys supplied from the Hash structure.
180
+ # More info: NiceHash.get_values
181
+ ###########################################################################
182
+ def get_values(*keys)
183
+ NiceHash.get_values(self, keys)
184
+ end
122
185
 
123
- alias_method :gen, :generate
124
- alias_method :val, :validate
125
- alias_method :patterns, :pattern_fields
126
-
186
+ alias gen generate
187
+ alias val validate
188
+ alias patterns pattern_fields
127
189
  end
data/lib/nice_hash.rb CHANGED
@@ -579,4 +579,132 @@ class NiceHash
579
579
  return array
580
580
  end
581
581
 
582
+
583
+ ##################################################
584
+ # Get values from the Hash structure (array of Hashes allowed)
585
+ # In case the key supplied doesn't exist in the hash then it will be return nil for that one
586
+ # input:
587
+ # hash: a simple hash or a hash containing arrays. Example:
588
+ # example={"id"=>344,
589
+ # "customer"=>{
590
+ # "name"=>"Peter Smith",
591
+ # "phone"=>334334333
592
+ # },
593
+ # "tickets"=>[
594
+ # {"idt"=>345,"name"=>"myFavor1"},
595
+ # {"idt"=>3123},
596
+ # {"idt"=>3145,"name"=>"Special ticket"}
597
+ # ]
598
+ # }
599
+ # keys: one key (string) or an array of keys
600
+ # output:
601
+ # a Hash of Arrays with all values found.
602
+ # Example of output with example.get_values("id","name")
603
+ # {"id"=>[334],"name"=>["Peter North"]}
604
+ # Example of output with example.get_values("idt")
605
+ # {"idt"=>[345,3123,3145]}
606
+ #
607
+ ####################################################
608
+ def NiceHash.get_values(hash,keys)
609
+ if keys.kind_of?(String) or keys.kind_of?(Symbol) then
610
+ keys=[keys]
611
+ end
612
+ result=Hash.new()
613
+ number_of_results=Hash.new()
614
+ keys.each {|key|
615
+ number_of_results[key]=0
616
+ }
617
+ if hash.kind_of?(Array) then
618
+ hash.each {|tmp|
619
+ if tmp.kind_of?(Array) or tmp.kind_of?(Hash) then
620
+ n_result=get_values(tmp, keys)
621
+ if n_result!=:error then
622
+ n_result.each {|n_key, n_value|
623
+ if result.has_key?(n_key) then
624
+ if !result[n_key].kind_of?(Array) or
625
+ (result[n_key].kind_of?(Array) and number_of_results[n_key] < result[n_key].size) then
626
+ if result[n_key].kind_of?(Hash) or result[n_key].kind_of?(Array) then
627
+ res_tx=result[n_key].dup()
628
+ else
629
+ res_tx=result[n_key]
630
+ end
631
+ result[n_key]=Array.new()
632
+ result[n_key].push(res_tx)
633
+ result[n_key].push(n_value)
634
+ else
635
+ result[n_key].push(n_value)
636
+ end
637
+ else
638
+ result[n_key]=n_value
639
+ end
640
+ number_of_results[n_key]+=1
641
+ }
642
+ end
643
+ end
644
+ }
645
+ elsif hash.kind_of?(Hash) then
646
+ hash.each {|key, value|
647
+ #if keys.include?(key) then
648
+ #added to be able to access the keys with symbols to strings and opposite
649
+ if keys.include?(key) or keys.include?(key.to_s) or keys.include?(key.to_sym) then
650
+ #added to be able to access the keys with symbols to strings and opposite
651
+ key=key.to_s() if keys.include?(key.to_s)
652
+ key=key.to_sym() if keys.include?(key.to_sym)
653
+
654
+ if result.has_key?(key) then
655
+ if !result[key].kind_of?(Array) or
656
+ (result[key].kind_of?(Array) and number_of_results[key] < result[key].size) then
657
+ if result[key].kind_of?(Hash) or result[key].kind_of?(Array) then
658
+ res_tx=result[key].dup()
659
+ else
660
+ res_tx=result[key]
661
+ end
662
+ result[key]=Array.new()
663
+ result[key].push(res_tx)
664
+ result[key].push(value)
665
+ else
666
+ result[key].push(value)
667
+ end
668
+ else
669
+ result[key]=value
670
+ end
671
+ number_of_results[key]+=1
672
+ end
673
+ if value.kind_of?(Array) or value.kind_of?(Hash) then
674
+ n_result=get_values(value, keys)
675
+ if n_result!=:error then
676
+ n_result.each {|n_key, n_value|
677
+ if result.has_key?(n_key) then
678
+ if !result[n_key].kind_of?(Array) or
679
+ (result[n_key].kind_of?(Array) and number_of_results[n_key] < result[n_key].size) then
680
+ if result[n_key].kind_of?(Hash) or result[n_key].kind_of?(Array) then
681
+ res_tx=result[n_key].dup()
682
+ else
683
+ res_tx=result[n_key]
684
+ end
685
+ result[n_key]=Array.new()
686
+ result[n_key].push(res_tx)
687
+ result[n_key].push(n_value)
688
+ else
689
+ result[n_key].push(n_value)
690
+ end
691
+ else
692
+ result[n_key]=n_value
693
+ end
694
+ number_of_results[n_key]+=1
695
+ }
696
+ end
697
+ end
698
+ }
699
+ else
700
+ return :error
701
+ end
702
+ if result.kind_of?(Hash) and caller[0]["get_values"].nil? then #no error or anything weird
703
+ (keys-result.keys).each {|k| #in case some keys don't exist in the hash
704
+ result[k]=nil
705
+ }
706
+ end
707
+ return result
708
+ end
709
+
582
710
  end
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.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-08 00:00:00.000000000 Z
11
+ date: 2018-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: string_pattern
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '1.4'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 1.4.0
22
+ version: 1.4.1
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '1.4'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 1.4.0
32
+ version: 1.4.1
33
33
  description: 'You can easily generates all the hashes you want following the criteria
34
34
  you specify. Many other features coming to Hash class like the methods ''bury''
35
35
  or select_key, access the keys like methods: my_hash.my_key.other_key. You will
@@ -71,5 +71,5 @@ rubygems_version: 2.7.6
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: NiceHash creates hashes following certain patterns so your testing will be
74
- much easier.
74
+ much easier. Parse and filter JSON.
75
75
  test_files: []