diff options
Diffstat (limited to 'new/lib/crys/processors')
-rw-r--r-- | new/lib/crys/processors/common_functions.rb | 36 | ||||
-rw-r--r-- | new/lib/crys/processors/common_parts.rb | 18 | ||||
-rw-r--r-- | new/lib/crys/processors/html_processor.rb | 38 | ||||
-rw-r--r-- | new/lib/crys/processors/rss_processor.rb | 30 |
4 files changed, 122 insertions, 0 deletions
diff --git a/new/lib/crys/processors/common_functions.rb b/new/lib/crys/processors/common_functions.rb new file mode 100644 index 0000000..9cf5669 --- /dev/null +++ b/new/lib/crys/processors/common_functions.rb @@ -0,0 +1,36 @@ +# 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__)))))).to_s + end + + def process_image(file:, width: :auto) + full_path = "#{image_dir}/#{file}" + + image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: width) + assets << image + + image + end + + def fetch_rss(url: nil, name: nil) + if name + path = case name + when :main + '/public/index.xml' + end + + rss = File.read(project_root + path) + else + puts url + + end + + RSS::Parser.parse(rss) + end + end +end diff --git a/new/lib/crys/processors/common_parts.rb b/new/lib/crys/processors/common_parts.rb new file mode 100644 index 0000000..a6c2dc1 --- /dev/null +++ b/new/lib/crys/processors/common_parts.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Crys + module CommonParts + def webbutton(file:, url:, alt:) + 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.result(binding) + end + end +end diff --git a/new/lib/crys/processors/html_processor.rb b/new/lib/crys/processors/html_processor.rb new file mode 100644 index 0000000..33116ce --- /dev/null +++ b/new/lib/crys/processors/html_processor.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'erb' +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 + @assets = [] + end + + def parsed_content + html_file = ERB.new(File.read(file_path)) + html_file.result(local_binding) + end + + def filename + file_path.scan(%r{.*/(.*)\.erb}).flatten.first + end + + attr_reader :assets, :file_path + + private + + attr_reader :image_dir + attr_writer :assets + + def local_binding + @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 new file mode 100644 index 0000000..de79f11 --- /dev/null +++ b/new/lib/crys/processors/rss_processor.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require_relative 'common_functions' +require_relative 'common_parts' + +module Crys + class RssProcessor + def initialize(file_path:, image_dir:) + @file_path = file_path + rss_file = File.read(file_path) + instance_eval(rss_file) + @assets = [] + @image_dir = image_dir # just for api consistency + end + + def parsed_content + rss.content + end + + def filename + rss.filename + end + + attr_reader :assets, :file_path + + private + + attr_reader :rss + end +end |