relaton-index 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.adoc +41 -8
- data/lib/relaton/index/config.rb +13 -1
- data/lib/relaton/index/file_io.rb +11 -7
- 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: c0a18404c0b3197dd888ee8b286ec575e391327d0ad7c50645fb57adfa922b78
|
4
|
+
data.tar.gz: 69bd5fc4c3dc70d2daf646debe653d899ebfd7bef691809c8ac31b4e49ebf1cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66c4fc49df2fcebbbb41913e55223d94b5058bde802b714381c1a91178d37d13904055a99aa5b5d5f87283d1248cdde6d86c309060f238a7988f538277de72db
|
7
|
+
data.tar.gz: fa6fed8320e73cc1bea2ef9fbf3aec9640f9de5f62f79c03cb5a34ad2473c5bbd452008b1dd4ba275c47e844d75ca0d68351efc4c6fd67d0dc0cd284211e0465
|
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
|
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/[TYPE]/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,29 @@ 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" }]
|
82
|
+
----
|
69
83
|
|
70
|
-
|
71
|
-
|
84
|
+
=== Configuration
|
85
|
+
|
86
|
+
The gem can be configured by using the `Relaton::Index.config` method. The following settings are available:
|
87
|
+
|
88
|
+
- `filename` - the name of the index file. By default, it is `index.yaml`.
|
89
|
+
- `storage` - the storage class or module. By default, it is `FileStorage` module. It can be any class or module that implements the `ctime`, `read`, and `write` class methods.
|
90
|
+
- `storage_dir` - the directory where the `.relaton/[TYPE]` folder is created. By default, it's a user's home dir. This setting works only when the gem is used for searching. When indexing, the index is always saved in a current folder.
|
91
|
+
|
92
|
+
[source,ruby]
|
93
|
+
----
|
94
|
+
Relaton::Index.config do |config|
|
95
|
+
config.filename = "index-new.yaml"
|
96
|
+
config.storage = S3Storage
|
97
|
+
config.storage_dir = "/"
|
98
|
+
end
|
99
|
+
----
|
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"
|
72
105
|
----
|
73
106
|
|
74
107
|
== Development
|
data/lib/relaton/index/config.rb
CHANGED
@@ -4,7 +4,7 @@ module Relaton
|
|
4
4
|
# Configuration class for Relaton::Index
|
5
5
|
#
|
6
6
|
class Config
|
7
|
-
attr_reader :storage, :storage_dir
|
7
|
+
attr_reader :storage, :storage_dir, :filename
|
8
8
|
|
9
9
|
#
|
10
10
|
# Set default values
|
@@ -12,6 +12,7 @@ module Relaton
|
|
12
12
|
def initialize
|
13
13
|
@storage = FileStorage
|
14
14
|
@storage_dir = Dir.home
|
15
|
+
@filename = "index.yaml"
|
15
16
|
end
|
16
17
|
|
17
18
|
#
|
@@ -35,6 +36,17 @@ module Relaton
|
|
35
36
|
def storage_dir=(dir)
|
36
37
|
@storage_dir = dir
|
37
38
|
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Set filename
|
42
|
+
#
|
43
|
+
# @param [String] filename filename
|
44
|
+
#
|
45
|
+
# @return [void]
|
46
|
+
#
|
47
|
+
def filename=(filename)
|
48
|
+
@filename = filename
|
49
|
+
end
|
38
50
|
end
|
39
51
|
end
|
40
52
|
end
|
@@ -6,16 +6,20 @@ 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
|
#
|
12
14
|
# @param [String] dir local directory in ~/.relaton to store index
|
13
|
-
# @param [String, nil] url git repository URL to fetch index from
|
14
|
-
# or nil if index is used to
|
15
|
+
# @param [String, nil] url git repository URL to fetch index from
|
16
|
+
# (if not exists, or older than 24 hours) or nil if index is used to
|
17
|
+
# index files
|
15
18
|
#
|
16
|
-
def initialize(dir, url)
|
19
|
+
def initialize(dir, url, filename)
|
17
20
|
@dir = dir
|
18
21
|
@url = url
|
22
|
+
@filename = filename
|
19
23
|
end
|
20
24
|
|
21
25
|
#
|
@@ -24,11 +28,11 @@ module Relaton
|
|
24
28
|
# @return [Array<Hash>] index
|
25
29
|
#
|
26
30
|
def read
|
27
|
-
if
|
28
|
-
@file ||= File.join(Index.config.storage_dir, ".relaton", @dir,
|
31
|
+
if url
|
32
|
+
@file ||= File.join(Index.config.storage_dir, ".relaton", @dir, @filename)
|
29
33
|
check_file || fetch_and_save
|
30
34
|
else
|
31
|
-
@file ||=
|
35
|
+
@file ||= @filename
|
32
36
|
read_file
|
33
37
|
end
|
34
38
|
end
|
@@ -63,7 +67,7 @@ module Relaton
|
|
63
67
|
# @return [Array<Hash>] index
|
64
68
|
#
|
65
69
|
def fetch_and_save
|
66
|
-
resp = URI(
|
70
|
+
resp = URI(url).open
|
67
71
|
zip = Zip::InputStream.new resp
|
68
72
|
entry = zip.get_next_entry
|
69
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, 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.4
|
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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyzip
|