Class: Colore::LegacyConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/legacy_converter.rb

Overview

The Colore Legacy Converter provides the conversion functionality from the original Heathen application. Conversions are performed foreground (unlike the original, where conversion takes place in the follow-up URL call). The converted files are saved to a special disk area. A sidekiq task will clear files older than a certain age.

Constant Summary collapse

LEGACY =
'legacy'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_dir = C_.storage_directory) ⇒ LegacyConverter


17
18
19
20
21
# File 'lib/legacy_converter.rb', line 17

def initialize storage_dir = C_.storage_directory
  @storage_dir = Pathname.new(storage_dir)
  @legacy_dir = @storage_dir + LEGACY
  @legacy_dir.mkpath
end

Instance Attribute Details

#legacy_dirObject (readonly)

Returns the value of attribute legacy_dir


15
16
17
# File 'lib/legacy_converter.rb', line 15

def legacy_dir
  @legacy_dir
end

Instance Method Details

#convert_file(action, orig_content, language = 'en') ⇒ String

Converts the given file and stores it in the legacy directory


28
29
30
31
32
33
# File 'lib/legacy_converter.rb', line 28

def convert_file action, orig_content, language='en'
  content = Heathen::Converter.new.convert(action, orig_content, language)
  filename = Digest::SHA2.hexdigest content
  store_file filename, content
  (@legacy_dir.basename + filename).to_s
end

#get_file(filename) ⇒ Object

Loads and returns a legacy converted file


41
42
43
44
# File 'lib/legacy_converter.rb', line 41

def get_file filename
  raise "File does not exists" unless (@legacy_dir + filename).file?
  File.read(@legacy_dir + filename)
end

#store_file(filename, content) ⇒ Object

Stores the specified file in the legacy directory


36
37
38
# File 'lib/legacy_converter.rb', line 36

def store_file filename, content
  File.open(@legacy_dir + filename, 'wb') { |f| f.write content }
end