diff --git a/.rubocop.yml b/.rubocop.yml index 55c2ba4..e4cc9e0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,21 @@ require: rubocop-sequel +AllCops: + NewCops: enable + +Layout/EmptyLinesAroundClassBody: + EnforcedStyle: 'empty_lines_except_namespace' +Layout/EmptyLinesAroundModuleBody: + EnforcedStyle: 'empty_lines_except_namespace' +Metrics/ClassLength: + Max: 150 +Style/ClassVars: + Enabled: false +Style/GlobalVars: + Enabled: false Style/MethodCallWithoutArgsParentheses: Enabled: false Style/MethodCallWithArgsParentheses: Enabled: true +Style/RedundantReturn: + Enabled: false diff --git a/app/config.rb b/app/config.rb index da2d625..cabd7ee 100644 --- a/app/config.rb +++ b/app/config.rb @@ -1,18 +1,24 @@ +# frozen_string_literal: true + require 'yaml' +# Configuration loader - loads config defaults and an optional config file class Config DEFAULT_CONFIG = 'config/defaults.yaml' def initialize(config_path) - @data = YAML::load_file(DEFAULT_CONFIG) + # Load the default config + @data = YAML.load_file(DEFAULT_CONFIG) - if File.exists?(config_path) - @data.merge!(YAML::load_file(config_path)) - end + # End if the optional config file doesn't exist + return unless File.exist?(config_path) + + # If the optional config exists, load it up! + @data.merge!(YAML.load_file(config_path)) end - def get(key, depth = 0) + def get(key) bits = key.split('.') value = @data diff --git a/app/helpers.rb b/app/helpers.rb index c8291bf..7a21a09 100644 --- a/app/helpers.rb +++ b/app/helpers.rb @@ -1,11 +1,14 @@ +# frozen_string_literal: true + +# ERB view helper functions module Helpers def nullable(value) - if (value) and (value != '') - return value - else - return 'N/a' - end + # Returns the value if it actually exists + return value if value && (value != '') + + # Returns a default 'N/a' string + return 'N/a' end def date_format(date) diff --git a/app/models/channel.rb b/app/models/channel.rb index 39d7288..2a520df 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -1,22 +1,25 @@ +# frozen_string_literal: true + +require 'fileutils' + +# Model handles YouTube channels class Channel < Sequel::Model one_to_many :videos - def ensureDirectoryStructure() + def ensure_directory_structure sub_dirs = ['Archive', 'Channel Documents', 'Main', 'Shorts'] sub_dirs.each do |d| sub_path = File.join( @values[:directory_path], d ) - unless Dir.exist?(sub_path) - Dir.mkdir(sub_path) - end + FileUtils.mkdir_p(sub_path) end end - def openProjects() - return self.videos_dataset.exclude(status: 'published').exclude(archived: true).all() + def open_projects + return videos_dataset.exclude(status: 'published').exclude(archived: true).all() end end diff --git a/app/models/video.rb b/app/models/video.rb index 951fdb9..34adc6e 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,32 +1,35 @@ +# frozen_string_literal: true + +require 'fileutils' require 'kramdown' require 'pandoc-ruby' +# Model handles Video projects class Video < Sequel::Model many_to_one :channel - def ensureDirectoryStructure() - sub_dirs = ['Audio', 'B-Roll', 'Clips', 'Images', 'Export'] + def ensure_directory_structure + sub_dirs = %w[Audio B-Roll Clips Images Export] sub_dirs.each do |d| sub_path = File.join( @values[:directory_path], d ) - unless Dir.exist?(sub_path) - Dir.mkdir(sub_path) - end + FileUtils.mkdir_p(sub_path) end end - def parseScript() - return Kramdown::Document.new(@values[:script]).to_html + def parse_script + Kramdown::Document.new(@values[:script]).to_html end - def importScript() + def import_script scripts = Dir.glob("#{@values[:directory_path]}/*Script.docx") script_content = PandocRuby.convert([scripts[0].dump()], from: :docx, to: :markdown) @values[:script] = script_content - self.save() + # save changes to the model + save_changes() end end diff --git a/app/routes.rb b/app/routes.rb index bfe3a28..40b3048 100644 --- a/app/routes.rb +++ b/app/routes.rb @@ -1,5 +1,7 @@ -require_relative 'routes/index.rb' +# frozen_string_literal: true -require_relative 'routes/channel.rb' +require_relative 'routes/index' -require_relative 'routes/video.rb' +require_relative 'routes/channel' + +require_relative 'routes/video' diff --git a/app/routes/api_v1.rb b/app/routes/api_v1.rb index de4bd16..322b33a 100644 --- a/app/routes/api_v1.rb +++ b/app/routes/api_v1.rb @@ -1,8 +1,11 @@ +# frozen_string_literal: true + class StageManager + # Controller to handle API v1 routes class ApiV1Controller get '/health' do - json :status => 'success' + json status: 'success' end get '/channels' do @@ -16,4 +19,4 @@ class StageManager end end -end \ No newline at end of file +end diff --git a/app/routes/channel.rb b/app/routes/channel.rb index d04fa00..8fef27d 100644 --- a/app/routes/channel.rb +++ b/app/routes/channel.rb @@ -1,4 +1,7 @@ +# frozen_string_literal: true + class StageManager + # Channel that handles channel top-level routes class ChannelController get '/' do @@ -6,16 +9,16 @@ class StageManager end get '/list' do channels = Channel.reverse(:updated_at).all() - erb :'channel/list', :locals => { - :title => 'List of channels', - :channels => channels + erb :'channel/list', locals: { + title: 'List of channels', + channels: channels } end get '/create' do - erb :'channel/create', :locals => { - :title => 'Create new channel', - :base_directory => $conf.get('stgm.base_directory') + erb :'channel/create', locals: { + title: 'Create new channel', + base_directory: $conf.get('stgm.base_directory') } end post '/create' do @@ -27,7 +30,7 @@ class StageManager # create supporting directory structure Dir.mkdir(channel.directory_path) - channel.ensureDirectoryStructure() + channel.ensure_directory_structure() redirect "/channel/#{channel.id}" end @@ -35,18 +38,18 @@ class StageManager get '/:channel_id' do channel = Channel.where(id: params[:channel_id]).first() channel_videos = channel.videos_dataset.reverse(:updated_at).limit(10).all() - erb :'channel/view', :locals => { - :title => channel.name, - :channel => channel, - :channel_videos => channel_videos + erb :'channel/view', locals: { + title: channel.name, + channel: channel, + channel_videos: channel_videos } end get '/:channel_id/edit' do channel = Channel.where(id: params[:channel_id]).first() - erb :'channel/edit', :locals => { - :title => "Editing: #{channel.name}", - :channel => channel + erb :'channel/edit', locals: { + title: "Editing: #{channel.name}", + channel: channel } end post '/:channel_id/edit' do @@ -77,9 +80,9 @@ class StageManager post '/:channel_id/edit/:attr' do # find channel and temporarily save the old channel path channel = Channel.where(id: params[:channel_id]).first() - attrToEdit = params[:attr] + attr_to_edit = params[:attr] - if attrToEdit == 'directory_path' + if attr_to_edit == 'directory_path' File.rename(channel.directory_path, params[:value]) channel.videos.each do |v| video_path = v.directory_path.sub(channel.directory_path, params[:value]) @@ -87,10 +90,10 @@ class StageManager end end - channel[attrToEdit.to_sym] = params[:value] - channel.save() + channel[attr_to_edit.to_sym] = params[:value] + channel.save_changes() - return "success" + return 'success' end end diff --git a/app/routes/index.rb b/app/routes/index.rb index fe3f8e5..4902920 100644 --- a/app/routes/index.rb +++ b/app/routes/index.rb @@ -1,15 +1,18 @@ +# frozen_string_literal: true + class StageManager + # Controller to handle top-level pages class IndexController get '/' do channels = Channel.reverse(:updated_at).limit(10).all() videos = Video.reverse(:updated_at).limit(10).all() active_projects = Video.where(starred: true).reverse(:updated_at).limit($conf.get('stgm.max_stars')) - erb :index, :locals => { - :title => 'Dashboard', - :channels => channels, - :videos => videos, - :active_projects => active_projects + erb :index, locals: { + title: 'Dashboard', + channels: channels, + videos: videos, + active_projects: active_projects } end diff --git a/app/routes/video.rb b/app/routes/video.rb index e7ab7eb..83f35c6 100644 --- a/app/routes/video.rb +++ b/app/routes/video.rb @@ -1,6 +1,9 @@ +# frozen_string_literal: true + require 'fileutils' class StageManager + # Controller to handle top-level video routes class VideoController get '/' do @@ -8,24 +11,21 @@ class StageManager end get '/list' do videos = Video.reverse(:updated_at).all() - erb :'video/list', :locals => { - :title => 'List of videos', - :videos => videos + erb :'video/list', locals: { + title: 'List of videos', + videos: videos } end get '/create' do # check if there's a channel specified - selected_channel = false - if params.has_key?(:channel) - selected_channel = params[:channel].to_i() - end + selected_channel = params[:channel].to_i() if params.key?(:channel) channels = Channel.all() - erb :'video/create', :locals => { - :title => 'Create new video', - :channels => channels, - :selected_channel => selected_channel + erb :'video/create', locals: { + title: 'Create new video', + channels: channels, + selected_channel: selected_channel } end post '/create' do @@ -48,34 +48,34 @@ class StageManager # create supporting directory structure Dir.mkdir(video_path) - video.ensureDirectoryStructure() + video.ensure_directory_structure() redirect "/video/#{video.id}" end get '/:video_id' do video = Video.where(id: params[:video_id]).first() - erb :'video/view', :locals => { - :title => video.name, - :video => video + erb :'video/view', locals: { + title: video.name, + video: video } end get '/:video_id/script' do video = Video.where(id: params[:video_id]).first() - erb :'video/script', :locals => { - :title => "Script: #{video.name}", - :video => video + erb :'video/script', locals: { + title: "Script: #{video.name}", + video: video } end get '/:video_id/edit' do video = Video.where(id: params[:video_id]).first() channels = Channel.all() - erb :'video/edit', :locals => { - :title => "Editing: #{video.name}", - :video => video, - :channels => channels + erb :'video/edit', locals: { + title: "Editing: #{video.name}", + video: video, + channels: channels } end post '/:video_id/edit' do @@ -110,20 +110,20 @@ class StageManager post '/:video_id/edit/:attr' do # find video and temporarily save the old video path video = Video.where(id: params[:video_id]).first() - attrToEdit = params[:attr] + attr_to_edit = params[:attr] # if we update the video's serial, we need to also update the directory path - if attrToEdit == 'serial' + if attr_to_edit == 'serial' old_path = video.directory_path new_path = video.directory_path.sub("##{video.serial}", "##{params[:value]}") File.rename(old_path, new_path) video[:directory_path] = new_path end - video[attrToEdit.to_sym] = params[:value] - video.save() + video[attr_to_edit.to_sym] = params[:value] + video.save_changes() - return "success" + return 'success' end get '/:video_id/archive' do @@ -145,8 +145,9 @@ class StageManager archived: true ) - redirect '/video/' + video.id.to_s() + redirect "/video/#{video.id}" end + get '/:video_id/unarchive' do # find the video video = Video.where(id: params[:video_id]).first() @@ -166,14 +167,14 @@ class StageManager archived: false ) - redirect '/video/' + video.id.to_s() + redirect "/video/#{video.id}" end get '/:video_id/edit/script' do video = Video.where(id: params[:video_id]).first() - erb :'video/edit-script', :locals => { - :title => "Editing script: #{video.name}", - :video => video + erb :'video/edit-script', locals: { + title: "Editing script: #{video.name}", + video: video } end post '/:video_id/edit/script' do diff --git a/scan.rb b/scan.rb index ca95a82..199cd21 100755 --- a/scan.rb +++ b/scan.rb @@ -51,7 +51,7 @@ subs.each do |d| script: "# Introduction\n\n# Body\n\n# Conclusions" ) - video.importScript() + video.import_script() puts "Successfully added video ##{video.serial} - #{video.name} for channel '#{channel.name}' to the database." end diff --git a/server.rb b/server.rb index c1a76bc..d20f855 100644 --- a/server.rb +++ b/server.rb @@ -16,9 +16,16 @@ Dir.glob('./app/models/*.rb').sort().each { |f| require f } # Base Sinatra app class StageManager < Sinatra::Base + @@my_app = {} - def self.new(*) self < StageManager ? super : Rack::URLMap.new(@@my_app) end - def self.map(url) @@my_app[url] = self end + + def self.new(*) + self < StageManager ? super : Rack::URLMap.new(@@my_app) + end + + def + self.map(url) @@my_app[url] = self + end # Enable and configure sessions enable :sessions @@ -44,23 +51,32 @@ class StageManager < Sinatra::Base ## Map controllers # Top-level routes controller class IndexController < StageManager + map '/' + end # Channel routes controller class ChannelController < StageManager + map '/channel' + end # Video routes controller class VideoController < StageManager + map '/video' + end # API v1 controller class ApiV1Controller < StageManager + map '/api/v1' + end + end # Load controllers diff --git a/views/channel/view.erb b/views/channel/view.erb index 0cc6e4c..657c6fd 100644 --- a/views/channel/view.erb +++ b/views/channel/view.erb @@ -20,7 +20,7 @@

<%= channel.directory_path %>

-

Open projects: <%= channel.openProjects().length %>

+

Open projects: <%= channel.open_projects().length %>

Total videos: <%= channel.videos.length %>

diff --git a/views/index.erb b/views/index.erb index 5b3a2c4..5ebfe40 100644 --- a/views/index.erb +++ b/views/index.erb @@ -68,7 +68,7 @@ <% channels.each do |c| %> <%= c.name %> - <%= nullable(c.openProjects().length) %> + <%= nullable(c.open_projects().length) %> <%= nullable(c.videos.length) %> <%= date_format(c.updated_at) %> diff --git a/views/video/script.erb b/views/video/script.erb index 847f5c0..743ac1d 100644 --- a/views/video/script.erb +++ b/views/video/script.erb @@ -4,6 +4,6 @@
- <%= video.parseScript() %> + <%= video.parse_script() %>
diff --git a/views/video/view.erb b/views/video/view.erb index c4d432d..8aa3340 100644 --- a/views/video/view.erb +++ b/views/video/view.erb @@ -41,7 +41,7 @@
- <%= video.parseScript() %> + <%= video.parse_script() %>