From bc799ff2cb9876ff7c109cb65e9bd8253fad63c0 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Wed, 31 Aug 2022 01:22:39 -0400 Subject: [PATCH] Refactored some of the code to match Rubocop style guidelines; will need to create rubocop config to make the style better --- src/config.rb | 41 +++++++++++++++---------------- src/repository.rb | 61 ++++++++++++++++++++++++----------------------- src/transcoder.rb | 35 ++++++++++++++------------- src/util.rb | 12 +++++----- 4 files changed, 76 insertions(+), 73 deletions(-) diff --git a/src/config.rb b/src/config.rb index 71b6bab..45844b5 100644 --- a/src/config.rb +++ b/src/config.rb @@ -1,29 +1,30 @@ require 'toml' +# Loads and handles the application configuration. class Config - # class constructor - def initialize(config_path) - expanded_path = File.expand_path(config_path) - @config = TOML::load_file(expanded_path) + # class constructor + def initialize(config_path) + expanded_path = File.expand_path(config_path) + @config = TOML::load_file(expanded_path) - # just in case the user wants to use a tilde (~) in the repository path... - @config['transcoder']['repository'] = File.expand_path(self.get('transcoder.repository')) - end + # just in case the user wants to use a tilde (~) in the repository path... + @config['transcoder']['repository'] = File.expand_path(self.get('transcoder.repository')) + end - # returns a configuration value from a dot-seperated string, like 'transcoder.interval' - def get(path) - value = @config - bits = path.split('.') - bits.each { |bit| - if (value.has_key?(bit)) - value = value[bit] - else - abort("Configuration value #{path} does exist.") - end - } + # returns a configuration value from a dot-seperated string, like 'transcoder.interval' + def get(path) + value = @config + bits = path.split('.') + bits.each { |bit| + if (value.key?(bit)) + value = value[bit] + else + abort("Configuration value #{path} does exist.") + end + } - return value - end + return value + end end diff --git a/src/repository.rb b/src/repository.rb index a50f9c4..1e97e20 100644 --- a/src/repository.rb +++ b/src/repository.rb @@ -2,44 +2,45 @@ require_relative 'util.rb' require 'fileutils' +# Handles anything pertaining to the video file repository. class Repository - def initialize(path) - @basePath = path + def initialize(path) + @base_path = path - # create repository base directory - createDirectory(@basePath) + # create repository base directory + create_directory(@base_path) - # create sub-directories - subPaths = ['ingest', 'archive', 'output'] - subPaths.each { |p| - createDirectory(File.join(@basePath, p)) - } - end + # create sub-directories + sub_paths = ['ingest', 'archive', 'output'] + sub_paths.each { |p| + create_directory(File.join(@base_path, p)) + } + end - def searchIngest - # create our ingest directory path - ingestPath = File.join(@basePath, 'ingest') - # search for files in ingest; ignore non-files (e.g. directories) - ingestFiles = Dir.entries(ingestPath).select { |f| File.file? File.join(ingestPath, f) } + def search_ingest + # create our ingest directory path + ingest_path = File.join(@base_path, 'ingest') + # search for files in ingest; ignore non-files (e.g. directories) + ingest_files = Dir.entries(ingest_path).select { |f| File.file? File.join(ingest_path, f) } - return ingestFiles - end + return ingest_files + end - def archiveFile(filename) - # create source and destination paths for the copy - ingestPath = File.join(@basePath, 'ingest', filename) - archivePath = File.join(@basePath, 'archive', filename) + def archive_file(filename) + # create source and destination paths for the copy + ingest_path = File.join(@base_path, 'ingest', filename) + archive_path = File.join(@base_path, 'archive', filename) - # perform the copy, preserving file attributes - FileUtils.cp(ingestPath, archivePath, preserve: true) - end + # perform the copy, preserving file attributes + FileUtils.cp(ingest_path, archive_path, preserve: true) + end - def cleanupFile(filename) - # create ingest path - ingestPath = File.join(@basePath, 'ingest', filename) - # remove the file - FileUtils.remove_file(ingestPath) - end + def cleanup_file(filename) + # create ingest path + ingest_path = File.join(@base_path, 'ingest', filename) + # remove the file + FileUtils.remove_file(ingest_path) + end end diff --git a/src/transcoder.rb b/src/transcoder.rb index 0b6c5c9..a12375a 100644 --- a/src/transcoder.rb +++ b/src/transcoder.rb @@ -1,23 +1,24 @@ +# Handles the actual transcoding. class Transcoder - def initialize(config, repository) - @config = config - @repository = repository - end + def initialize(config, repository) + @config = config + @repository = repository + end - def start - puts "Starting transcoder..." + def start + puts 'Starting transcoder...' - # search for files in ingest - ingestFiles = @repository.searchIngest() - ingestFiles.each { |ifile| - # archive the file - @repository.archiveFile(ifile) - # perform the transcode - # // TODO self.transcode(ifile) - # clean up the file from ingest - @repository.cleanupFile(ifile) - } - end + # search for files in ingest + ingest_files = @repository.search_ingest() + ingest_files.each { |ifile| + # archive the file + @repository.archive_file(ifile) + # perform the transcode + # // TODO self.transcode(ifile) + # clean up the file from ingest + @repository.cleanup_file(ifile) + } + end end diff --git a/src/util.rb b/src/util.rb index 743357e..18ad104 100644 --- a/src/util.rb +++ b/src/util.rb @@ -1,7 +1,7 @@ -def createDirectory(path) - if Dir.exists?(path) - puts "Directory #{path} already exists." - else - puts "Creating directory #{path}." - end +def create_directory(path) + if Dir.exist?(path) + puts "Directory #{path} already exists." + else + puts "Creating directory #{path}." + end end