Class: DerivativeRodeo::Services::ConvertUriViaTemplateService

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, **options)
  @from_uri = from_uri
  @template = template
  @adapter = adapter
  @separator = options.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 = options[:filename] || @parts[-1]
  @basename = options[: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 = options[: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

#adapterObject

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

#basenameObject

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_partsObject

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

#extensionObject

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

#filenameObject

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_schemeObject

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_uriObject

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

#partsObject

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

#pathObject

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

#separatorString

Returns the directory seperator character; default: “/”.

Returns:

  • (String)

    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

#templateObject

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_queryObject

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_queryObject

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

#uriObject

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.

Examples:

DerivativeRodeo::Services::ConvertUriViaTemplateService.call(
  from_uris: ["file:///path1/A/file.pdf", "file:///path2/B/file.pdf"],
  template: "file:///dest1/{{dir_parts[-2..-1]}}/{{filename}}")
=> ["file:///dest1/path2/A/file.pdf", "file:///dest1/path2/B/file.pdf"]

DerivativeRodeo::Services::ConvertUriViaTemplateService.call(
  from_uris: ["file:///path1/A/file.pdf", "aws:///path2/B/file.pdf"],
  template: "file:///dest1/{{dir_parts[-1..-1]}}/{{ filename }}")
=> ["file:///dest1/A/file.pdf", "aws:///dest1/B/file.pdf"]

Parameters:

  • from_uri (String)

    Of the form “scheme://dir/parts/basename.extension”

  • template (String)

    Another URI that may contain path_parts or scheme template values.

  • adapter (StorageLocations::Location) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Returns:

  • (String)


59
60
61
# File 'lib/derivative_rodeo/services/convert_uri_via_template_service.rb', line 59

def self.call(from_uri:, template:, adapter: nil, **options)
  new(from_uri: from_uri, template: template, adapter: adapter, **options).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.

Parameters:

  • template (String)

Returns:

  • (String)

See Also:



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

#callObject

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