format_data_converter 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,14 @@ require "format_data_converter/hasher"
4
4
 
5
5
  class FDC
6
6
 
7
+ def self.detect_and_to_yaml s
8
+ Converter.to_yaml(Detector.detect s)
9
+ end
10
+
11
+ def self.detect_and_to_csv s
12
+ Converter.to_csv(Detector.detect s)
13
+ end
14
+
7
15
  def self.detect_and_to_json s
8
16
  Converter.to_json(Detector.detect s)
9
17
  end
@@ -15,6 +23,15 @@ class FDC
15
23
  def self.detect_and_to_hash s
16
24
  Converter.to_hash_symbols(Detector.detect s)
17
25
  end
26
+
27
+ def self.detect_and_to_xml s
28
+ Converter.to_xml(Detector.detect s)
29
+ end
18
30
 
31
+ def self.detect_and_to_xls s
32
+ Converter.to_xls(Detector.detect s)
33
+ end
34
+
35
+
19
36
  end
20
37
 
@@ -1,5 +1,76 @@
1
1
  class Converter
2
2
 
3
+ def self.to_yaml s
4
+ "Feature not implemented yet!"
5
+ end
6
+
7
+ def self.to_csv s
8
+ s=stringify(eval(s)).to_s
9
+ lines=[]
10
+ items=[]
11
+ vari1=""
12
+ vari2=""
13
+ s=stringify(eval(s)).to_s
14
+ s=s.gsub("\"",'')
15
+ s=s.gsub("[",'')
16
+ s=s.gsub("]",'')
17
+ s=s.gsub("}",'')
18
+ s=s.gsub("=>",',')
19
+ lines=s.split("{")
20
+ items=lines[1].split(",").map{|word| word+=","}
21
+ for i in 0..items.length/2-1
22
+ vari1+=items[i*2]
23
+ vari2+=items[i*2+1]
24
+ end
25
+ vari1=vari1[0..-2]
26
+ vari2=vari2[0..-2]
27
+ s=vari1+"\n"+vari2
28
+ vari1=""
29
+ for i in 2..lines.length-1
30
+ items=lines[i].split(",").map{|word| word+=","}
31
+ for j in 0..items.length/2-1
32
+ vari1+=items[j*2+1]
33
+ end
34
+ vari1=vari1[0..-2]
35
+ s+="\n"+vari1
36
+ end
37
+ end
38
+
39
+ def self.to_xls s
40
+ s=stringify(eval(s)).to_s
41
+ lines=[]
42
+ columns=[]
43
+ cols=0
44
+ rows=0
45
+ s=s.gsub("\"",'')
46
+ s=s.gsub("[",'')
47
+ s=s.gsub("]",'')
48
+ s=s.gsub("}",'')
49
+ s=s.gsub("=>",',')
50
+ lines=s.split("{")
51
+ rows=lines.length
52
+ columns=lines[1].split(",")
53
+ cols=columns.length/2
54
+ s="<?xml version=\"1.0\"?><Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\nxmlns:o=\"urn:schemas-microsoft-com:office:office\"\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\nxmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n xmlns:html=\"http://www.w3.org/TR/REC-html40\"><Worksheet ss:Name=\"Sheet 1\"><Table ss:ExpandedColumnCount=\""+cols.to_s+"\" ss:ExpandedRowCount=\""+rows.to_s+"\" x:FullColumns=\"1\" x:FullRows=\"1\"><Row>"
55
+ for i in 0..columns.length/2-1
56
+ s+="<Cell><Data ss:Type=\"String\">"+columns[i*2]+"</Data></Cell>"
57
+ end
58
+ s+="</Row><Row>"
59
+ for i in 0..columns.length/2-1
60
+ s+="<Cell><Data ss:Type=\"String\">"+columns[i*2+1]+"</Data></Cell>"
61
+ end
62
+ s+="</Row>"
63
+ for i in 2..lines.length-1
64
+ columns=lines[i].split(",")
65
+ s+="<Row>"
66
+ for j in 0..columns.length/2-1
67
+ s+="<Cell><Data ss:Type=\"String\">"+columns[j*2+1]+"</Data></Cell>"
68
+ end
69
+ s+="</Row></Table></Worksheet></Workbook>"
70
+ end
71
+ puts s
72
+ end
73
+
3
74
  def self.to_xml s
4
75
  s=stringify(eval(s)).to_s
5
76
  n = 0
@@ -8,11 +8,24 @@ class Detector
8
8
  Hasher.from_hash(s)
9
9
  elsif detect_activerecord s
10
10
  Hasher.from_activerecord(s)
11
+ elsif detect_csv
12
+ Hasher.from_csv(s)
11
13
  else
12
14
  "Type not supported or wrong input!"
13
15
  end
14
16
  end
15
17
 
18
+ def self.detect_csv s
19
+ str=s
20
+ lines=str.split("\n")
21
+ return false if lines.length < 2
22
+ counter=lines[0].split(",").length
23
+ for i in 1..lines.length-1
24
+ return false if lines[i].split(",").length != counter
25
+ end
26
+ true
27
+ end
28
+
16
29
  def self.detect_xml s
17
30
  if (s[0] == "<") then true
18
31
  else
@@ -1,5 +1,25 @@
1
1
  class Hasher
2
2
 
3
+ def self.from_csv s
4
+ lines=[]
5
+ cols=[]
6
+ items=[]
7
+ vari=""
8
+ s=s.gsub("\"",'')
9
+ puts s
10
+ lines=s.split("\n")
11
+ cols=lines[0].split(",").map{|word| word="\""+word+"\"=>"}
12
+ for i in 1..lines.length-1
13
+ items=lines[i].split(",").map{|word| word="\""+word+"\","}
14
+ var=items[-1]
15
+ items[-1]=var[0..-2]+"},{"
16
+ for j in 0..cols.length-1
17
+ vari+=cols[j]+items[j]
18
+ end
19
+ end
20
+ s="[{"+vari[0..-3]+"]"
21
+ end
22
+
3
23
  def self.from_xml s
4
24
  shash = ""
5
25
  arbre = []
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.6.0
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: