Skip to content

Commit

Permalink
implements rubocop and updates gems (#86)
Browse files Browse the repository at this point in the history
* rubocop and gems

* support ruby 3.0
  • Loading branch information
simonneutert authored Oct 15, 2023
1 parent 0623982 commit e9d2000
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ updates:
time: "12:00"
timezone: "Europe/Berlin"
assignees:
- "simonneutert"
- "simonneutert"
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions .rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--server
33 changes: 33 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 3 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'minitest'
require 'minitest/test_task'

Expand Down
5 changes: 3 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -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] }
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/controllers/memos/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/controllers/memos/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/file_operations/attachments/deleter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module FileOperations
module Attachments
class Deleter
Expand Down
2 changes: 2 additions & 0 deletions lib/file_operations/delete_memo.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module FileOperations
#
# Deletes a Memo and its Meta Data and Attachments
Expand Down
6 changes: 4 additions & 2 deletions lib/file_operations/files_sort_by_latest_modified.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module FileOperations
#
# Scans the filesystem and returns the latest modified file(paths)
Expand All @@ -10,9 +12,9 @@ class FilesSortByLatestModified
#
# @return [Array<String>] 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
Expand Down
2 changes: 2 additions & 0 deletions lib/file_operations/memos/reload.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Controllers
module Memos
class Reload
Expand Down
2 changes: 1 addition & 1 deletion lib/file_operations/meta_data_file_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down
6 changes: 5 additions & 1 deletion lib/file_operations/new_memo_generator.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/helper/simple_uid_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/search_index/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion test/default_memos_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion test/endpoints_api_v1_test.rb
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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'

Expand All @@ -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'
Expand All @@ -71,6 +75,7 @@ def test_memos_markdown_to_html_preview
assert_equal json['md'], "<h1>Labradorite</h1>\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',
Expand All @@ -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
4 changes: 3 additions & 1 deletion test/endpoints_get_test.rb
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
6 changes: 4 additions & 2 deletions test/meta_data_serializer_test.rb
Original file line number Diff line number Diff line change
@@ -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!
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/simple_uid_generator_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dev = ENV['RACK_ENV'] == 'development'

require 'yaml'
Expand Down

0 comments on commit e9d2000

Please sign in to comment.