diff --git a/.github/dependabot.yml b/.github/dependabot.yml index edd526c..6151513 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -13,4 +13,4 @@ updates: time: "12:00" timezone: "Europe/Berlin" assignees: - - "simonneutert" \ No newline at end of file + - "simonneutert" diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 4466911..bb5e2bd 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -38,6 +38,6 @@ jobs: bundle config path vendor/bundle bundle install --jobs 4 --retry 3 - name: Run tests - run: bundle exec rake + run: bundle exec rubocop && bundle exec rake env: RACK_ENV: test diff --git a/.rubocop b/.rubocop new file mode 100644 index 0000000..bec972c --- /dev/null +++ b/.rubocop @@ -0,0 +1 @@ +--server \ No newline at end of file diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..ac39c31 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,33 @@ +# The behavior of RuboCop can be controlled via the .rubocop.yml +# configuration file. It makes it possible to enable/disable +# certain cops (checks) and to alter their behavior if they accept +# any parameters. The file can be placed either in your home +# directory or in some project directory. +# +# RuboCop will start looking for the configuration file in the directory +# where the inspected file is and continue its way up to the root directory. +# +# See https://docs.rubocop.org/rubocop/configuration + + +AllCops: + TargetRubyVersion: 3.0 + NewCops: enable + +Metrics/AbcSize: + Exclude: + - test/**/* + +Style/Documentation: + Enabled: false + +Metrics/ClassLength: + Exclude: + - app.rb + +Metrics/BlockLength: + Exclude: + - app.rb + +Metrics/MethodLength: + Max: 20 \ No newline at end of file diff --git a/Gemfile b/Gemfile index cfe179a..7d278d3 100644 --- a/Gemfile +++ b/Gemfile @@ -2,16 +2,14 @@ source 'https://rubygems.org' +gem 'puma', '~> 6.4' gem 'rack-unreloader', '~> 2.1' gem 'rackup', '~> 2.1.0' gem 'rake', '~> 13.0' -gem 'roda', '~> 3.73' -gem 'tilt', '~> 2.3' - gem 'redcarpet', '~> 3.6' +gem 'roda', '~> 3.73' gem 'tantiny', '0.3.3' - -gem 'puma', '~> 6.4' +gem 'tilt', '~> 2.3' group :development do gem 'htmlbeautifier', require: false diff --git a/Rakefile b/Rakefile index 25ca87a..895cab2 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'minitest' require 'minitest/test_task' diff --git a/app.rb b/app.rb index 8a66233..f9c6f23 100644 --- a/app.rb +++ b/app.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true - # TODO: write a method for whitelist checks of media type MEDIA_WHITELIST = %w[txt pdf md png jpg jpeg heic webp yml yaml json] .map { |c| [c.upcase, c] } @@ -19,8 +18,10 @@ class App < Roda plugin :render, layout: './layout' plugin :view_options plugin :all_verbs + # rubocop:disable Layout/LineLength plugin :sessions, key: 'labradorite', secret: ENV.delete('SESSION_SECRET') || 'labradoritelabradoritelabradoritelabradoritelabradoritelabradorite' + # rubocop:enable Layout/LineLength plugin :caching plugin :json plugin :json_parser @@ -181,7 +182,7 @@ class App < Roda new_memo = FileOperations::NewMemoGenerator.new new_memo.generate r.redirect "/#{new_memo.path}/edit" - rescue StandardError => e + rescue StandardError r.redirect '/memos/new' end diff --git a/lib/controllers/memos/search.rb b/lib/controllers/memos/search.rb index dc21d59..3a26298 100644 --- a/lib/controllers/memos/search.rb +++ b/lib/controllers/memos/search.rb @@ -5,8 +5,8 @@ module Memos class Search attr_reader :r, :meta, :meta_ostruct, :index - def initialize(r, index) - @params = Helper::DeepCopy.create(r.params) + def initialize(req, index) + @params = Helper::DeepCopy.create(req.params) @index = index @search_input = @params['search'] end diff --git a/lib/controllers/memos/update.rb b/lib/controllers/memos/update.rb index e532117..405ca3e 100644 --- a/lib/controllers/memos/update.rb +++ b/lib/controllers/memos/update.rb @@ -5,8 +5,8 @@ module Memos class Update attr_reader :r, :meta, :meta_ostruct, :index - def initialize(r, index, memo_path, meta) - @params = Helper::DeepCopy.create(r.params) + def initialize(req, index, memo_path, meta) + @params = Helper::DeepCopy.create(req.params) @index = index @memo_path = memo_path @meta = meta diff --git a/lib/file_operations/attachments/deleter.rb b/lib/file_operations/attachments/deleter.rb index 67298cb..cca0eae 100644 --- a/lib/file_operations/attachments/deleter.rb +++ b/lib/file_operations/attachments/deleter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module FileOperations module Attachments class Deleter diff --git a/lib/file_operations/delete_memo.rb b/lib/file_operations/delete_memo.rb index ffd6b24..b9ff706 100644 --- a/lib/file_operations/delete_memo.rb +++ b/lib/file_operations/delete_memo.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module FileOperations # # Deletes a Memo and its Meta Data and Attachments diff --git a/lib/file_operations/files_sort_by_latest_modified.rb b/lib/file_operations/files_sort_by_latest_modified.rb index 3c60697..45b14f6 100644 --- a/lib/file_operations/files_sort_by_latest_modified.rb +++ b/lib/file_operations/files_sort_by_latest_modified.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module FileOperations # # Scans the filesystem and returns the latest modified file(paths) @@ -10,9 +12,9 @@ class FilesSortByLatestModified # # @return [Array] file paths from latest to touch to least touched # - def latest_n_memos_by_file_modified(n) + def latest_n_memos_by_file_modified(num) all_files_order_from_least_to_newest_modified - .last(n) + .last(num) .map! { |file_path| File.dirname(file_path) } .reverse end diff --git a/lib/file_operations/memos/reload.rb b/lib/file_operations/memos/reload.rb index 7f6ff73..4ea505d 100644 --- a/lib/file_operations/memos/reload.rb +++ b/lib/file_operations/memos/reload.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Controllers module Memos class Reload diff --git a/lib/file_operations/meta_data_file_reader.rb b/lib/file_operations/meta_data_file_reader.rb index cc97598..92ab0ff 100644 --- a/lib/file_operations/meta_data_file_reader.rb +++ b/lib/file_operations/meta_data_file_reader.rb @@ -8,7 +8,7 @@ def from_path(path_string) end def hash_to_ostruct(meta_data) - OpenStruct.new(meta_data) + OpenStruct.new(meta_data) # rubocop:disable Style/OpenStructUse end # diff --git a/lib/file_operations/new_memo_generator.rb b/lib/file_operations/new_memo_generator.rb index b7065a6..c6e6245 100644 --- a/lib/file_operations/new_memo_generator.rb +++ b/lib/file_operations/new_memo_generator.rb @@ -1,8 +1,12 @@ +# frozen_string_literal: true + module FileOperations class NewMemoGenerator attr_reader :slug, :path - def initialize; end + def initialize + # overwrite this, if needed + end def generate @slug = Helper::SimpleUidGenerator.generate diff --git a/lib/helper/simple_uid_generator.rb b/lib/helper/simple_uid_generator.rb index 813610d..8ff9069 100644 --- a/lib/helper/simple_uid_generator.rb +++ b/lib/helper/simple_uid_generator.rb @@ -6,7 +6,7 @@ class << self def generate 8.times.map do ('a'..'z').to_a.sample - end.join('').insert(4, '-') + end.join.insert(4, '-') end end end diff --git a/lib/search_index/core.rb b/lib/search_index/core.rb index c98e6eb..de85b7b 100644 --- a/lib/search_index/core.rb +++ b/lib/search_index/core.rb @@ -55,7 +55,8 @@ def recreate_index! # TODO: extract paths to constants def extract_data_from_files Dir.glob(MARKDOWN_DIR).map do |file| - meta = YAML.safe_load_file(file.gsub(MARKDOWN_FILENAME, META_FILENAME), permitted_classes: [Date, Time, DateTime]) + meta = YAML.safe_load_file(file.gsub(MARKDOWN_FILENAME, META_FILENAME), + permitted_classes: [Date, Time, DateTime]) content = File.read(file) data = meta.merge({ 'content' => content }) map_data_to_schema(data) diff --git a/test/default_memos_test.rb b/test/default_memos_test.rb index ad887aa..6ff40ec 100644 --- a/test/default_memos_test.rb +++ b/test/default_memos_test.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'minitest/autorun' -require_relative './test_helper' +require_relative 'test_helper' class TestMemosOnFilesystem < Minitest::Test def test_memos_default diff --git a/test/endpoints_api_v1_test.rb b/test/endpoints_api_v1_test.rb index 5aa5709..6bd300c 100644 --- a/test/endpoints_api_v1_test.rb +++ b/test/endpoints_api_v1_test.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'minitest/autorun' -require_relative './test_helper' +require_relative 'test_helper' class TestEndpointsApiV1 < Minitest::Test OUTER_APP = Rack::Builder.parse_file('config.ru') @@ -26,6 +28,7 @@ def test_memos_tantiny_reload assert_equal json['status'], 'success' end + # rubocop:disable Layout/LineLength def test_memos_tantiny_search post '/api/v1/memos/reload' @@ -45,6 +48,7 @@ def test_memos_tantiny_search "![](/memos/2021/08/21/hgfe-dcba/665507228-pug.jpg)\r"], first_result_triplet.last assert_kind_of Array, first_result_triplet.last end + # rubocop:enable Layout/LineLength def test_memos_tantiny_search_without_match post '/api/v1/memos/reload' @@ -71,6 +75,7 @@ def test_memos_markdown_to_html_preview assert_equal json['md'], "

