From 85750b4de4d64cdb815d3d1c5298ff9c355f338d Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 10 Mar 2023 12:10:44 -0500 Subject: [PATCH] Added rubocop to project and started cleaning up some code style issues --- .rubocop.yml | 6 ++++++ Gemfile | 11 ++++++++--- Gemfile.lock | 25 +++++++++++++++++++++++++ Rakefile | 6 ++++++ scan.rb | 45 +++++++++++++++++++++++---------------------- server.rb | 27 ++++++++++++++++++--------- 6 files changed, 86 insertions(+), 34 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..55c2ba4 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,6 @@ +require: rubocop-sequel + +Style/MethodCallWithoutArgsParentheses: + Enabled: false +Style/MethodCallWithArgsParentheses: + Enabled: true diff --git a/Gemfile b/Gemfile index e370834..d289c37 100644 --- a/Gemfile +++ b/Gemfile @@ -11,7 +11,12 @@ gem 'kramdown', '~> 2.4' gem 'pandoc-ruby', '~> 2.1' -# Use rerun gem to auto-reload app -gem 'guard-rack' -gem 'wdm', '>= 0.1.0' if Gem.win_platform? +group :development, :test do + # Use guard-rack gem to auto-reload app + gem 'guard-rack' + gem 'wdm', '>= 0.1.0' if Gem.win_platform? + # rubocop and extensions for code style + gem 'rubocop' + gem 'rubocop-sequel' +end diff --git a/Gemfile.lock b/Gemfile.lock index 0008e01..9bacc58 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + ast (2.4.2) coderay (1.1.3) ffi (1.15.5) ffi (1.15.5-x64-mingw-ucrt) @@ -18,6 +19,7 @@ GEM ffi guard (~> 2.3) spoon + json (2.6.3) kramdown (2.4.0) rexml listen (3.8.0) @@ -34,6 +36,9 @@ GEM nenv (~> 0.1) shellany (~> 0.0) pandoc-ruby (2.1.7) + parallel (1.22.1) + parser (3.2.1.1) + ast (~> 2.4.1) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -42,10 +47,27 @@ GEM rack (2.2.6.2) rack-protection (3.0.5) rack + rainbow (3.1.1) rb-fsevent (0.11.2) rb-inotify (0.10.1) ffi (~> 1.0) + regexp_parser (2.7.0) rexml (3.2.5) + rubocop (1.48.0) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.2.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.26.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.27.0) + parser (>= 3.2.1.0) + rubocop-sequel (0.3.4) + rubocop (~> 1.0) + ruby-progressbar (1.13.0) ruby2_keywords (0.0.5) sequel (5.66.0) shellany (0.0.1) @@ -66,6 +88,7 @@ GEM sqlite3 (1.6.1-x86_64-linux) thor (1.2.1) tilt (2.1.0) + unicode-display_width (2.4.2) PLATFORMS x64-mingw-ucrt @@ -76,6 +99,8 @@ DEPENDENCIES kramdown (~> 2.4) pandoc-ruby (~> 2.1) puma (~> 6.1) + rubocop + rubocop-sequel sequel (~> 5.66) sinatra (~> 3.0) sinatra-contrib (~> 3.0) diff --git a/Rakefile b/Rakefile index 9e192ae..3564db3 100644 --- a/Rakefile +++ b/Rakefile @@ -23,3 +23,9 @@ namespace :server do %x{guard} end end + +namespace :test do + task :rubocop do + system("rubocop app/ server.rb scan.rb") + end +end diff --git a/scan.rb b/scan.rb index 3f9afe9..ca95a82 100755 --- a/scan.rb +++ b/scan.rb @@ -1,24 +1,24 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + require 'pathname' require 'sequel' -require_relative 'lib/config.rb' +require_relative 'app/config' # Load configuration file -$conf = Config.new(File.join(__dir__, 'data/defaults.yaml')) +conf = Config.new(File.join(__dir__, 'data/defaults.yaml')) # Load the Sequel timestamps plugin -Sequel::Model.plugin :timestamps +Sequel::Model.plugin(:timestamps) # Initialize Sequel gem for database actions -DB = Sequel.connect(adapter: $conf.get('database.adapter'), database: $conf.get('database.database')) +DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database')) # Load models -require_relative 'lib/models/channel.rb' -require_relative 'lib/models/video.rb' +Dir.glob('app/models/*.rb').sort().each { |f| require f } -unless ARGV.length == 1 - abort 'You must supply a channel name!' -end +# fail if the script is being called incorrectly +abort('You must supply a channel name!') unless ARGV.length == 1 channel = Channel.where(name: ARGV[0]).first() channel_dir = File.join(channel.directory_path, 'Main') @@ -30,7 +30,7 @@ subs.each do |d| # parse video serial from folder name serial_raw = dir_name[0..5].strip() - video_serial = serial_raw[1..-1] + video_serial = serial_raw.slice(0) # parse video name from folder name video_name = dir_name.split(' - ')[1] @@ -38,19 +38,20 @@ subs.each do |d| # check if a video by the same serial number exists for the channel db_results = Video.where(serial: video_serial, channel_id: channel.id).all() + # skip to next video if the video already exists + next unless db_results.empty?() + # add video project to DB if there's no existing project - if db_results.length == 0 - video = Video.create( - serial: video_serial, - name: video_name, - channel_id: channel.id, - directory_path: d, - description: 'TODO - imported from storage.', - script: "# Introduction\n\n# Body\n\n# Conclusions" - ) + video = Video.create( + serial: video_serial, + name: video_name, + channel_id: channel.id, + directory_path: d, + description: 'TODO - imported from storage.', + script: "# Introduction\n\n# Body\n\n# Conclusions" + ) - video.importScript() + video.importScript() - puts "Successfully added video ##{video.serial} - #{video.name} for channel '#{channel.name}' to the database." - end + 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 0a69e43..c1a76bc 100644 --- a/server.rb +++ b/server.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'logger' require 'sequel' require 'sqlite3' @@ -6,12 +8,13 @@ require 'sinatra/json' require 'rack/protection' # Load the Sequel timestamps plugin -Sequel::Model.plugin :timestamps +Sequel::Model.plugin(:timestamps) # Initialize Sequel gem for database actions DB = Sequel.connect(adapter: $conf.get('database.adapter'), database: $conf.get('database.database')) # Load models -Dir.glob('./app/models/*.rb').each { |f| require f } +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 @@ -25,34 +28,40 @@ class StageManager < Sinatra::Base # Set up static file serving enable :static - set :public_folder, __dir__ + '/public' + set :public_folder, File.join(__dir__, '/public') # Set up our view engine - set :views, settings.root + '/views' + set :views, File.join(settings.root, '/views') # Initialize logging - logger = Logger.new(STDOUT) + logger = Logger.new($stdout) logger.level = Logger::INFO # Load helper functions - require_relative 'app/helpers.rb' + require_relative 'app/helpers' helpers Helpers - # Map controllers + ## 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 controllers + + # API v1 controller class ApiV1Controller < StageManager map '/api/v1' end end # Load controllers -Dir.glob('./app/routes/*.rb').each { |f| require f } +Dir.glob('./app/routes/*.rb').sort().each { |f| require f }