summaryrefslogtreecommitdiff
path: root/new/lib
diff options
context:
space:
mode:
authormms <git@sapka.me>2025-01-08 21:24:33 +0100
committermms <git@sapka.me>2025-01-08 21:24:33 +0100
commit4004a55b0e324c35cbc7d58b831e49efd484ab93 (patch)
tree3ff4c68c28165b6d08c1a49532e8421193433d65 /new/lib
parent2781360c7c25404c5a1fd03ed3472d43367ed8c6 (diff)
feat: rubocop
Diffstat (limited to 'new/lib')
-rw-r--r--new/lib/crys.rb21
-rw-r--r--new/lib/crys/batch_builder.rb37
-rw-r--r--new/lib/crys/builder.rb14
-rw-r--r--new/lib/crys/deployer.rb38
-rw-r--r--new/lib/crys/image_processor.rb12
-rw-r--r--new/lib/crys/pages_db_manager.rb26
-rw-r--r--new/lib/crys/processors/common_functions.rb11
-rw-r--r--new/lib/crys/processors/common_parts.rb14
-rw-r--r--new/lib/crys/processors/html_processor.rb13
-rw-r--r--new/lib/crys/processors/rss_processor.rb8
-rw-r--r--new/lib/crys/server.rb16
11 files changed, 124 insertions, 86 deletions
diff --git a/new/lib/crys.rb b/new/lib/crys.rb
index bd0d298c..eea74fa7 100644
--- a/new/lib/crys.rb
+++ b/new/lib/crys.rb
@@ -1,15 +1,16 @@
-require 'erb'
-require 'rss'
+# frozen_string_literal: true
-require_relative "crys/processors/html_processor.rb"
-require_relative "crys/processors/rss_processor.rb"
+require 'bundler/setup'
-require_relative "crys/server.rb"
-require_relative "crys/builder.rb"
-require_relative "crys/batch_builder.rb"
-require_relative "crys/image_processor.rb"
-require_relative "crys/pages_db_manager.rb"
+require_relative 'crys/processors/html_processor'
+require_relative 'crys/processors/rss_processor'
+
+require_relative 'crys/server'
+require_relative 'crys/builder'
+require_relative 'crys/batch_builder'
+require_relative 'crys/image_processor'
+require_relative 'crys/pages_db_manager'
+require_relative 'crys/deployer'
module Crys
end
-
diff --git a/new/lib/crys/batch_builder.rb b/new/lib/crys/batch_builder.rb
index 61fdcf22..43dbec62 100644
--- a/new/lib/crys/batch_builder.rb
+++ b/new/lib/crys/batch_builder.rb
@@ -1,34 +1,31 @@
+# frozen_string_literal: true
+
module Crys
class BatchBuilder
- def db_path
- output_dir = "#{File.dirname(File.dirname(File.dirname(__FILE__)))}/db/pages.yaml"
- end
-
- def output_dir
- "#{File.dirname(File.dirname(__FILE__))}/output"
- end
-
- def image_dir
- "#{File.dirname(File.dirname(File.expand_path(__FILE__)))}/assets/images"
+ def initialize(pages_db:, output_dir:, image_dir:, builder_class:)
+ @pages_db = pages_db
+ @output_dir = output_dir
+ @image_dir = image_dir
+ @builder_class = builder_class
end
- def builder_class
- Crys::Builder
- end
-
- def db
- Crys::PagesDbManager.new
- end
-
def build
db.pages.each do |page|
builder_class.new(
file_path: page.file_path,
output_dir: output_dir,
- image_dir: image_dir,
+ image_dir: image_dir
).build
- puts "processed: " + page.filename
+ puts "processed: #{page.filename}"
end
end
+
+ private
+
+ attr_reader :pages_db, :output_dir, :image_dir, :builder_class
+
+ def db
+ pages_db.new
+ end
end
end
diff --git a/new/lib/crys/builder.rb b/new/lib/crys/builder.rb
index b8135c77..3b1ed388 100644
--- a/new/lib/crys/builder.rb
+++ b/new/lib/crys/builder.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
module Crys
class Builder
def initialize(file_path:, output_dir:, image_dir:)
@@ -20,20 +22,20 @@ module Crys
def pages_db_manager
Crys::PagesDbManager.new
end
-
+
def process_assets
processor.assets.each do |asset|
- output_path = output_dir + "/" + asset.relative_path
- File.open(output_path, 'w') { |file| file.write(asset.processed_asset) }
+ output_path = "#{output_dir}/#{asset.relative_path}"
+ File.write(output_path, asset.processed_asset)
end
end
def process_content
content = processor.parsed_content
filename = processor.filename
- output_path = output_dir + "/" + filename
+ output_path = "#{output_dir}/#{filename}"
- File.open(output_path, 'w') { |file| file.write(content) }
+ File.write(output_path, content)
end
def processor_class
@@ -43,7 +45,7 @@ module Crys
when /xml.rb$/
rss_processor
else
- raise StandardError.new("No processor for #{file_path}")
+ raise StandardError, "No processor for #{file_path}"
end
end
diff --git a/new/lib/crys/deployer.rb b/new/lib/crys/deployer.rb
new file mode 100644
index 00000000..501ea046
--- /dev/null
+++ b/new/lib/crys/deployer.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+require 'rsync'
+
+module Crys
+ class Deployer
+ DEST_PREFIX = '/usr/local/sites/'
+
+ def initialize(dest:, source:)
+ @dest = dest
+ @source = source
+
+ Rsync.configure do |config|
+ config.host = 'mms@10.0.7.0'
+ end
+ end
+
+ def run
+ puts "source: #{source}"
+ full_dest = DEST_PREFIX + dest
+ puts "dest: #{full_dest}"
+
+ Rsync.run(source, full_dest, ['-rtz']) do |result|
+ if result.success?
+ result.changes.each do |change|
+ puts "#{change.filename} (#{change.summary})"
+ end
+ else
+ puts result.error
+ end
+ end
+ end
+
+ private
+
+ attr_reader :dest, :source
+ end
+end
diff --git a/new/lib/crys/image_processor.rb b/new/lib/crys/image_processor.rb
index b7e2fd67..306c0769 100644
--- a/new/lib/crys/image_processor.rb
+++ b/new/lib/crys/image_processor.rb
@@ -1,26 +1,24 @@
+# frozen_string_literal: true
+
module Crys
class ImageProcessor
def initialize(path:, filename:, width: :auto)
@path = path
@filename = filename
@file = File.read(path)
+ @width = width
end
def relative_path
filename
end
- def filename
- filename
- end
-
def processed_asset
file.to_s
end
- private
+ private
- attr_reader :path, :filename, :file
-
+ attr_reader :path, :filename, :file
end
end
diff --git a/new/lib/crys/pages_db_manager.rb b/new/lib/crys/pages_db_manager.rb
index 6a5e71c9..029b0352 100644
--- a/new/lib/crys/pages_db_manager.rb
+++ b/new/lib/crys/pages_db_manager.rb
@@ -1,10 +1,11 @@
+# frozen_string_literal: true
+
module Crys
class PagesDbManager
PAGES_KEY = :pages
- PAGE = Struct.new(:filename, :file_path, :last_update, :in_rss, keyword_init: true)
+ PAGE = Struct.new(:filename, :file_path, :last_update, :in_rss, keyword_init: true)
- def db_path
- end
+ def db_path; end
def add_page(processor)
@processor = processor
@@ -16,7 +17,7 @@ module Crys
end
def pages
- db.fetch(:pages).map do | _, data|
+ db.fetch(:pages).map do |_, data|
PAGE.new(data)
end
end
@@ -25,7 +26,6 @@ module Crys
attr_reader :hash, :processor
-
def db
@db ||= YAML.load_file(
db_path,
@@ -34,15 +34,9 @@ module Crys
end
def upsert_db(hash, known_page)
- if known_page
- record = PAGE.new(known_page)
- else
- record = PAGE.new(
- filename: processor.filename,
- file_path: processor.file_path,
- in_rss: true
- )
- end
+ record = PAGE.new(known_page || { filename: processor.filename,
+ file_path: processor.file_path,
+ in_rss: true })
record.last_update = Time.now
@@ -50,9 +44,7 @@ module Crys
end
def save_db
- File.open(db_path, 'w') do |f|
- f.write db.to_yaml
- end
+ File.write(db_path, db.to_yaml)
end
def known_page(hash)
diff --git a/new/lib/crys/processors/common_functions.rb b/new/lib/crys/processors/common_functions.rb
index f33c7681..9cf56692 100644
--- a/new/lib/crys/processors/common_functions.rb
+++ b/new/lib/crys/processors/common_functions.rb
@@ -1,13 +1,15 @@
+# frozen_string_literal: true
+
require 'rss'
module Crys
module CommonFunctions
def project_root
- "#{File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))))}"
+ File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))))).to_s
end
def process_image(file:, width: :auto)
- full_path = image_dir + "/" + file
+ full_path = "#{image_dir}/#{file}"
image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: width)
assets << image
@@ -19,10 +21,13 @@ module Crys
if name
path = case name
when :main
- "/public/index.xml"
+ '/public/index.xml'
end
rss = File.read(project_root + path)
+ else
+ puts url
+
end
RSS::Parser.parse(rss)
diff --git a/new/lib/crys/processors/common_parts.rb b/new/lib/crys/processors/common_parts.rb
index 4e1dae56..a6c2dc16 100644
--- a/new/lib/crys/processors/common_parts.rb
+++ b/new/lib/crys/processors/common_parts.rb
@@ -1,18 +1,18 @@
+# frozen_string_literal: true
+
module Crys
module CommonParts
def webbutton(file:, url:, alt:)
- full_path = image_dir + "/buttons/" + file
-
+ full_path = "#{image_dir}/buttons/#{file}"
+
image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: 88)
assets << image
- code = ERB.new <<-EOF
-<a href="<%=url%>"><img src="<%=image.relative_path%>" width="88" height="31" class="webbutton" alt="<%=alt%>"></a>
-EOF
+ code = ERB.new <<~EOF
+ <a href="<%=url%>"><img src="<%=image.relative_path%>" width="88" height="31" class="webbutton" alt="<%=alt%>"></a>
+ EOF
code.result(binding)
-
end
-
end
end
diff --git a/new/lib/crys/processors/html_processor.rb b/new/lib/crys/processors/html_processor.rb
index bcc9768b..33116cef 100644
--- a/new/lib/crys/processors/html_processor.rb
+++ b/new/lib/crys/processors/html_processor.rb
@@ -1,13 +1,14 @@
+# frozen_string_literal: true
+
require 'erb'
-require_relative "common_functions"
-require_relative "common_parts"
+require_relative 'common_functions'
+require_relative 'common_parts'
module Crys
class HtmlProcessor
-
include CommonFunctions
include CommonParts
-
+
def initialize(file_path:, image_dir:)
@file_path = file_path
@image_dir = image_dir
@@ -20,7 +21,7 @@ module Crys
end
def filename
- file_path.scan(/.*\/(.*)\.erb/).flatten.first
+ file_path.scan(%r{.*/(.*)\.erb}).flatten.first
end
attr_reader :assets, :file_path
@@ -31,7 +32,7 @@ module Crys
attr_writer :assets
def local_binding
- local_binding ||= binding.clone
+ @local_binding ||= binding.clone
end
end
end
diff --git a/new/lib/crys/processors/rss_processor.rb b/new/lib/crys/processors/rss_processor.rb
index bce48ad6..de79f11b 100644
--- a/new/lib/crys/processors/rss_processor.rb
+++ b/new/lib/crys/processors/rss_processor.rb
@@ -1,5 +1,7 @@
-require_relative "common_functions"
-require_relative "common_parts"
+# frozen_string_literal: true
+
+require_relative 'common_functions'
+require_relative 'common_parts'
module Crys
class RssProcessor
@@ -8,6 +10,7 @@ module Crys
rss_file = File.read(file_path)
instance_eval(rss_file)
@assets = []
+ @image_dir = image_dir # just for api consistency
end
def parsed_content
@@ -23,6 +26,5 @@ module Crys
private
attr_reader :rss
-
end
end
diff --git a/new/lib/crys/server.rb b/new/lib/crys/server.rb
index aa0c19c3..cc43b396 100644
--- a/new/lib/crys/server.rb
+++ b/new/lib/crys/server.rb
@@ -1,4 +1,6 @@
-require 'webrick'
+# frozen_string_literal: true
+
+require 'webrick'
module Crys
class Server
def initialize(root:)
@@ -6,16 +8,16 @@ module Crys
end
def start
- server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
- trap 'INT' do server.shutdown end
+ server = WEBrick::HTTPServer.new Port: 8000, DocumentRoot: root
+ trap 'INT' do
+ server.shutdown
+ end
server.start
end
- private
-
- attr_reader :root
+ private
+ attr_reader :root
end
end
-