Labradorite

\n" end + # rubocop:disable Layout/LineLength def test_memos_update_memo post('/api/v1/memos/2021/08/21/hgfe-dcba/update', { 'title' => 'Facts about Pugsies!', 'tags' => 'dog,pug,pet', @@ -80,4 +85,5 @@ def test_memos_update_memo assert_equal last_response.status, 200 assert_equal JSON.parse(last_response.body)['status'], 'success' end + # rubocop:enable Layout/LineLength end diff --git a/test/endpoints_get_test.rb b/test/endpoints_get_test.rb index 73fa1d9..21876e9 100644 --- a/test/endpoints_get_test.rb +++ b/test/endpoints_get_test.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'minitest/autorun' -require_relative './test_helper' +require_relative 'test_helper' class TestEndpointsGet < Minitest::Test OUTER_APP = Rack::Builder.parse_file('config.ru') diff --git a/test/meta_data_serializer_test.rb b/test/meta_data_serializer_test.rb index 999b853..cbfb20a 100644 --- a/test/meta_data_serializer_test.rb +++ b/test/meta_data_serializer_test.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'minitest/autorun' require_relative 'test_helper' class TestMetaDataSerializing < Minitest::Test def yaml_string - yaml_string = ''' + ' --- id: 2021/08/21/hgfe-dcba title: Facts about Pugs! @@ -13,7 +15,7 @@ def yaml_string - http://localhost:9292/memos/2022/09/25/abcd-efgh/edit - http://localhost:9292/memos/2022/09/24/abcd-efgh/edit updated_at: !ruby/object:DateTime 2021-08-21 17:29:58.161601000 +02:00 -''' +' end def setup diff --git a/test/simple_uid_generator_test.rb b/test/simple_uid_generator_test.rb index 04b401e..000a559 100644 --- a/test/simple_uid_generator_test.rb +++ b/test/simple_uid_generator_test.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'minitest/autorun' -require_relative './test_helper' +require_relative 'test_helper' class TestSimpleUidGenerator < Minitest::Test def setup diff --git a/test/test_helper.rb b/test/test_helper.rb index e744246..88faa82 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + dev = ENV['RACK_ENV'] == 'development' require 'yaml'