json_normalizer 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json_normalizer.rb +32 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f326f1ce438826dbe5512abdd815b24a5d35157
|
4
|
+
data.tar.gz: edad37f5355ba047057179650a9c8cbd0564dea7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 977ea62e0137b15ce9a40705bd7d461b37aed441785a4a810fb1a1b33bb05c30a04977bf5c8efba364a175bc32afde31081ee5eb5d17317aa8013ac271c76d20
|
7
|
+
data.tar.gz: f0be58054fb53aa51db391d2fd06e97f1e696204dd7b952e070f9f535ebc818d38fd6e5bded2f07a3a40fcd5e5b25841657ea064de76b497cd269892fb5f5b70
|
data/lib/json_normalizer.rb
CHANGED
@@ -5,31 +5,43 @@ class JsonNormalizer
|
|
5
5
|
# Init requires pre defined JSON map.
|
6
6
|
# Ex. { "this_key": ["morphed", "to", "this_key"] }
|
7
7
|
def initialize(map)
|
8
|
-
@map =
|
9
|
-
@
|
8
|
+
@map = make_hash(map)
|
9
|
+
@mapped_keys = mapped_keys
|
10
|
+
@keymap = keymap
|
10
11
|
end
|
11
12
|
|
12
13
|
def key_contained?(key)
|
13
|
-
@
|
14
|
-
return false
|
14
|
+
@mapped_keys.include?(key)
|
15
15
|
end
|
16
16
|
|
17
17
|
def fetch_key(key)
|
18
|
-
@
|
18
|
+
@keymap[key]
|
19
19
|
end
|
20
20
|
|
21
21
|
def swap_key(json, key)
|
22
22
|
if self.key_contained?(key)
|
23
|
-
json[
|
24
|
-
json.delete(key)
|
23
|
+
json[fetch_key(key)] = json.delete(key)
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
27
|
+
def batch_translate(batch=[])
|
28
|
+
batch.map!{|item| make_hash(item) }.map!{|item| recur_translate(item) }
|
29
|
+
batch
|
30
|
+
end
|
31
|
+
|
32
|
+
def make_hash(item)
|
33
|
+
[Hash, Array].include?(item.class) ? item : JSON.parse(item)
|
34
|
+
end
|
35
|
+
|
28
36
|
def translate(json)
|
29
|
-
json =
|
37
|
+
json = make_hash(json)
|
38
|
+
recur_translate(json)
|
39
|
+
end
|
40
|
+
|
41
|
+
def recur_translate(json)
|
30
42
|
if json.is_a?(Array)
|
31
43
|
json.each do |j|
|
32
|
-
translate(j)
|
44
|
+
translate(j) if [Hash, Array].include?(j.class)
|
33
45
|
end
|
34
46
|
else
|
35
47
|
json.keys.each do |key|
|
@@ -46,4 +58,15 @@ class JsonNormalizer
|
|
46
58
|
end
|
47
59
|
json
|
48
60
|
end
|
61
|
+
|
62
|
+
##### SETTERS #####
|
63
|
+
def keymap
|
64
|
+
km = {}
|
65
|
+
@map.each {|k,v| v.each{ |old_key| km.merge!({old_key => k}) } }
|
66
|
+
km
|
67
|
+
end
|
68
|
+
|
69
|
+
def mapped_keys
|
70
|
+
@map.values.flatten
|
71
|
+
end
|
49
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_normalizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Tetzlaff
|
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
85
|
requirements:
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
88
|
+
version: 2.3.0
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
90
|
requirements:
|
91
91
|
- - ">="
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.5.1
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Normalizes JSON Doc Schema.
|