relaton-index 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae7a2387f5e09bff4b3d07563d7e6b3daec43720e7af59d5387f62c41d3892e4
4
- data.tar.gz: 8cd557981092f4f37c7d5a318eb6655472895f1a383044af469615e4e99430c0
3
+ metadata.gz: c0a18404c0b3197dd888ee8b286ec575e391327d0ad7c50645fb57adfa922b78
4
+ data.tar.gz: 69bd5fc4c3dc70d2daf646debe653d899ebfd7bef691809c8ac31b4e49ebf1cf
5
5
  SHA512:
6
- metadata.gz: aebc8bc1def475961474cb000a79d02d8341f1f9687d93d93559f0126cbf7d091a88fcdfdb0308f181bda3bfc9539cfd876f159936f488cd6117fc21baaf1cac
7
- data.tar.gz: 35ad2ae0c5c84328860147a1c5422548bf99437496085d1d0f29f3b2515344e8dca01bad75f40a6f21a84838576763c811d9f873b06afde1ff90e73623f4b4bc
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 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
- require 'relaton/index'
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 @url
29
- @file ||= File.join(Index.config.storage_dir, ".relaton", @dir, Index.config.filename)
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 ||= Index.config.filename
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(@url).open
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])
@@ -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, url)
20
- @pool[type.upcase.to_sym] ||= Type.new(type, url)
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
  #
@@ -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
- @file_io = FileIO.new type.to_s.downcase, url
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
  #
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Relaton
4
4
  module Index
5
- VERSION = "0.1.3"
5
+ VERSION = "0.1.4"
6
6
  end
7
7
  end
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-index
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.