kataba 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/kataba.rb +121 -0
  3. metadata +59 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd9fd6f5ee1170b5552d4345a4c23fab923efcaa
4
+ data.tar.gz: e78ca648b702ecd40a576263eb6aaab7d4fd4d13
5
+ SHA512:
6
+ metadata.gz: 13ed9bbd4e28ee1e7e8e955f6ea9084b52e94da83458d395fb776a75b40094079e640bdb82c7424eca93dc27b1063968b3fb96791723d1a9d432a29837a18362
7
+ data.tar.gz: 46f8e1813d56ecdd4cd689bd5ee2354d250861f1b627cd0812fed4ed36b4ccab8aa8313a779416e0ee69e12fb08c048c7f4bd109d2f626af1e20ac49c426ac5c
@@ -0,0 +1,121 @@
1
+ require 'nokogiri'
2
+ require 'tmpdir'
3
+ require 'digest/md5'
4
+ require 'open-uri'
5
+
6
+ module Kataba
7
+
8
+ class << self
9
+ attr_accessor :configuration
10
+ end
11
+
12
+ def self.configure
13
+ yield(configuration) if block_given?
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.reset
21
+ @configuration = Configuration.new
22
+ end
23
+
24
+ class Configuration
25
+ attr_accessor :offline_storage
26
+ attr_accessor :mirror_list
27
+
28
+ def initialize
29
+ @offline_storage = "#{Dir.tmpdir}/kataba"
30
+ end
31
+ end
32
+
33
+ def self.fetch_schema(xsd_uri)
34
+ uri_md5 = Digest::MD5.hexdigest(xsd_uri)
35
+ dir_path = "#{self.configuration.offline_storage}"
36
+ xsd_path = "#{dir_path}/#{uri_md5}.xsd"
37
+
38
+ # Does the offline version exist already?
39
+ if !(File.exists?(xsd_path))
40
+ # If not, go download
41
+ xsd_array = []
42
+ xsd_array << xsd_uri
43
+ download_xsd(xsd_array)
44
+ end
45
+
46
+ # Validate and return Nokogiri schema
47
+ Dir.chdir(dir_path) do
48
+ return Nokogiri::XML::Schema(IO.read(xsd_path))
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def self.download_xsd(xsd_uri_array)
55
+ new_xsd_uris = []
56
+ file_paths = []
57
+
58
+ # Download files
59
+ xsd_uri_array.each do |xsd_uri|
60
+ uri_md5 = Digest::MD5.hexdigest(xsd_uri)
61
+
62
+ dir_name = "#{self.configuration.offline_storage}"
63
+
64
+ # Make dir if needed
65
+ unless File.directory?(dir_name)
66
+ FileUtils.mkdir_p(dir_name)
67
+ end
68
+
69
+ file_path = "#{dir_name}/#{uri_md5}.xsd"
70
+
71
+ file_paths << file_path
72
+
73
+ open(file_path, "wb+") do |file|
74
+ if !self.configuration.mirror_list.to_s.empty?
75
+ mirror_list = YAML.load_file(self.configuration.mirror_list)
76
+ mirror = mirror_list[xsd_uri]
77
+ if mirror.to_s.empty?
78
+ # No mirror for that uri
79
+ file.write(open(xsd_uri).read)
80
+ else
81
+ file.write(open(mirror).read)
82
+ end
83
+ else
84
+ file.write(open(xsd_uri).read)
85
+ end
86
+ end
87
+ end
88
+
89
+ # Search inside for other schemaLocations
90
+ file_paths.each do |file_path|
91
+ new_xsd_uris = find_schemas(file_path)
92
+ end
93
+
94
+ if !new_xsd_uris.reject(&:empty?).empty?
95
+ download_xsd(new_xsd_uris)
96
+ end
97
+ end
98
+
99
+ def self.find_schemas(xml_file_path)
100
+ xsd_uri_array = []
101
+
102
+ # Open XML file
103
+ doc = File.open(xml_file_path) { |f| Nokogiri::XML(f) }
104
+ # search for schemaLocations
105
+ doc.xpath("//@schemaLocation").each do |node|
106
+ if !node.value.to_s.empty?
107
+ # Add to array
108
+ xsd_uri_array << node.value
109
+ # Get MD5
110
+ uri_md5 = Digest::MD5.hexdigest(node.value)
111
+ # Reassign attribute value
112
+ node.value = "#{uri_md5}.xsd"
113
+ end
114
+ end
115
+
116
+ # Overwrite with md5'd doc
117
+ File.write(xml_file_path, doc.to_xml)
118
+
119
+ return xsd_uri_array
120
+ end
121
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kataba
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Cliff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ description: A simple gem that allows for the functionality that an XML catalog would
28
+ provide
29
+ email: d.cliff@northeastern.edu
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/kataba.rb
35
+ homepage: http://rubygems.org/gems/kataba
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.2.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: XML Schema Definition (XSD) mirroring and offline validation for Nokogiri
59
+ test_files: []