Class: DerivativeRodeo::Services::ConvertUriViaTemplateService
- Inherits:
-
Object
- Object
- DerivativeRodeo::Services::ConvertUriViaTemplateService
- Defined in:
- lib/derivative_rodeo/services/convert_uri_via_template_service.rb
Overview
A service to convert an array of :from_uris to :to_uris via a :template.
Constant Summary collapse
- DIR_PARTS_REPLACEMENT_REGEXP =
%r{\{\{\s*dir_parts\[(?<left>\-?\d+)\.\.(?<right>\-?\d+)\]\s*\}\}}.freeze
- FILENAME_REPLACEMENT_REGEXP =
%r{\{\{\s*filename\s*\}\}}.freeze
- BASENAME_REPLACEMENT_REGEXP =
%r{\{\{\s*basename\s*\}\}}.freeze
- EXTENSION_REPLACEMENT_REGEXP =
%r{\{\{\s*extension\s*\}\}}.freeze
- SCHEME_REPLACEMENT_REGEXP =
%r{\{\{\s*scheme* \}\}}.freeze
- SCHEME_FOR_URI_REGEXP =
%r{^(?<from_scheme>[^:]+)://}.freeze
Class Attributes collapse
-
#separator ⇒ String
The directory seperator character; default: “/”.
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#basename ⇒ Object
Returns the value of attribute basename.
-
#dir_parts ⇒ Object
Returns the value of attribute dir_parts.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#from_scheme ⇒ Object
Returns the value of attribute from_scheme.
-
#from_uri ⇒ Object
Returns the value of attribute from_uri.
-
#parts ⇒ Object
Returns the value of attribute parts.
-
#path ⇒ Object
Returns the value of attribute path.
-
#template ⇒ Object
Returns the value of attribute template.
-
#template_query ⇒ Object
Returns the value of attribute template_query.
-
#template_without_query ⇒ Object
Returns the value of attribute template_without_query.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
-
.call(from_uri:, template:, adapter: nil, **options) ⇒ String
Convert the given :from_uris to a different list of uris based on the given :template.
-
.coerce_pre_requisite_template_from(template:) ⇒ String
There are generators that have requisite files necessary for processing.
Instance Method Summary collapse
-
#call ⇒ Object
rubocop:enable Metrics/MethodLength.
-
#initialize(from_uri:, template:, adapter: nil, **options) ⇒ ConvertUriViaTemplateService
constructor
rubocop:disable Metrics/MethodLength.
Constructor Details
#initialize(from_uri:, template:, adapter: nil, **options) ⇒ ConvertUriViaTemplateService
rubocop:disable Metrics/MethodLength
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 79 def initialize(from_uri:, template:, adapter: nil, **) @from_uri = from_uri @template = template @adapter = adapter @separator = .fetch(:separator) { self.class.separator } @uri, _query = from_uri.split("?") @from_scheme, @path = uri.split("://") @parts = @path.split(separator) @dir_parts = @parts[0..-2] @filename = [:filename] || @parts[-1] @basename = [:basename] || File.basename(@filename, ".*") ## # HACK: Because the HocrGenerator has `.mono.tiff` and we are not interested in carrying # forward the `.mono` suffix as that makes it hard to find the preprocessed word # coordinates, alto, and plain text. This ensures files derived from the .mono are findable # in IIIF Print. @basename = @basename.sub(/\.mono\z/, '') @extension = [:extension] || File.extname(@filename) # When a generator specifies "same" we want to use the given file's extension @extension = File.extname(@filename) if @extension == DerivativeRodeo::StorageLocations::SAME @extension = ".#{@extension}" unless @extension.start_with?(".") @template_without_query, @template_query = template.split("?") end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def adapter @adapter end |
#basename ⇒ Object
Returns the value of attribute basename.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def basename @basename end |
#dir_parts ⇒ Object
Returns the value of attribute dir_parts.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def dir_parts @dir_parts end |
#extension ⇒ Object
Returns the value of attribute extension.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def extension @extension end |
#filename ⇒ Object
Returns the value of attribute filename.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def filename @filename end |
#from_scheme ⇒ Object
Returns the value of attribute from_scheme.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def from_scheme @from_scheme end |
#from_uri ⇒ Object
Returns the value of attribute from_uri.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def from_uri @from_uri end |
#parts ⇒ Object
Returns the value of attribute parts.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def parts @parts end |
#path ⇒ Object
Returns the value of attribute path.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def path @path end |
#separator ⇒ String
Returns the directory seperator character; default: “/”.
25 26 27 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 25 def separator @separator end |
#template ⇒ Object
Returns the value of attribute template.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def template @template end |
#template_query ⇒ Object
Returns the value of attribute template_query.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def template_query @template_query end |
#template_without_query ⇒ Object
Returns the value of attribute template_without_query.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def template_without_query @template_without_query end |
#uri ⇒ Object
Returns the value of attribute uri.
18 19 20 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 18 def uri @uri end |
Class Method Details
.call(from_uri:, template:, adapter: nil, **options) ⇒ String
Convert the given :from_uris to a different list of uris based on the given :template.
Components of the template:
- basename
-
the file’s basename without extension
- extension
-
the file’s extension with the period
- dir_parts
-
the directory parts in which the file exists; excludes the scheme
- filename
-
a convenience that could be represented as ‘basename.extension`
- scheme
-
a convenience that could be represented as ‘basename.extension`
The specs demonstrate the use cases.
59 60 61 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 59 def self.call(from_uri:, template:, adapter: nil, **) new(from_uri: from_uri, template: template, adapter: adapter, **).call end |
.coerce_pre_requisite_template_from(template:) ⇒ String
There are generators that have requisite files necessary for processing.
For example, before we run ‘tesseract` on an image, we would like to make sure it is monochrome. Hence the interplay between the Generators::HocrGenerator and the Generators::MonochromeGenerator.
74 75 76 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 74 def self.coerce_pre_requisite_template_from(template:) template.split(separator)[0..-2].join(separator) + "#{separator}{{ basename }}{{ extension }}" end |
Instance Method Details
#call ⇒ Object
rubocop:enable Metrics/MethodLength
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 107 def call to_uri = template_without_query.gsub(DIR_PARTS_REPLACEMENT_REGEXP) do |text| # The yielded value does not include capture regions. So I'm re-matching things. # capture region to handle this specific thing. match = DIR_PARTS_REPLACEMENT_REGEXP.match(text) dir_parts[(match[:left].to_i)..(match[:right].to_i)].join(separator) end to_uri = to_uri.gsub(SCHEME_REPLACEMENT_REGEXP, (adapter&.scheme || from_scheme)) to_uri = to_uri.gsub(EXTENSION_REPLACEMENT_REGEXP, extension) to_uri = to_uri.gsub(BASENAME_REPLACEMENT_REGEXP, basename) to_uri.gsub!(FILENAME_REPLACEMENT_REGEXP, filename) to_uri = "#{to_uri}?#{template_query}" if template_query to_uri end |