class OmgImage::Processor
Attributes
options[R]
template[R]
Public Class Methods
new(template, options)
click to toggle source
# File lib/omg_image/processor.rb, line 5 def initialize(template, options) template.downcase! options ||= {} options[:size] ||= '800,400' options[:key] ||= options.to_s template = "#{template}.html.erb" unless template.ends_with?(".html.erb") @template = template @options = options end
Public Instance Methods
cached_or_new(regenerate: false)
click to toggle source
# File lib/omg_image/processor.rb, line 15 def cached_or_new(regenerate: false) reset_cache if regenerate cached&.file || generate&.file end
generate(cache: true)
click to toggle source
# File lib/omg_image/processor.rb, line 20 def generate(cache: true) output = create_screenshot if cache image = save_to_cache(output) output&.close image else output end end
with_screenshot(&block)
click to toggle source
# File lib/omg_image/processor.rb, line 31 def with_screenshot(&block) create_screenshot(&block) end
Private Instance Methods
cached()
click to toggle source
# File lib/omg_image/processor.rb, line 79 def cached image = OmgImage::Image.find_by(key: options[:key]) if image && !image.file.attached? image.destroy image = nil end image end
create_screenshot() { |output| ... }
click to toggle source
# File lib/omg_image/processor.rb, line 37 def create_screenshot begin start = Time.now body = OmgImage::Renderer.render(template, locals: options) log_smt " to_html: #{(Time.now - start).round(2)}" input = BetterTempfile.new("input.html") input.write(body) input.flush output = BetterTempfile.new("image.png") command = Shell.command({ file: output, size: options[:size], path: input.path }) log_smt " => #{command}" start = Time.now begin process = open4.spawn(command, timeout: 10) rescue Timeout::Error Process.kill('KILL', process.pid) rescue nil log_smt "omg error: please retry. options: #{options}" return nil end log_smt " to_image: #{(Time.now - start).round(2)}" yield(output) if block_given? output ensure #output&.close! input&.close! end end
log_smt(str)
click to toggle source
# File lib/omg_image/processor.rb, line 71 def log_smt(str) Rails.logger.debug(str) end
reset_cache()
click to toggle source
# File lib/omg_image/processor.rb, line 75 def reset_cache OmgImage::Image.where(key: options[:key]).destroy_all end
save_to_cache(output)
click to toggle source
# File lib/omg_image/processor.rb, line 88 def save_to_cache(output) image = OmgImage::Image.find_by(key: options[:key]) image ||= OmgImage::Image.new(key: options[:key]) if !image.file.attached? image.file.attach(io: File.open(output.path), filename: "image.png", content_type: "image/png") image.save! end image end