relaton-index 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e593c33ab08170851f728d43821031387b3ad70ce441501429909132306880ab
4
+ data.tar.gz: a5445a29d744fa3ff9214274be7be64a0694f1262ee8f99820a4abef61c0398b
5
+ SHA512:
6
+ metadata.gz: a0a292add233827c1298bcddad35aaab24a611a4ac4bdc9cb8a323aee92b2673740f551d68fb1591a11bda20fa01ccc55ea444233b4bc5d2719493ba9ab5dd92
7
+ data.tar.gz: 0e06a6dd6372fc7cca12bf05b5f83007f48ee992af18c2191b8fc0f6ea854df9401867aa9aac848bd93e228ac5de8f66ca52ee19b42cfb0f7b1c55078678804c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ # This project follows the Ribose OSS style guide.
2
+ # https://github.com/riboseinc/oss-guides
3
+ # All project-specific additions and overrides should be specified in this file.
4
+
5
+ require: rubocop-rails
6
+
7
+ inherit_from:
8
+ - https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
9
+ AllCops:
10
+ TargetRubyVersion: 2.7
11
+ Rails:
12
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in relaton-index.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.46"
13
+ gem "rubocop-performance", "~> 1.15"
14
+ gem "rubocop-rails", "~> 2.17"
15
+
16
+ gem "simplecov", "~> 0.22"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Ribose Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.adoc ADDED
@@ -0,0 +1,79 @@
1
+ = Relaton::Index
2
+
3
+ image:https://img.shields.io/gem/v/relaton-index.svg["Gem Version", link="https://rubygems.org/gems/relaton-index"]
4
+ image:https://github.com/relaton/relaton-index/workflows/macos/badge.svg["Build Status (macOS)", link="https://github.com/relaton/relaton-index/actions?workflow=macos"]
5
+ image:https://github.com/relaton/relaton-index/workflows/windows/badge.svg["Build Status (Windows)", link="https://github.com/relaton/relaton-index/actions?workflow=windows"]
6
+ image:https://github.com/relaton/relaton-index/workflows/ubuntu/badge.svg["Build Status (Ubuntu)", link="https://github.com/relaton/relaton-index/actions?workflow=ubuntu"]
7
+ image:https://codeclimate.com/github/relaton/relaton-index/badges/gpa.svg["Code Climate", link="https://codeclimate.com/github/metanorma/relaton-index"]
8
+ image:https://img.shields.io/github/issues-pr-raw/relaton/relaton-index.svg["Pull Requests", link="https://github.com/relaton/relaton-index/pulls"]
9
+ image:https://img.shields.io/github/commits-since/relaton/relaton-index/latest.svg["Commits since latest",link="https://github.com/relaton/relaton-index/releases"]
10
+
11
+ This gem is a part of [Relaton](https://github.com/relaton) project. It provides a way to index Relaton documents' files and search them by references.
12
+
13
+ == Installation
14
+
15
+ Install the gem and add it to the application's Gemfile by executing:
16
+
17
+ $ bundle add relaton-index
18
+
19
+ If bundler is not being used to manage dependencies, install the gem by executing:
20
+
21
+ $ gem install relaton-index
22
+
23
+ == Usage
24
+
25
+ The aim of this gem is to be used by Relaton libraries in two ways:
26
+ - indexing documents' files in GitHub repositories
27
+ - searching a document by a reference when the Relaton library fetches a document
28
+
29
+ === Indexing
30
+
31
+ In this case, the Relaton library should create an index object and add documents' files to it. The index object should be saved to the `index.yaml` file in the root of the repository.
32
+
33
+ [source,ruby]
34
+ ----
35
+ require 'relaton/index'
36
+
37
+ # 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
+ index = Relaton::Index.find_or_create :IHO
39
+
40
+ # Add a document to the index or update it if it already exists.
41
+ index.add_or_update id: "B-4 2.19.0", file: "data/b-4_2_19_0.xml"
42
+
43
+ # Save the index to the `index.yaml` file in the current directory.
44
+ index.save
45
+ ----
46
+
47
+ === Searching
48
+
49
+ In this case, the Relaton library should create an index object and search for a document by reference. The gem looks for the `.relaton/[TYPE]/index.yaml` file in the user's home directory. The `[TYPE]` is one of downcased ISO, IEC, IHO, etc. If the file is not found or is older than 24 hours then it will be downloaded from the URL specified in the `find_or_create` method.
50
+
51
+ [source,ruby]
52
+ ----
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/[TYPE]/index.yaml` file.
56
+ index = Relaton::Index.find_or_create :IHO, url: "https://raw.githubusercontent.com/relaton/relaton-data-iho/master/index.zip"
57
+
58
+
59
+ # Search for a document by reference
60
+ index.search "B-4 2.19.0"
61
+ # => ["data/b-4_2_19_0.xml"]
62
+
63
+ # Remove the index from the pool.
64
+ Relaton::Index.close :IHO
65
+ ----
66
+
67
+ == Development
68
+
69
+ 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.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ == Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/relaton/relaton-index.
76
+
77
+ == License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec]
@@ -0,0 +1,40 @@
1
+ module Relaton
2
+ module Index
3
+ #
4
+ # Configuration class for Relaton::Index
5
+ #
6
+ class Config
7
+ attr_reader :storage, :storage_dir
8
+
9
+ #
10
+ # Set default values
11
+ #
12
+ def initialize
13
+ @storage = FileStorage
14
+ @storage_dir = Dir.home
15
+ end
16
+
17
+ #
18
+ # Set storage
19
+ #
20
+ # @param [#ctime, #read, #write] storage storage object
21
+ #
22
+ # @return [void]
23
+ #
24
+ def storage=(storage)
25
+ @storage = storage
26
+ end
27
+
28
+ #
29
+ # Set storage directory
30
+ #
31
+ # @param [String] dir storage directory
32
+ #
33
+ # @return [void]
34
+ #
35
+ def storage_dir=(dir)
36
+ @storage_dir = dir
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,86 @@
1
+ module Relaton
2
+ module Index
3
+ #
4
+ # File IO class is used to read and write index files.
5
+ # In searh mode url is used to fetch index from external repository and save it to storage.
6
+ # In index mode url should be nil.
7
+ #
8
+ class FileIO
9
+ #
10
+ # Initialize FileIO
11
+ #
12
+ # @param [String] dir local directory in ~/.relaton to store index
13
+ # @param [String, nil] url git repository URL to fetch index from (if not exists, or older than 24 hours)
14
+ # or nil if index is used to index files
15
+ #
16
+ def initialize(dir, url)
17
+ @dir = dir
18
+ @url = url
19
+ end
20
+
21
+ #
22
+ # Read index from storage or fetch from external repository
23
+ #
24
+ # @return [Array<Hash>] index
25
+ #
26
+ def read
27
+ if @url
28
+ @file ||= File.join(Index.config.storage_dir, ".relaton", @dir, "index.yaml")
29
+ check_file || fetch_and_save
30
+ else
31
+ @file ||= "index.yaml"
32
+ read_file
33
+ end
34
+ end
35
+
36
+ #
37
+ # Check if index file exists and is not older than 24 hours
38
+ #
39
+ # @return [Array<Hash>, nil] index or nil
40
+ #
41
+ def check_file
42
+ ctime = Index.config.storage.ctime(@file)
43
+ return unless ctime && ctime > Time.now - 86400
44
+
45
+ read_file
46
+ end
47
+
48
+ #
49
+ # Read index from storage
50
+ #
51
+ # @return [Array<Hash>] index
52
+ #
53
+ def read_file
54
+ yaml = Index.config.storage.read(@file)
55
+ return [] unless yaml
56
+
57
+ YAML.safe_load yaml, permitted_classes: [Symbol]
58
+ end
59
+
60
+ #
61
+ # Fetch index from external repository and save it to storage
62
+ #
63
+ # @return [Array<Hash>] index
64
+ #
65
+ def fetch_and_save
66
+ resp = URI(@url).open
67
+ zip = Zip::InputStream.new resp
68
+ entry = zip.get_next_entry
69
+ index = YAML.safe_load(entry.get_input_stream.read, [Symbol])
70
+ save index
71
+ index
72
+ end
73
+
74
+ #
75
+ # Save index to storage
76
+ #
77
+ # @param [Array<Hash>] index <description>
78
+ #
79
+ # @return [void]
80
+ #
81
+ def save(index)
82
+ Index.config.storage.write @file, index.to_yaml
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,46 @@
1
+ module Relaton
2
+ module Index
3
+ #
4
+ # File storage module contains methods to read and write files
5
+ #
6
+ module FileStorage
7
+ #
8
+ # Return file creation time
9
+ #
10
+ # @param [String] file file path
11
+ #
12
+ # @return [Time, nil] file creation time or nil if file does not exist
13
+ #
14
+ def ctime(file)
15
+ File.exist?(file) && File.ctime(file)
16
+ end
17
+
18
+ #
19
+ # Read file
20
+ #
21
+ # @param [String] file file path
22
+ #
23
+ # @return [String, nil] file content or nil if file does not exist
24
+ #
25
+ def read(file)
26
+ return unless File.exist?(file)
27
+
28
+ File.read file, encoding: "UTF-8"
29
+ end
30
+
31
+ #
32
+ # Write file
33
+ #
34
+ # @param [String] file file path
35
+ # @param [String] data content to write
36
+ #
37
+ # @return [void]
38
+ #
39
+ def write(file, data)
40
+ File.write file, data, encoding: "UTF-8"
41
+ end
42
+
43
+ extend self
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ module Relaton
2
+ module Index
3
+ #
4
+ # Pool of indexes
5
+ #
6
+ class Pool
7
+ def initialize
8
+ @pool = {}
9
+ end
10
+
11
+ #
12
+ # Return index by type, create if not exists
13
+ #
14
+ # @param [String] type <description>
15
+ # @param [String, nil] url external URL to index, used to fetch index for searching files
16
+ #
17
+ # @return [Relaton::Index::Type] typed index
18
+ #
19
+ def type(type, url)
20
+ @pool[type.upcase.to_sym] ||= Type.new(type, url)
21
+ end
22
+
23
+ #
24
+ # Remove index by type from pool
25
+ #
26
+ # @param [String] type index type
27
+ #
28
+ # @return [void]
29
+ #
30
+ def remove(type)
31
+ @pool.delete type.upcase.to_sym
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ module Relaton
2
+ module Index
3
+ #
4
+ # Relaton::Index::Type is a class for indexing Relaton files.
5
+ #
6
+ class Type
7
+ #
8
+ # Initialize a new Relaton::Index::Type object
9
+ #
10
+ # @param [String, Symbol] type type of index (ISO, IEC, etc.)
11
+ # @param [String, nil] url external URL to index, used to fetch index for searching files
12
+ #
13
+ def initialize(type, url = nil)
14
+ @file_io = FileIO.new type.to_s.downcase, url
15
+ @index = @file_io.read
16
+ end
17
+
18
+ #
19
+ # Add or update index item
20
+ #
21
+ # @param [String] id document ID
22
+ # @param [String] file file name of the document
23
+ #
24
+ # @return [void]
25
+ #
26
+ def add_or_update(id, file)
27
+ item = @index.find { |i| i[:id] == id }
28
+ if item
29
+ item[:file] = file
30
+ else
31
+ @index << { id: id, file: file }
32
+ end
33
+ end
34
+
35
+ #
36
+ # Search index for a given ID
37
+ #
38
+ # @param [Comparable] id ID to search for
39
+ #
40
+ # @return [Array<Hash>] search results
41
+ #
42
+ def search(id)
43
+ @index.select do |i|
44
+ block_given? ? yield(i) : i[:id].include?(id)
45
+ end
46
+ end
47
+
48
+ #
49
+ # Save index to storage
50
+ #
51
+ # @return [void]
52
+ #
53
+ def save
54
+ @file_io.save @index
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Relaton
4
+ module Index
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "zip"
5
+
6
+ require_relative "index/version"
7
+ require_relative "index/file_storage"
8
+ require_relative "index/config"
9
+ require_relative "index/pool"
10
+ require_relative "index/type"
11
+ require_relative "index/file_io"
12
+
13
+ module Relaton
14
+ module Index
15
+ class Error < StandardError; end
16
+
17
+ class << self
18
+ #
19
+ # Find or create index
20
+ #
21
+ # @param [String] type index type (ISO, IEC, etc.)
22
+ # @param [String, nil] url external URL to index, used to fetch index for searching files
23
+ #
24
+ # @return [Relaton::Index::Type] typed index
25
+ #
26
+ def find_or_create(type, url: nil)
27
+ pool.type(type, url)
28
+ end
29
+
30
+ def close(type)
31
+ pool.remove type
32
+ end
33
+
34
+ #
35
+ # Create new index pool object or return existing
36
+ #
37
+ # @return [Relaton::Index::Pool] index pool
38
+ #
39
+ def pool
40
+ @pool ||= Pool.new
41
+ end
42
+
43
+ #
44
+ # Create new config object or return existing
45
+ #
46
+ # @return [Relaton::Index::Config] config object
47
+ #
48
+ def config
49
+ @config ||= Config.new
50
+ end
51
+
52
+ #
53
+ # Configure Relaton::Index
54
+ #
55
+ # @return [void]
56
+ #
57
+ def configure
58
+ yield config
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/relaton/index/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "relaton-index"
7
+ spec.version = Relaton::Index::VERSION
8
+ spec.authors = ["Ribose Inc."]
9
+ spec.email = ["open.source@ribose.com"]
10
+
11
+ spec.summary = "Relaton Index is a library for indexing Relaton files."
12
+ spec.homepage = "https://github.com/relaton/relaton-index"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.7.0"
15
+
16
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "rubyzip", "~> 2.3.0"
34
+
35
+ # For more information and examples about making a new gem, check out our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
@@ -0,0 +1,6 @@
1
+ module Relaton
2
+ module Index
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: relaton-index
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ribose Inc.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubyzip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.0
27
+ description:
28
+ email:
29
+ - open.source@ribose.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.adoc
39
+ - Rakefile
40
+ - lib/relaton/index.rb
41
+ - lib/relaton/index/config.rb
42
+ - lib/relaton/index/file_io.rb
43
+ - lib/relaton/index/file_storage.rb
44
+ - lib/relaton/index/pool.rb
45
+ - lib/relaton/index/type.rb
46
+ - lib/relaton/index/version.rb
47
+ - relaton-index.gemspec
48
+ - sig/relaton/index.rbs
49
+ homepage: https://github.com/relaton/relaton-index
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/relaton/relaton-index
54
+ source_code_uri: https://github.com/relaton/relaton-index
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.7.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Relaton Index is a library for indexing Relaton files.
74
+ test_files: []