dynarex 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/dynarex.rb +27 -0
  2. data/lib/dynarex2.rb +163 -0
  3. metadata +2 -1
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  require 'rexml/document'
4
+ require 'nokogiri'
4
5
  require 'open-uri'
5
6
  require 'builder'
6
7
 
@@ -19,6 +20,32 @@ class Dynarex
19
20
  @records
20
21
  end
21
22
 
23
+ def to_s
24
+ xsl_buffer =<<EOF
25
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
26
+ <xsl:output encoding="UTF-8"
27
+ method="text"
28
+ indent="no"
29
+ omit-xml-declaration="yes"/>
30
+
31
+ <xsl:template match="*">
32
+ <xsl:for-each select="records/*">[!regex_values]<xsl:text>
33
+ </xsl:text>
34
+ </xsl:for-each>
35
+ </xsl:template>
36
+ </xsl:stylesheet>
37
+ EOF
38
+
39
+ format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
40
+
41
+ xslt_format = format_mask.to_s.gsub(/\s(?=\[!\w+\])/,'<xsl:text> </xsl:text>').gsub(/\[!(\w+)\]/, '<xsl:value-of select="\1"/>')
42
+ xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
43
+
44
+ xslt = Nokogiri::XSLT(xsl_buffer)
45
+ out = xslt.transform(Nokogiri::XML(@doc.to_s))
46
+ out.text
47
+ end
48
+
22
49
  def to_xml
23
50
  display_xml()
24
51
  end
@@ -0,0 +1,163 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'rexml/document'
4
+ require 'nokogiri'
5
+ require 'open-uri'
6
+ require 'builder'
7
+
8
+ class Dynarex
9
+ include REXML
10
+
11
+ def initialize(location)
12
+ open(location)
13
+ end
14
+
15
+ def summary
16
+ @summary
17
+ end
18
+
19
+ def records
20
+ @records
21
+ end
22
+
23
+ def to_s
24
+ xsl_buffer =<<EOF
25
+ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
26
+ <xsl:output encoding="UTF-8"
27
+ method="text"
28
+ indent="no"
29
+ omit-xml-declaration="yes"/>
30
+
31
+ <xsl:template match="*">
32
+ <xsl:for-each select="records/*">[!regex_values]<xsl:text>
33
+ </xsl:text>
34
+ </xsl:for-each>
35
+ </xsl:template>
36
+ </xsl:stylesheet>
37
+ EOF
38
+
39
+ format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
40
+
41
+ xslt_format = format_mask.to_s.gsub(/\s(?=\[!\w+\])/,'<xsl:text> </xsl:text>').gsub(/\[!(\w+)\]/, '<xsl:value-of select="\1"/>')
42
+ xsl_buffer.sub!(/\[!regex_values\]/, xslt_format)
43
+
44
+ xslt = Nokogiri::XSLT(xsl_buffer)
45
+ out = xslt.transform(Nokogiri::XML(@doc.to_s))
46
+ out.text
47
+ end
48
+
49
+ def to_xml
50
+ display_xml()
51
+ end
52
+
53
+ def save(filepath)
54
+ xml = display_xml()
55
+ File.open(filepath,'w'){|f| f.write xml}
56
+ end
57
+
58
+ def parse(buffer)
59
+ format_mask = XPath.first(@doc.root, 'summary/format_mask/text()').to_s
60
+ t = format_mask.to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
61
+ lines = buffer.split(/\r?\n|\r(?!\n)/).map {|x|x.match(/#{t}/).captures}
62
+ fields = format_mask.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
63
+
64
+ a = lines.each_with_index.map do|x,i|
65
+ created = Time.now.to_s; id = Time.now.strftime("%Y%m%I%H%M%S") + i.to_s
66
+ h = Hash[fields.zip(x)]
67
+ [h[@default_key], {id: id, created: created, last_modified: '', body: h}]
68
+ end
69
+
70
+ h2 = Hash[a]
71
+
72
+ #replace the existing records hash
73
+ h = @records
74
+ h2.each do |key,item|
75
+ if h.has_key? key then
76
+
77
+ # overwrite the previous item and change the timestamps
78
+ h[key][:last_modified] = item[:created]
79
+ item[:body].each do |k,v|
80
+ h[key][:body][k.to_sym] = v
81
+ end
82
+ else
83
+
84
+ h[key] = item.clone
85
+ end
86
+ end
87
+
88
+ h.each {|key, item| h.delete(key) if not h2.has_key? key}
89
+ self
90
+ end
91
+
92
+ private
93
+
94
+ def display_xml
95
+
96
+ xml = Builder::XmlMarkup.new( :target => buffer='', :indent => 2 )
97
+ xml.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
98
+
99
+ xml.send @root_name do
100
+ xml.summary do
101
+ @summary.each{|key,value| xml.send key, value}
102
+ end
103
+ xml.records do
104
+ @records.each do |k, item|
105
+ xml.send(@item_name, id: item[:id], created: item[:created], \
106
+ last_modified: item[:last_modified]) do
107
+ item[:body].each{|name,value| xml.send name, value}
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ buffer
114
+
115
+ end
116
+
117
+ def open(location)
118
+ if location[/^https?:\/\//] then
119
+ buffer = Kernel.open(location, 'UserAgent' => 'Dynarex-Reader').read
120
+ elsif location[/\</]
121
+ buffer = location
122
+ else
123
+ buffer = File.open(location,'r').read
124
+ end
125
+ @doc = Document.new buffer
126
+ @default_key = (XPath.first(@doc.root, 'summary/default_key/text()') || \
127
+ XPath.first(@doc.root, 'records/*[1]/*[1]').name).to_s.to_sym
128
+
129
+ @summary = summary_to_h
130
+ @records = records_to_h
131
+ @root_name = @doc.root.name
132
+ @item_name = XPath.first(@doc.root, 'records/*[1]').name
133
+ end
134
+
135
+ def display()
136
+ puts @doc.to_s
137
+ end
138
+
139
+ def records_to_h()
140
+ ah = XPath.match(@doc.root, 'records/*').each_with_index.map do |row,i|
141
+ created = Time.now.to_s; id = Time.now.strftime("%Y%m%I%H%M%S") + i.to_s
142
+ last_modified = ''
143
+ id = row.attribute('id').value.to_s if row.attribute('id')
144
+ created = row.attribute('created').value.to_s if row.attribute('created')
145
+ last_modified = row.attribute('last_modified').value.to_s if row.attribute('last_modified')
146
+ body = XPath.match(row, '*').inject({}) do |r,node|
147
+ r[node.name.to_s.to_sym] = node.text.to_s
148
+ r
149
+ end
150
+ [body[@default_key],{id: id, created: created, last_modified: \
151
+ last_modified, body: body}]
152
+ end
153
+ Hash[*ah.flatten]
154
+ end
155
+
156
+ def summary_to_h
157
+ XPath.match(@doc.root, 'summary/*').inject({}) do |r,node|
158
+ r[node.name.to_s.to_sym] = node.text.to_s
159
+ r
160
+ end
161
+ end
162
+
163
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynarex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -23,6 +23,7 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - lib/dynarex.rb
26
+ - lib/dynarex2.rb
26
27
  has_rdoc: true
27
28
  homepage:
28
29
  licenses: []