dwc-archive 0.5.9 → 0.5.10
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/dwc-archive.rb +1 -0
- data/lib/dwc-archive/archive.rb +2 -2
- data/lib/dwc-archive/xml_reader.rb +64 -0
- metadata +7 -8
- data/.gitignore +0 -24
- data/lib/ruby_extensions.rb +0 -67
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.10
|
data/lib/dwc-archive.rb
CHANGED
data/lib/dwc-archive/archive.rb
CHANGED
@@ -8,8 +8,8 @@ class DarwinCore
|
|
8
8
|
@expander = DarwinCore::Expander.new(@archive_path, @tmp_dir)
|
9
9
|
@expander.unpack
|
10
10
|
if valid?
|
11
|
-
@meta =
|
12
|
-
@eml = files.include?("eml.xml") ?
|
11
|
+
@meta = DarwinCore::XmlReader.from_xml(open(File.join(@expander.path, 'meta.xml')))
|
12
|
+
@eml = files.include?("eml.xml") ? DarwinCore::XmlReader.from_xml(open(File.join(@expander.path, 'eml.xml'))) : nil
|
13
13
|
else
|
14
14
|
clean
|
15
15
|
raise InvalidArchiveError
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# USAGE: Hash.from_xml:(YOUR_XML_STRING)
|
2
|
+
require 'nokogiri'
|
3
|
+
# modified from http://stackoverflow.com/questions/1230741/convert-a-nokogiri-document-to-a-ruby-hash/1231297#1231297
|
4
|
+
class DarwinCore
|
5
|
+
module XmlReader
|
6
|
+
class << self
|
7
|
+
def from_xml(xml_io)
|
8
|
+
begin
|
9
|
+
result = Nokogiri::XML(xml_io)
|
10
|
+
return { result.root.name.to_sym => xml_node_to_hash(result.root)}
|
11
|
+
rescue Exception => e
|
12
|
+
raise e
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def xml_node_to_hash(node)
|
19
|
+
# If we are at the root of the document, start the hash
|
20
|
+
if node.element?
|
21
|
+
result_hash = {}
|
22
|
+
if node.attributes != {}
|
23
|
+
result_hash[:attributes] = {}
|
24
|
+
node.attributes.keys.each do |key|
|
25
|
+
result_hash[:attributes][node.attributes[key].name.to_sym] = prepare(node.attributes[key].value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
if node.children.size > 0
|
29
|
+
node.children.each do |child|
|
30
|
+
result = xml_node_to_hash(child)
|
31
|
+
|
32
|
+
if child.name == "text"
|
33
|
+
unless child.next_sibling || child.previous_sibling
|
34
|
+
return prepare(result)
|
35
|
+
end
|
36
|
+
elsif result_hash[child.name.to_sym]
|
37
|
+
if result_hash[child.name.to_sym].is_a?(Object::Array)
|
38
|
+
result_hash[child.name.to_sym] << prepare(result)
|
39
|
+
else
|
40
|
+
result_hash[child.name.to_sym] = [result_hash[child.name.to_sym]] << prepare(result)
|
41
|
+
end
|
42
|
+
else
|
43
|
+
result_hash[child.name.to_sym] = prepare(result)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
return result_hash
|
48
|
+
else
|
49
|
+
return result_hash
|
50
|
+
end
|
51
|
+
else
|
52
|
+
return prepare(node.content.to_s)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def prepare(data)
|
57
|
+
return data if data.class != String
|
58
|
+
return true if data.strip == "true"
|
59
|
+
return false if data.strip == "false"
|
60
|
+
data.to_i.to_s == data ? data.to_i : data
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dwc-archive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 10
|
10
|
+
version: 0.5.10
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dmitry Mozzherin
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-13 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -75,7 +75,6 @@ extra_rdoc_files:
|
|
75
75
|
- README.rdoc
|
76
76
|
files:
|
77
77
|
- .document
|
78
|
-
- .gitignore
|
79
78
|
- LICENSE
|
80
79
|
- README.rdoc
|
81
80
|
- Rakefile
|
@@ -99,7 +98,7 @@ files:
|
|
99
98
|
- lib/dwc-archive/ingester.rb
|
100
99
|
- lib/dwc-archive/metadata.rb
|
101
100
|
- lib/dwc-archive/utf_regex_ruby18.rb
|
102
|
-
- lib/
|
101
|
+
- lib/dwc-archive/xml_reader.rb
|
103
102
|
- spec/files/broken.tar.gz
|
104
103
|
- spec/files/data.tar.gz
|
105
104
|
- spec/files/data.zip
|
@@ -123,8 +122,8 @@ homepage: http://github.com/GlobalNamesArchitecture/dwc-archive
|
|
123
122
|
licenses: []
|
124
123
|
|
125
124
|
post_install_message:
|
126
|
-
rdoc_options:
|
127
|
-
|
125
|
+
rdoc_options: []
|
126
|
+
|
128
127
|
require_paths:
|
129
128
|
- lib
|
130
129
|
required_ruby_version: !ruby/object:Gem::Requirement
|
data/.gitignore
DELETED
data/lib/ruby_extensions.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
# USAGE: Hash.from_xml:(YOUR_XML_STRING)
|
2
|
-
require 'nokogiri'
|
3
|
-
# modified from http://stackoverflow.com/questions/1230741/convert-a-nokogiri-document-to-a-ruby-hash/1231297#1231297
|
4
|
-
|
5
|
-
class Hash
|
6
|
-
class << self
|
7
|
-
def from_xml(xml_io)
|
8
|
-
begin
|
9
|
-
result = Nokogiri::XML(xml_io)
|
10
|
-
return { result.root.name.to_sym => xml_node_to_hash(result.root)}
|
11
|
-
rescue Exception => e
|
12
|
-
raise e
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def xml_node_to_hash(node)
|
17
|
-
# If we are at the root of the document, start the hash
|
18
|
-
if node.element?
|
19
|
-
result_hash = {}
|
20
|
-
if node.attributes != {}
|
21
|
-
result_hash[:attributes] = {}
|
22
|
-
node.attributes.keys.each do |key|
|
23
|
-
result_hash[:attributes][node.attributes[key].name.to_sym] = prepare(node.attributes[key].value)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
if node.children.size > 0
|
27
|
-
node.children.each do |child|
|
28
|
-
result = xml_node_to_hash(child)
|
29
|
-
|
30
|
-
if child.name == "text"
|
31
|
-
unless child.next_sibling || child.previous_sibling
|
32
|
-
return prepare(result)
|
33
|
-
end
|
34
|
-
elsif result_hash[child.name.to_sym]
|
35
|
-
if result_hash[child.name.to_sym].is_a?(Object::Array)
|
36
|
-
result_hash[child.name.to_sym] << prepare(result)
|
37
|
-
else
|
38
|
-
result_hash[child.name.to_sym] = [result_hash[child.name.to_sym]] << prepare(result)
|
39
|
-
end
|
40
|
-
else
|
41
|
-
result_hash[child.name.to_sym] = prepare(result)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
return result_hash
|
46
|
-
else
|
47
|
-
return result_hash
|
48
|
-
end
|
49
|
-
else
|
50
|
-
return prepare(node.content.to_s)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def prepare(data)
|
55
|
-
return data if data.class != String
|
56
|
-
return true if data.strip == "true"
|
57
|
-
return false if data.strip == "false"
|
58
|
-
data.to_i.to_s == data ? data.to_i : data
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def to_struct(struct_name)
|
63
|
-
Struct.new(struct_name,*keys).new(*values)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
|