libis-tools 1.0.5-java
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.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +40 -0
- data/Gemfile +7 -0
- data/README.md +202 -0
- data/Rakefile +11 -0
- data/bin/libis_tool +5 -0
- data/lib/libis-tools.rb +1 -0
- data/lib/libis/tools.rb +25 -0
- data/lib/libis/tools/assert.rb +52 -0
- data/lib/libis/tools/checksum.rb +106 -0
- data/lib/libis/tools/cli/cli_helper.rb +189 -0
- data/lib/libis/tools/cli/reorg.rb +416 -0
- data/lib/libis/tools/command.rb +133 -0
- data/lib/libis/tools/command_line.rb +23 -0
- data/lib/libis/tools/config.rb +147 -0
- data/lib/libis/tools/config_file.rb +85 -0
- data/lib/libis/tools/csv.rb +38 -0
- data/lib/libis/tools/deep_struct.rb +71 -0
- data/lib/libis/tools/extend/array.rb +16 -0
- data/lib/libis/tools/extend/empty.rb +7 -0
- data/lib/libis/tools/extend/hash.rb +147 -0
- data/lib/libis/tools/extend/kernel.rb +25 -0
- data/lib/libis/tools/extend/ostruct.rb +3 -0
- data/lib/libis/tools/extend/roo.rb +91 -0
- data/lib/libis/tools/extend/string.rb +94 -0
- data/lib/libis/tools/extend/struct.rb +29 -0
- data/lib/libis/tools/extend/symbol.rb +8 -0
- data/lib/libis/tools/logger.rb +130 -0
- data/lib/libis/tools/mets_dnx.rb +61 -0
- data/lib/libis/tools/mets_file.rb +504 -0
- data/lib/libis/tools/mets_objects.rb +547 -0
- data/lib/libis/tools/parameter.rb +372 -0
- data/lib/libis/tools/spreadsheet.rb +196 -0
- data/lib/libis/tools/temp_file.rb +42 -0
- data/lib/libis/tools/thread_safe.rb +31 -0
- data/lib/libis/tools/version.rb +5 -0
- data/lib/libis/tools/xml_document.rb +583 -0
- data/libis-tools.gemspec +55 -0
- data/spec/assert_spec.rb +65 -0
- data/spec/checksum_spec.rb +68 -0
- data/spec/command_spec.rb +90 -0
- data/spec/config_file_spec.rb +83 -0
- data/spec/config_spec.rb +113 -0
- data/spec/csv_spec.rb +159 -0
- data/spec/data/test-headers.csv +2 -0
- data/spec/data/test-headers.tsv +2 -0
- data/spec/data/test-noheaders.csv +1 -0
- data/spec/data/test-noheaders.tsv +1 -0
- data/spec/data/test.data +9 -0
- data/spec/data/test.xlsx +0 -0
- data/spec/data/test.xml +8 -0
- data/spec/data/test.yml +2 -0
- data/spec/data/test_config.yml +15 -0
- data/spec/deep_struct_spec.rb +138 -0
- data/spec/logger_spec.rb +165 -0
- data/spec/mets_file_spec.rb +223 -0
- data/spec/parameter_container_spec.rb +152 -0
- data/spec/parameter_spec.rb +148 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/spreadsheet_spec.rb +1820 -0
- data/spec/temp_file_spec.rb +76 -0
- data/spec/test.xsd +20 -0
- data/spec/thread_safe_spec.rb +64 -0
- data/spec/xmldocument_spec.rb +421 -0
- data/test/test_helper.rb +7 -0
- data/test/webservices/test_ca_item_info.rb +59 -0
- data/test/webservices/test_ca_search.rb +35 -0
- metadata +437 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7dfb912e9b01e86323a41b92b09cfb20c1376767
|
4
|
+
data.tar.gz: 104aef995014cd9297ad97efd7274cbd76263c73
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 221d41957c111983a162b2ff4ba2175bba747ec07fe699ce03edbbcd67c200133bb919b76921b68f754d6e14514ea8f67afa2fc5c911a655602fd1bda8570640
|
7
|
+
data.tar.gz: c951a929559105f46926f1570443bd53a7f944aea0a126dd2c0a5303544c1893cde798b007eb7e2e7de34486f9e60290ef0626069fc2159de0f8ea609c6071b2
|
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
dist: bionic
|
2
|
+
language: ruby
|
3
|
+
cache: bundler
|
4
|
+
sudo: false
|
5
|
+
rvm:
|
6
|
+
- 2.3
|
7
|
+
- 2.4
|
8
|
+
- 2.5
|
9
|
+
- 2.6
|
10
|
+
- ruby-head
|
11
|
+
# - jruby-9.1
|
12
|
+
- jruby-9.2
|
13
|
+
- jruby-head
|
14
|
+
jdk:
|
15
|
+
- openjdk10
|
16
|
+
- openjdk11
|
17
|
+
matrix:
|
18
|
+
exclude:
|
19
|
+
- rvm: 2.3
|
20
|
+
jdk: openjdk10
|
21
|
+
- rvm: 2.4
|
22
|
+
jdk: openjdk10
|
23
|
+
- rvm: 2.5
|
24
|
+
jdk: openjdk10
|
25
|
+
- rvm: 2.6
|
26
|
+
jdk: openjdk10
|
27
|
+
- rvm: ruby-head
|
28
|
+
jdk: openjdk10
|
29
|
+
allow_failures:
|
30
|
+
- rvm: ruby-head
|
31
|
+
- rvm: jruby-head
|
32
|
+
branches:
|
33
|
+
only:
|
34
|
+
- master
|
35
|
+
#before_install:
|
36
|
+
# - gem update --system
|
37
|
+
# - gem --version
|
38
|
+
addons:
|
39
|
+
code_climate:
|
40
|
+
repo_token: f6978964f55e6543befb82373651365f8195677816af6ff40ad70c82f7f22f09
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
[](http://badge.fury.io/rb/libis-tools)
|
2
|
+
[](https://travis-ci.org/Kris-LIBIS/LIBIS_Tools)
|
3
|
+
[](https://coveralls.io/r/Kris-LIBIS/LIBIS_Tools)
|
4
|
+
[](https://gemnasium.com/Kris-LIBIS/LIBIS_Tools)
|
5
|
+
|
6
|
+
# Libis::Tools
|
7
|
+
|
8
|
+
This gem contains some generic helper methods, classes and modules that should be easily reusable in other projects.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'libis-tools'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install libis-tools
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
In order to make available all the code the gem supplies a single file can be included:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'libis-tools'
|
32
|
+
```
|
33
|
+
|
34
|
+
or:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'libis/tools'
|
38
|
+
```
|
39
|
+
|
40
|
+
Alternatively, if you only want to use a single class or module, partial files are available. See the examples in the
|
41
|
+
sections below for their names.
|
42
|
+
|
43
|
+
## Content
|
44
|
+
|
45
|
+
### {Object#assert}
|
46
|
+
|
47
|
+
The {Object#assert} method enables the assert functionality found in other languages. Since it is defined on the
|
48
|
+
Object class, it is available on almost any class.
|
49
|
+
|
50
|
+
### {::Libis::Tools::Checksum}
|
51
|
+
|
52
|
+
The {::Libis::Tools::Checksum} class offers a standardized interface for calculating checksums of file
|
53
|
+
contents in different formats.
|
54
|
+
|
55
|
+
### {::Libis::Tools::Command}
|
56
|
+
|
57
|
+
The {::Libis::Tools::Command} module offers a safe way to execute external commands and gives you access to the
|
58
|
+
exit status as well as standard output and standard error information. May have issues on older JRuby versions.
|
59
|
+
|
60
|
+
### {::Libis::Tools::DeepStruct}
|
61
|
+
|
62
|
+
A class that derives from OpenStruct through the RecursiveOpenStruct.
|
63
|
+
By wrapping a Hash recursively, it allows for easy access to the content by method names.
|
64
|
+
|
65
|
+
### {::Libis::Tools::ConfigFile}
|
66
|
+
|
67
|
+
A base class for {::Libis::Tools::Config}, but useable on it's own.
|
68
|
+
It extends the DeepStruct with loading from and saving to YAML files.
|
69
|
+
|
70
|
+
### {::Libis::Tools::Config}
|
71
|
+
|
72
|
+
This Singleton class is a convenience class for easy configuration maintenance and loading.
|
73
|
+
It also initializes a default logger.
|
74
|
+
|
75
|
+
### {::Libis::Tools::Logger}
|
76
|
+
|
77
|
+
The ::Libis::Tools::Logger module adds support for logging functionality to any class.
|
78
|
+
|
79
|
+
## {::Libis::Tools::Parameter} and {::Libis::Tools::ParameterContainer}
|
80
|
+
|
81
|
+
The class {::Libis::Tools::Parameter} and the {::Libis::Tools::ParameterContainer} module provide a simple framework for
|
82
|
+
instance variables that are type-safe and can easily be documented and provide defaults.
|
83
|
+
|
84
|
+
## {::Libis::Tools::TempFile}
|
85
|
+
|
86
|
+
A small and simple module that provides some convenience methods to deal with temp files. Random file names are generated
|
87
|
+
in a similar way as the standard Ruby Tempfile class does. It has the form:
|
88
|
+
```
|
89
|
+
<Optional prefix with '_' appended><YYYYMMDD>_<process id>_<random base36 number><optional suffix>
|
90
|
+
```
|
91
|
+
|
92
|
+
The #name method creates a random file name. Optional parameters are the prefix and suffix (including '.' character if
|
93
|
+
needed) for the temp file name and the directory part of the file path. Without directory option the file path will be
|
94
|
+
located in the standard folder for temporary files (e.g. /tmp on Linux).
|
95
|
+
|
96
|
+
The #file method creates a random file name as above, but immediately opens it for writing. If a block is given, the open
|
97
|
+
file pointer (IO object) will be passed as argument to the block and the file will automatically be closed and deleted
|
98
|
+
when the block ends. In that case the return value will be whatever the block returns.
|
99
|
+
|
100
|
+
Without a block, the method still creates and opens the file, but it will return the open file pointer to the caller. The
|
101
|
+
caller is responsible to #close and #unlink or #delete the file. The #unlink and #delete methods are injected into the
|
102
|
+
returned IO object for your convenience, but calling the corresponding File methods instead is equally valid.
|
103
|
+
|
104
|
+
## {::Libis::Tools::ThreadSafe}
|
105
|
+
|
106
|
+
A convenience method that embeds the mutex implementation. Just include this module whenever you need a thread-save
|
107
|
+
implementation and use the mutex instance variable without any concerns regarding initialization. Your class will have
|
108
|
+
access to an instance variable 'mutex' as well as a class variable 'class_mutex'. The mutexes (Montor instance) themselves
|
109
|
+
are created in a thread-safe way.
|
110
|
+
|
111
|
+
## {::Libis::Tools::XmlDocument}
|
112
|
+
|
113
|
+
Class that embodies most used features of Nokogiri, Nori and Gyoku in one convenience class. The Nokogiri document is
|
114
|
+
stored in the class variable 'document' and can be accessed and manipulated directly - if required. The class supports
|
115
|
+
the Nokogiri Build syntax for creating XML documents in a compact DSL. It also allows you to check the document against
|
116
|
+
an XML Schema and provides shorthand notations for accessing nodes and attributes.
|
117
|
+
|
118
|
+
Example:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
xml_doc = ::Libis::Tools::XmlDocument.parse(<<-END.align_left)
|
122
|
+
<patron>
|
123
|
+
<name>Harry Potter</name>
|
124
|
+
<barcode library="Hogwarts Library">1234567890</barcode>
|
125
|
+
<access_level>student</access_level>
|
126
|
+
<email>harry.potter@hogwarts.edu</email>
|
127
|
+
<email>hpotter@JKRowling.com</email>
|
128
|
+
</patron>
|
129
|
+
END
|
130
|
+
puts '---parse---', xml_doc.to_xml
|
131
|
+
xml_doc.save('/tmp/test.xml')
|
132
|
+
xml_doc = ::Libis::Tools::XmlDocument.open('/tmp/test.xml')
|
133
|
+
puts '---save/open---', xml_doc.to_xml
|
134
|
+
xml_doc = ::Libis::Tools::XmlDocument.build do
|
135
|
+
patron {
|
136
|
+
name 'Harry Potter'
|
137
|
+
barcode( '1234567890', library: 'Hogwarts Library')
|
138
|
+
access_level 'student'
|
139
|
+
email 'harry.potter@hogwarts.edu'
|
140
|
+
email 'hpotter@JKRowling.com'
|
141
|
+
}
|
142
|
+
end
|
143
|
+
puts '---build---', xml_doc.to_xml
|
144
|
+
xml_doc = ::Libis::Tools::XmlDocument.new
|
145
|
+
xml_doc.add_node :patron
|
146
|
+
xml_doc.name = 'Harry Potter'
|
147
|
+
xml_doc.barcode = '1234567890'
|
148
|
+
xml_doc.barcode :library, 'Hogwarts Library'
|
149
|
+
xml_doc.access_level = 'student'
|
150
|
+
xml_doc.email = 'harry.potter@hogwarts.edu'
|
151
|
+
xml_doc.add_node :email, 'hpotter@JKRowling.com'
|
152
|
+
# Note: xml_doc.email('hpotter@JKRowling.com') whould not have created a new node.
|
153
|
+
# It would override the first email element
|
154
|
+
puts '---method---', xml_doc.to_xml
|
155
|
+
```
|
156
|
+
|
157
|
+
produces:
|
158
|
+
|
159
|
+
---parse---
|
160
|
+
<?xml version="1.0" encoding="utf-8"?>
|
161
|
+
<patron>
|
162
|
+
<name>Harry Potter</name>
|
163
|
+
<barcode library="Hogwarts Library">1234567890</barcode>
|
164
|
+
<access_level>student</access_level>
|
165
|
+
<email>harry.potter@hogwarts.edu</email>
|
166
|
+
<email>hpotter@JKRowling.com</email>
|
167
|
+
</patron>
|
168
|
+
---save/open---
|
169
|
+
<?xml version="1.0" encoding="utf-8"?>
|
170
|
+
<patron>
|
171
|
+
<name>Harry Potter</name>
|
172
|
+
<barcode library="Hogwarts Library">1234567890</barcode>
|
173
|
+
<access_level>student</access_level>
|
174
|
+
<email>harry.potter@hogwarts.edu</email>
|
175
|
+
<email>hpotter@JKRowling.com</email>
|
176
|
+
</patron>
|
177
|
+
---build---
|
178
|
+
<?xml version="1.0" encoding="utf-8"?>
|
179
|
+
<patron>
|
180
|
+
<name>Harry Potter</name>
|
181
|
+
<barcode library="Hogwarts Library">1234567890</barcode>
|
182
|
+
<access_level>student</access_level>
|
183
|
+
<email>harry.potter@hogwarts.edu</email>
|
184
|
+
<email>hpotter@JKRowling.com</email>
|
185
|
+
</patron>
|
186
|
+
---method---
|
187
|
+
<?xml version="1.0" encoding="utf-8"?>
|
188
|
+
<patron>
|
189
|
+
<name>Harry Potter</name>
|
190
|
+
<barcode library="Hogwarts Library">1234567890</barcode>
|
191
|
+
<access_level>student</access_level>
|
192
|
+
<email>harry.potter@hogwarts.edu</email>
|
193
|
+
<email>hpotter@JKRowling.com</email>
|
194
|
+
</patron>
|
195
|
+
|
196
|
+
## Contributing
|
197
|
+
|
198
|
+
1. Fork it ( https://github.com/Kris-LIBIS/LIBIS_Tools/fork )
|
199
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
200
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
201
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
202
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/libis_tool
ADDED
data/lib/libis-tools.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'libis/tools'
|
data/lib/libis/tools.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'tools/version'
|
2
|
+
|
3
|
+
module Libis
|
4
|
+
module Tools
|
5
|
+
|
6
|
+
autoload :CommandLine, 'libis/tools/command_line'
|
7
|
+
|
8
|
+
autoload :Checksum, 'libis/tools/checksum'
|
9
|
+
autoload :Command, 'libis/tools/command'
|
10
|
+
autoload :Config, 'libis/tools/config'
|
11
|
+
autoload :ConfigFile, 'libis/tools/config_file'
|
12
|
+
autoload :Csv, 'libis/tools/csv'
|
13
|
+
autoload :DeepStruct, 'libis/tools/deep_struct'
|
14
|
+
autoload :Logger, 'libis/tools/logger'
|
15
|
+
autoload :MetsFile, 'libis/tools/mets_file'
|
16
|
+
autoload :Parameter, 'libis/tools/parameter'
|
17
|
+
autoload :Spreadsheet, 'libis/tools/spreadsheet'
|
18
|
+
autoload :TempFile, 'libis/tools/temp_file'
|
19
|
+
autoload :ThreadSafe, 'libis/tools/thread_safe'
|
20
|
+
autoload :XmlDocument, 'libis/tools/xml_document'
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
require_relative 'tools/extend/struct'
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
class AssertionFailure < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
class Object
|
7
|
+
|
8
|
+
# Assert functionality as found in other languages (e.g. 'C').
|
9
|
+
#
|
10
|
+
# The assert is enabled/disabled by setting the $DEBUG global variable. If $DEBUG evaluates to true, the assertion is
|
11
|
+
# active.
|
12
|
+
#
|
13
|
+
# If activated, the first argument will be evaluated and when it evaluates to true, an AssertionFailure exception will
|
14
|
+
# be raised with the message given as the second argument.
|
15
|
+
#
|
16
|
+
# Alternatively, a code block may be passed to the assert. In that case the test expression is not evaluated, but used
|
17
|
+
# as the message for the expression. The assert will yield the code block and it's result will be evaluated to decide
|
18
|
+
# if the exception will be thrown.
|
19
|
+
#
|
20
|
+
# Examples:
|
21
|
+
#
|
22
|
+
# require 'libis/tools/assert'
|
23
|
+
# assert(value > 0, 'value should be positive number')
|
24
|
+
#
|
25
|
+
# # using a code block:
|
26
|
+
# require 'libis/tools/assert'
|
27
|
+
# assert 'database is not idle' do
|
28
|
+
# db = get_database
|
29
|
+
# db.status == :IDLE
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# # using $DEBUG:
|
33
|
+
# $DEBUG = nil
|
34
|
+
# assert false, 'assert 1' # nothing happens
|
35
|
+
# $DEBUG = true
|
36
|
+
# assert false, 'assert 2' # AssertionFailure 'assert 2' is raised
|
37
|
+
# assert 'assert 3', 'assert 4' do
|
38
|
+
# false
|
39
|
+
# end # AssertionFailure 'assert 3' is raised
|
40
|
+
#
|
41
|
+
# @param [Object] test_expression the expression that will be evaluated; the message if a code block is present
|
42
|
+
# @param [String] message exception message is no code block is present
|
43
|
+
def assert(test_expression, message = 'assertion failure')
|
44
|
+
if $DEBUG
|
45
|
+
if block_given?
|
46
|
+
message = test_expression
|
47
|
+
test_expression = yield
|
48
|
+
end
|
49
|
+
raise AssertionFailure.new(message) unless test_expression
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'digest'
|
4
|
+
|
5
|
+
module Libis
|
6
|
+
module Tools
|
7
|
+
|
8
|
+
# Common interface for checksum calculations.
|
9
|
+
#
|
10
|
+
# Supported checksum algortihms are MD5, RMD160 (not on JRuby), SHA-1, SHA-2 (256, 384 and 512-bit versions).
|
11
|
+
# All methods are available on the class and on the instance. The instance has to be initialized with a checksum
|
12
|
+
# algorithm and therefore the instance methods do not have to specify the checksum type.
|
13
|
+
#
|
14
|
+
# There are two ways this can be used: using a class instance or using class methods. When a class instance is used,
|
15
|
+
# the desired checksum type has to be supplied when the instance is created. Each call to a checksum method will
|
16
|
+
# calculate the checksum and reset the digest to prevent future calls to be affected by the current result. When a
|
17
|
+
# class method is used besides the file name, the checksum type has to be supplied.
|
18
|
+
#
|
19
|
+
# Examples:
|
20
|
+
#
|
21
|
+
# require 'libis/tools/checksum'
|
22
|
+
# checksum = ::Libis::Tools::Checksum.new(:MD5)
|
23
|
+
# puts "Checksum: #{checksum.hexdigest(file_name)} (MD5, hex)"
|
24
|
+
#
|
25
|
+
# require 'libis/tools/checksum'
|
26
|
+
# puts "Checksum: #{::Libis::Tools::Checksum.base64digest(file_name, :SHA384)} (SHA-2, 384 bit, base64)"
|
27
|
+
class Checksum
|
28
|
+
# All supported checksum types
|
29
|
+
CHECKSUM_TYPES = [:MD5, :SHA1, :SHA256, :SHA384, :SHA512]
|
30
|
+
|
31
|
+
# @!visibility private
|
32
|
+
# noinspection RubyResolve
|
33
|
+
unless defined? JRUBY_VERSION
|
34
|
+
checksum_types = CHECKSUM_TYPES
|
35
|
+
checksum_types << :RMD160
|
36
|
+
end
|
37
|
+
|
38
|
+
# Create instance for a given checksum algorithm.
|
39
|
+
#
|
40
|
+
# @param [Symbol] type checksum algorithm; one of {CHECKSUM_TYPES}
|
41
|
+
def initialize(type)
|
42
|
+
@hasher = self.class.get_hasher(type)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Calculate binary digest of a file.
|
46
|
+
#
|
47
|
+
# @param [String] file_path_or_string path of the file to calculate the digest for
|
48
|
+
def digest(file_path_or_string)
|
49
|
+
hashit(file_path_or_string).digest!
|
50
|
+
end
|
51
|
+
|
52
|
+
# Calculate the hexadecimal digest of a file.
|
53
|
+
# @param (see #digest)
|
54
|
+
def hexdigest(file_path_or_string)
|
55
|
+
hashit(file_path_or_string).hexdigest!
|
56
|
+
end
|
57
|
+
|
58
|
+
# Calculate the base64 digest of a file.
|
59
|
+
# @param (see #digest)
|
60
|
+
def base64digest(file_path_or_string)
|
61
|
+
hashit(file_path_or_string).base64digest!
|
62
|
+
end
|
63
|
+
|
64
|
+
# Calculate the binary digest of a file.
|
65
|
+
# @param (see #digest)
|
66
|
+
# @param (see #initialize)
|
67
|
+
def self.digest(file_path_or_string, type)
|
68
|
+
new(type).digest(file_path_or_string)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Calculate the hexadecimal digest of a file.
|
72
|
+
# @param (see #digest)
|
73
|
+
# @param (see #initialize)
|
74
|
+
def self.hexdigest(file_path_or_string, type)
|
75
|
+
new(type).hexdigest(file_path_or_string)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Calculate the base64 digest of a file.
|
79
|
+
# @param (see #digest)
|
80
|
+
# @param (see #initialize)
|
81
|
+
def self.base64digest(file_path_or_string, type)
|
82
|
+
new(type).base64digest(file_path_or_string)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Instatiate a Digest instance for access to low-level functionality
|
86
|
+
# @param (see #initialize)
|
87
|
+
def self.get_hasher(type)
|
88
|
+
raise RuntimeError, "Checksum type '#{type}' not supported." unless CHECKSUM_TYPES.include? type
|
89
|
+
Digest(type).new
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def hashit(file_path_or_string)
|
95
|
+
if File.exist?(file_path_or_string)
|
96
|
+
@hasher.file(file_path_or_string)
|
97
|
+
else
|
98
|
+
@hasher.reset.update(file_path_or_string)
|
99
|
+
end
|
100
|
+
@hasher
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|