relaton-index 0.1.3 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.adoc +25 -9
- data/lib/relaton/index/file_io.rb +8 -5
- data/lib/relaton/index/pool.rb +7 -2
- data/lib/relaton/index/type.rb +9 -2
- data/lib/relaton/index/version.rb +1 -1
- data/lib/relaton/index.rb +3 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1f22ce8f232dc77b5b791d5eaa6fb5b80bf62697f8a1a7cd9d18e7515ff7d65
|
4
|
+
data.tar.gz: 86884150296d335342dba8ebf3c2808f662fd08709378ce9674e91965494c600
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c59bccab496f4be0a8a7d228dcb676c148daf9e6d84308623d1877bd0fe85a6738a5f151219f787385ac53faae430f43bfa928b40e2e399d62082abcfb86600
|
7
|
+
data.tar.gz: 33ef8426b9e9e72cfb2488d78ef16d65e5083d3a50376b9e530d95b213744c69763d9fa2fa4c39395cd3428fdf7e8954ea3a47047c2ce029b2cdbb580ff406a0
|
data/README.adoc
CHANGED
@@ -22,18 +22,33 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
22
22
|
|
23
23
|
== Usage
|
24
24
|
|
25
|
+
[source,ruby]
|
26
|
+
----
|
27
|
+
require 'relaton/index'
|
28
|
+
|
29
|
+
# Create a new index object. The first argument is the type of dataset (ISO, IEC, IHO, etc.) URL and filename are optional.
|
30
|
+
Relaton::Index.find_or_create :IHO, url: "https://raw.githubusercontent.com/relaton/relaton-data-iho/master/index.zip", filename: "index-iho.yaml"
|
31
|
+
|
32
|
+
# Find an existing index object (created before).
|
33
|
+
Relaton::Index.find_or_create :IHO
|
34
|
+
|
35
|
+
# If a new value of URL or filename is specified and has a new value (different from what it was before), the index object will be recreated.
|
36
|
+
Relaton::Index.find_or_create :IHO, url: nil, filename: "index.yaml"
|
37
|
+
|
38
|
+
# Remove the index from the pool.
|
39
|
+
Relaton::Index.close :IHO
|
40
|
+
----
|
41
|
+
|
25
42
|
The aim of this gem is to be used by Relaton libraries in two ways:
|
26
43
|
- indexing documents' files in GitHub repositories
|
27
44
|
- searching a document by a reference when the Relaton library fetches a document
|
28
45
|
|
29
46
|
=== Indexing
|
30
47
|
|
31
|
-
In this case, the Relaton library creates an index object and adds documents' files to it. By default, the index object is saved to the `index.yaml` file in the root of the repository. The filename can be changed using the `filename` setting
|
48
|
+
In this case, the Relaton library creates an index object and adds documents' files to it. By default, the index object is saved to the `index.yaml` file in the root of the repository. The filename can be changed using the `filename` setting.
|
32
49
|
|
33
50
|
[source,ruby]
|
34
51
|
----
|
35
|
-
require 'relaton/index'
|
36
|
-
|
37
52
|
# Create a new index object or fetch an existing one. The first argument is the type of dataset (ISO, IEC, IHO, etc.) URL should not be specified.
|
38
53
|
index = Relaton::Index.find_or_create :IHO
|
39
54
|
|
@@ -50,9 +65,7 @@ In this case, the Relaton library should create an index object and search for a
|
|
50
65
|
|
51
66
|
[source,ruby]
|
52
67
|
----
|
53
|
-
|
54
|
-
|
55
|
-
# Create a new index object or fetch an existing one. URL should be specified. If the index file is not found or is older than 24 hours, it will be downloaded from the URL. By default, the index file is saved to the `/[HOME]/.relaton/iho/index.yaml` file.
|
68
|
+
# Create a new index object or fetch an existing one. URL should be specified. If the index file is not found or is older than 24 hours, it will be downloaded from the URL. By default, the index is saved as `index.yaml` file to the `/[HOME]/.relaton/iho/` folder.
|
56
69
|
index = Relaton::Index.find_or_create :IHO, url: "https://raw.githubusercontent.com/relaton/relaton-data-iho/master/index.zip"
|
57
70
|
|
58
71
|
|
@@ -66,9 +79,6 @@ index.search do |row|
|
|
66
79
|
row[:id] == "B-4"
|
67
80
|
end
|
68
81
|
# => [{ id: "B-4 2.19.0", file: "data/b-4_2_19_0.xml" }]
|
69
|
-
|
70
|
-
# Remove the index from the pool.
|
71
|
-
Relaton::Index.close :IHO
|
72
82
|
----
|
73
83
|
|
74
84
|
=== Configuration
|
@@ -88,6 +98,12 @@ Relaton::Index.config do |config|
|
|
88
98
|
end
|
89
99
|
----
|
90
100
|
|
101
|
+
It's also possible to redefine file name for a specific type of index:
|
102
|
+
[source,ruby]
|
103
|
+
----
|
104
|
+
index = Relaton::Index.find_or_create :IHO, filename: "index-iho.yaml"
|
105
|
+
----
|
106
|
+
|
91
107
|
== Development
|
92
108
|
|
93
109
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -6,6 +6,8 @@ module Relaton
|
|
6
6
|
# In index mode url should be nil.
|
7
7
|
#
|
8
8
|
class FileIO
|
9
|
+
attr_reader :url
|
10
|
+
|
9
11
|
#
|
10
12
|
# Initialize FileIO
|
11
13
|
#
|
@@ -14,9 +16,10 @@ module Relaton
|
|
14
16
|
# (if not exists, or older than 24 hours) or nil if index is used to
|
15
17
|
# index files
|
16
18
|
#
|
17
|
-
def initialize(dir, url)
|
19
|
+
def initialize(dir, url, filename)
|
18
20
|
@dir = dir
|
19
21
|
@url = url
|
22
|
+
@filename = filename
|
20
23
|
end
|
21
24
|
|
22
25
|
#
|
@@ -25,11 +28,11 @@ module Relaton
|
|
25
28
|
# @return [Array<Hash>] index
|
26
29
|
#
|
27
30
|
def read
|
28
|
-
if
|
29
|
-
@file ||= File.join(Index.config.storage_dir, ".relaton", @dir,
|
31
|
+
if url
|
32
|
+
@file ||= File.join(Index.config.storage_dir, ".relaton", @dir, @filename)
|
30
33
|
check_file || fetch_and_save
|
31
34
|
else
|
32
|
-
@file ||=
|
35
|
+
@file ||= @filename
|
33
36
|
read_file
|
34
37
|
end
|
35
38
|
end
|
@@ -64,7 +67,7 @@ module Relaton
|
|
64
67
|
# @return [Array<Hash>] index
|
65
68
|
#
|
66
69
|
def fetch_and_save
|
67
|
-
resp = URI(
|
70
|
+
resp = URI(url).open
|
68
71
|
zip = Zip::InputStream.new resp
|
69
72
|
entry = zip.get_next_entry
|
70
73
|
index = YAML.safe_load(entry.get_input_stream.read, permitted_classes: [Symbol])
|
data/lib/relaton/index/pool.rb
CHANGED
@@ -13,11 +13,16 @@ module Relaton
|
|
13
13
|
#
|
14
14
|
# @param [String] type <description>
|
15
15
|
# @param [String, nil] url external URL to index, used to fetch index for searching files
|
16
|
+
# @param [String, nil] file output file name
|
16
17
|
#
|
17
18
|
# @return [Relaton::Index::Type] typed index
|
18
19
|
#
|
19
|
-
def type(type,
|
20
|
-
@pool[type.upcase.to_sym]
|
20
|
+
def type(type, **args)
|
21
|
+
if @pool[type.upcase.to_sym]&.actual?(**args)
|
22
|
+
@pool[type.upcase.to_sym]
|
23
|
+
else
|
24
|
+
@pool[type.upcase.to_sym] = Type.new(type, args[:url], args[:file])
|
25
|
+
end
|
21
26
|
end
|
22
27
|
|
23
28
|
#
|
data/lib/relaton/index/type.rb
CHANGED
@@ -9,12 +9,19 @@ module Relaton
|
|
9
9
|
#
|
10
10
|
# @param [String, Symbol] type type of index (ISO, IEC, etc.)
|
11
11
|
# @param [String, nil] url external URL to index, used to fetch index for searching files
|
12
|
+
# @param [String, nil] file output file name
|
12
13
|
#
|
13
|
-
def initialize(type, url = nil)
|
14
|
-
@
|
14
|
+
def initialize(type, url = nil, file = nil)
|
15
|
+
@file = file
|
16
|
+
filename = file || Index.config.filename
|
17
|
+
@file_io = FileIO.new type.to_s.downcase, url, filename
|
15
18
|
@index = @file_io.read
|
16
19
|
end
|
17
20
|
|
21
|
+
def actual?(**args)
|
22
|
+
(!args.key?(:url) || args[:url] == @file_io.url) && (!args.key?(:file) || args[:file] == @file)
|
23
|
+
end
|
24
|
+
|
18
25
|
#
|
19
26
|
# Add or update index item
|
20
27
|
#
|
data/lib/relaton/index.rb
CHANGED
@@ -20,11 +20,12 @@ module Relaton
|
|
20
20
|
#
|
21
21
|
# @param [String] type index type (ISO, IEC, etc.)
|
22
22
|
# @param [String, nil] url external URL to index, used to fetch index for searching files
|
23
|
+
# @param [String, nil] file output file name, default is config.filename
|
23
24
|
#
|
24
25
|
# @return [Relaton::Index::Type] typed index
|
25
26
|
#
|
26
|
-
def find_or_create(type, url: nil)
|
27
|
-
pool.type(type, url)
|
27
|
+
def find_or_create(type, url: nil, file: nil)
|
28
|
+
pool.type(type, url: url, file: file)
|
28
29
|
end
|
29
30
|
|
30
31
|
def close(type)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relaton-index
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ribose Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-03-
|
11
|
+
date: 2023-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyzip
|