activeresource-google_spreadsheets 0.0.5 → 0.0.6
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.
- checksums.yaml +4 -4
- data/lib/google_spreadsheets/enhanced/namespace_preservable.rb +8 -59
- data/lib/google_spreadsheets/enhanced/namespace_preservable/nokogiri_parser.rb +58 -0
- data/lib/google_spreadsheets/enhanced/namespace_preservable/rexml_parser.rb +52 -0
- data/lib/google_spreadsheets/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc8450a6fc8c17123d28bebb4e79a9b8489960ae
|
4
|
+
data.tar.gz: 234cc37021b32622df1d6c561d04cff6335fc4f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a8df26c102d2d96c2e895dbfec5c51a55372cd4fbfe04fcd2659c169f2eba6975427c80fea76f890fb5f86a347bb569bca3faec27e48d5dd8da908635f9aff9
|
7
|
+
data.tar.gz: 4fb29be10cc47974968510d35fd4fdd627d40fa6a38fdb00adb037003fe9a48abbbe305f24258a266a5e15fb7b3ddcaff993b8241c243d4f7f2f176dab6c825d
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'nokogiri'
|
2
|
-
|
3
1
|
module GoogleSpreadsheets
|
4
2
|
module Enhanced
|
5
3
|
module NamespacePreservable
|
@@ -22,57 +20,14 @@ module GoogleSpreadsheets
|
|
22
20
|
{ :namespaces => { 'gsx' => 'http://schemas.google.com/spreadsheets/2006/extended' } })
|
23
21
|
end
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
rescue Exception => e
|
34
|
-
# raise your custom exception here
|
35
|
-
raise
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def xml_node_to_hash(node)
|
40
|
-
# If we are at the root of the document, start the hash
|
41
|
-
if node.element?
|
42
|
-
result_hash = {}
|
43
|
-
|
44
|
-
node.attributes.each do |key, attr|
|
45
|
-
result_hash[attr.namespaced_name] = prepare(attr.value)
|
46
|
-
end
|
47
|
-
|
48
|
-
node.children.each do |child|
|
49
|
-
result = xml_node_to_hash(child)
|
50
|
-
if child.is_a? Nokogiri::XML::Text
|
51
|
-
unless child.next_sibling || child.previous_sibling
|
52
|
-
return prepare(result)
|
53
|
-
end
|
54
|
-
elsif result_hash[child_name = child.namespaced_name]
|
55
|
-
result_hash[child_name] = [result_hash[child_name]] unless result_hash[child_name].is_a?(Object::Array)
|
56
|
-
result_hash[child_name] << prepare(result)
|
57
|
-
else
|
58
|
-
result_hash[child_name] = prepare(result)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
result_hash
|
63
|
-
else
|
64
|
-
prepare(node.content.to_s)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def prepare(data)
|
69
|
-
if data.is_a?(String) && data.to_i.to_s == data
|
70
|
-
data.to_i
|
71
|
-
elsif data == {}
|
72
|
-
''
|
73
|
-
else
|
74
|
-
data
|
75
|
-
end
|
23
|
+
begin
|
24
|
+
require 'nokogiri'
|
25
|
+
require 'google_spreadsheets/enhanced/namespace_preservable/nokogiri_parser'
|
26
|
+
include NokogiriParser
|
27
|
+
rescue LoadError => e
|
28
|
+
$stderr.puts "You don't have nokogiri installed in your application, so this runs with rexml. If you want to use nokogiri, please add it to your Gemfile and run bundle install"
|
29
|
+
require 'google_spreadsheets/enhanced/namespace_preservable/rexml_parser'
|
30
|
+
include RexmlParser
|
76
31
|
end
|
77
32
|
end
|
78
33
|
|
@@ -126,9 +81,3 @@ module GoogleSpreadsheets
|
|
126
81
|
end
|
127
82
|
end
|
128
83
|
end
|
129
|
-
|
130
|
-
class Nokogiri::XML::Node
|
131
|
-
def namespaced_name
|
132
|
-
(namespace.try(:prefix).present? ? "#{namespace.prefix}:" : '') + name
|
133
|
-
end
|
134
|
-
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module GoogleSpreadsheets
|
2
|
+
module Enhanced
|
3
|
+
module NamespacePreservable
|
4
|
+
module NokogiriParser
|
5
|
+
# convert to Hash from XML including namespace
|
6
|
+
# https://gist.github.com/baroquebobcat/1603671
|
7
|
+
def hash_from_xml_with_namespace(xml_io)
|
8
|
+
result = Nokogiri::XML(xml_io)
|
9
|
+
{ result.root.name => xml_node_to_hash(result.root) }
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def xml_node_to_hash(node)
|
14
|
+
# If we are at the root of the document, start the hash
|
15
|
+
if node.element?
|
16
|
+
result_hash = {}
|
17
|
+
|
18
|
+
node.attributes.each do |key, attr|
|
19
|
+
result_hash[namespaced_name_from_node(attr)] = prepare(attr.value)
|
20
|
+
end
|
21
|
+
|
22
|
+
node.children.each do |child|
|
23
|
+
result = xml_node_to_hash(child)
|
24
|
+
if child.is_a? Nokogiri::XML::Text
|
25
|
+
unless child.next_sibling || child.previous_sibling
|
26
|
+
return prepare(result)
|
27
|
+
end
|
28
|
+
elsif result_hash[child_name = namespaced_name_from_node(child)]
|
29
|
+
result_hash[child_name] = [result_hash[child_name]] unless result_hash[child_name].is_a?(::Array)
|
30
|
+
result_hash[child_name] << prepare(result)
|
31
|
+
else
|
32
|
+
result_hash[child_name] = prepare(result)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
result_hash
|
37
|
+
else
|
38
|
+
prepare(node.content.to_s)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def prepare(data)
|
43
|
+
if data.is_a?(String) && data.to_i.to_s == data
|
44
|
+
data.to_i
|
45
|
+
elsif data == {}
|
46
|
+
''
|
47
|
+
else
|
48
|
+
data
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def namespaced_name_from_node(node)
|
53
|
+
(node.namespace.try(:prefix).present? ? "#{node.namespace.prefix}:" : '') + node.name
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rexml/document'
|
2
|
+
|
3
|
+
module GoogleSpreadsheets
|
4
|
+
module Enhanced
|
5
|
+
module NamespacePreservable
|
6
|
+
module RexmlParser
|
7
|
+
def hash_from_xml_with_namespace(xml_io)
|
8
|
+
rexml = REXML::Document.new(xml_io)
|
9
|
+
{ rexml.root.name => xml_node_to_hash(rexml.root) }
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def xml_node_to_hash(node)
|
14
|
+
if node.node_type == :element
|
15
|
+
result_hash = {}
|
16
|
+
|
17
|
+
node.attributes.each do |name, attr|
|
18
|
+
result_hash[name] = prepare(attr)
|
19
|
+
end
|
20
|
+
|
21
|
+
node.each_child do |child|
|
22
|
+
result = xml_node_to_hash(child)
|
23
|
+
if child.node_type == :text
|
24
|
+
unless child.next_sibling_node || child.previous_sibling_node
|
25
|
+
return prepare(result)
|
26
|
+
end
|
27
|
+
elsif result_hash[child_name = child.expanded_name]
|
28
|
+
result_hash[child_name] = [result_hash[child_name]] unless result_hash[child_name].is_a?(Array)
|
29
|
+
result_hash[child_name] << prepare(result)
|
30
|
+
else
|
31
|
+
result_hash[child_name] = prepare(result)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
result_hash
|
35
|
+
else
|
36
|
+
prepare(node.value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def prepare(data)
|
41
|
+
if data.is_a?(String) && data.to_i.to_s == data
|
42
|
+
data.to_i
|
43
|
+
elsif data == {}
|
44
|
+
''
|
45
|
+
else
|
46
|
+
data
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeresource-google_spreadsheets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chihiro Ito
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activeresource
|
@@ -87,6 +87,8 @@ files:
|
|
87
87
|
- lib/google_spreadsheets/enhanced.rb
|
88
88
|
- lib/google_spreadsheets/enhanced/collection.rb
|
89
89
|
- lib/google_spreadsheets/enhanced/namespace_preservable.rb
|
90
|
+
- lib/google_spreadsheets/enhanced/namespace_preservable/nokogiri_parser.rb
|
91
|
+
- lib/google_spreadsheets/enhanced/namespace_preservable/rexml_parser.rb
|
90
92
|
- lib/google_spreadsheets/enhanced/row.rb
|
91
93
|
- lib/google_spreadsheets/enhanced/spreadsheet.rb
|
92
94
|
- lib/google_spreadsheets/enhanced/syncing.rb
|