format_data_converter 0.5.2 → 0.6.0
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.
data/README.rdoc
ADDED
@@ -1,19 +1,19 @@
|
|
1
|
-
require "format_data_converter/
|
2
|
-
require "format_data_converter/
|
3
|
-
require "format_data_converter/
|
1
|
+
require "format_data_converter/converter"
|
2
|
+
require "format_data_converter/detector"
|
3
|
+
require "format_data_converter/hasher"
|
4
4
|
|
5
5
|
class FDC
|
6
6
|
|
7
7
|
def self.detect_and_to_json s
|
8
|
-
to_json(detect s)
|
8
|
+
Converter.to_json(Detector.detect s)
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.detect_and_to_hash_symbols s
|
12
|
-
to_hash_symbols(detect s)
|
12
|
+
Converter.to_hash_symbols(Detector.detect s)
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.detect_and_to_hash s
|
16
|
-
to_hash_symbols(detect s)
|
16
|
+
Converter.to_hash_symbols(Detector.detect s)
|
17
17
|
end
|
18
18
|
|
19
19
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
class Converter
|
2
|
+
|
3
|
+
def self.to_xml s
|
4
|
+
s=stringify(eval(s)).to_s
|
5
|
+
n = 0
|
6
|
+
name_parent = []
|
7
|
+
sxml = ""
|
8
|
+
for i in 0..s.length-1
|
9
|
+
if (s[i..i+1] == "{\"")
|
10
|
+
sxml << "<"
|
11
|
+
saux = s.clone
|
12
|
+
n = n+1
|
13
|
+
name_parent[n] = saux.slice(i+2..(saux.index('"', i+2)-1))
|
14
|
+
if (!name_parent[n].nil?)
|
15
|
+
sxml << name_parent[n] << ">"
|
16
|
+
end
|
17
|
+
elsif (s[i] == "}")
|
18
|
+
sxml << "</"
|
19
|
+
print name_parent
|
20
|
+
sxml << name_parent[n]
|
21
|
+
n = n-1
|
22
|
+
sxml << ">"
|
23
|
+
elsif (s[i..i+1] == "=>")
|
24
|
+
if (s[i+2] == "[")
|
25
|
+
elsif saux = s.clone
|
26
|
+
sxml << saux.slice(i+2..(saux.index("}", i+2)-1))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
sxml
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.to_json s
|
34
|
+
s=stringify(eval(s)).to_s
|
35
|
+
s.gsub("=>",":")
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.to_hash_symbols s
|
39
|
+
s=symbolize(eval(s)).to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.to_hash s
|
43
|
+
s=stringify(eval(s)).to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.stringify(obj)
|
47
|
+
return obj.inject({}){|memo,(k,v)| memo[k.to_s] = stringify(v); memo} if obj.is_a? Hash
|
48
|
+
return obj.inject([]){|memo,v | memo << stringify(v); memo} if obj.is_a? Array
|
49
|
+
return obj
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.symbolize(obj)
|
53
|
+
return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize(v); memo} if obj.is_a? Hash
|
54
|
+
return obj.inject([]){|memo,v | memo << symbolize(v); memo} if obj.is_a? Array
|
55
|
+
return obj
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class Detector
|
2
|
+
def self.detect s
|
3
|
+
if detect_json s
|
4
|
+
Hasher.from_json(s)
|
5
|
+
elsif detect_xml s
|
6
|
+
Hasher.from_xml(s)
|
7
|
+
elsif detect_hash s
|
8
|
+
Hasher.from_hash(s)
|
9
|
+
elsif detect_activerecord s
|
10
|
+
Hasher.from_activerecord(s)
|
11
|
+
else
|
12
|
+
"Type not supported or wrong input!"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.detect_xml s
|
17
|
+
if (s[0] == "<") then true
|
18
|
+
else
|
19
|
+
false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.detect_json s
|
24
|
+
i=0
|
25
|
+
while s[i]=="[" do
|
26
|
+
i=i+1
|
27
|
+
end
|
28
|
+
if s[i] == "{" && (s[i+1] == "\'" || s[i+1] == "\"")
|
29
|
+
aux = s.index(s[i+1],i+2)
|
30
|
+
aux = aux+1
|
31
|
+
while s[aux] == " " do
|
32
|
+
aux=aux+1
|
33
|
+
end
|
34
|
+
if s[aux] == ":"
|
35
|
+
true
|
36
|
+
else
|
37
|
+
false
|
38
|
+
end
|
39
|
+
else
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.detect_hash s
|
45
|
+
i=0
|
46
|
+
if s[i] == "{" && (s[i+1] == "\'" || s[i+1] == "\"" || s[i+1] == ":")
|
47
|
+
if s[i+1] == "\'" || s[i+1] == "\""
|
48
|
+
aux = s.index(s[i+1],i+2)
|
49
|
+
aux = aux+1
|
50
|
+
while s[aux] == " " do
|
51
|
+
aux=aux+1
|
52
|
+
end
|
53
|
+
if s[aux..aux+1] == "=>"
|
54
|
+
true
|
55
|
+
else
|
56
|
+
false
|
57
|
+
end
|
58
|
+
else
|
59
|
+
aux = s.index("=>",i+2)
|
60
|
+
!aux.nil?
|
61
|
+
end
|
62
|
+
else
|
63
|
+
false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.detect_activerecord s
|
68
|
+
i=0
|
69
|
+
i=i+1 if s[i]=="["
|
70
|
+
if s[i..i+1] == "#<"
|
71
|
+
true
|
72
|
+
else
|
73
|
+
false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class Hasher
|
2
|
+
|
3
|
+
def self.from_xml s
|
4
|
+
shash = ""
|
5
|
+
arbre = []
|
6
|
+
for i in 0..s.length-2
|
7
|
+
if (s[i] == ">")
|
8
|
+
if (s[i+1] == "<")
|
9
|
+
if (s[i+2] != "/") then shash << '"=>['
|
10
|
+
end
|
11
|
+
else shash << '"=>'
|
12
|
+
saux = s.clone
|
13
|
+
shash << saux.slice(i+1..(saux.index("<", i+2)-1))
|
14
|
+
end
|
15
|
+
elsif (s[i] == "<")
|
16
|
+
if (s[i+1] == "/")
|
17
|
+
saux = s.clone
|
18
|
+
if (s[saux.index(">", i+1)+1 .. saux.index(">", i+1)+2] == "</") then shash << '}]'
|
19
|
+
else shash << '}'
|
20
|
+
end
|
21
|
+
else shash << '{"'
|
22
|
+
saux = s.clone
|
23
|
+
shash << saux.slice(i+1..(saux.index(">", i+2)-1))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
shash
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.from_json s
|
31
|
+
s.gsub(":","=>")
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.from_hash s
|
35
|
+
s
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.from_activerecord s
|
39
|
+
records=[]
|
40
|
+
vector = false
|
41
|
+
if s[0]=="["
|
42
|
+
hash="["
|
43
|
+
s=s[1..s.length-2]
|
44
|
+
records=s.split(">, #<")
|
45
|
+
vector = true
|
46
|
+
else
|
47
|
+
hash=""
|
48
|
+
records[0]=s
|
49
|
+
end
|
50
|
+
records.each do |record|
|
51
|
+
record = record[1..record.length-1] if record[0]==" "
|
52
|
+
record = record[2..record.length-1] if record[0..1]=="#<"
|
53
|
+
record = record[0..record.length-2] if record[record.length-1]==">"
|
54
|
+
hash=hash+"{"
|
55
|
+
name_finishes=record.index(" ")
|
56
|
+
record_name=record[0..name_finishes-1 ]
|
57
|
+
hash=hash+"\"name\" => \"" + record_name +"\", \"record\" => {"
|
58
|
+
entries = record[name_finishes+1..record.length-1].split(",")
|
59
|
+
entries.each do |entry|
|
60
|
+
hash=hash+"\""+entry[0..entry.index(":")-1]+"\" => "+entry[entry.index(":")+2..entry.length-1]+"," if entry[entry.index(":")+2..entry.length-1] != "nil"
|
61
|
+
end
|
62
|
+
hash=hash[0..hash.length-2]
|
63
|
+
hash=hash+"}},"
|
64
|
+
end
|
65
|
+
hash=hash[0..hash.length-2]
|
66
|
+
hash=hash+"]" if vector
|
67
|
+
hash
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: format_data_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,14 +11,18 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-10-28 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: Detects input data(JSON,XML,CSV,
|
15
|
-
in a format chosen by the user (JSON,XML,CSV,Excel,YAML)
|
14
|
+
description: Detects input data(JSON,XML,CSV,Hash,Activerecord) and transforms it
|
15
|
+
in a format chosen by the user (JSON,XML,Hash,CSV,Excel,YAML)
|
16
16
|
email: alex.sirbu.best@gmail.com
|
17
17
|
executables: []
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- lib/format_data_converter.rb
|
22
|
+
- lib/format_data_converter/detector.rb
|
23
|
+
- lib/format_data_converter/converter.rb
|
24
|
+
- lib/format_data_converter/hasher.rb
|
25
|
+
- README.rdoc
|
22
26
|
homepage: http://rubygems.org/gems/format_data_converter
|
23
27
|
licenses: []
|
24
28
|
post_install_message:
|