Refactor PNG generation
parent
fc873a8371
commit
b619302e3e
@ -0,0 +1,15 @@
|
||||
require 'open3'
|
||||
|
||||
class ImageInspector
|
||||
|
||||
def get_size(image_path)
|
||||
o, e, t = Open3.capture3(%(identify -format "%[fx:w]x%[fx:h]" #{image_path}))
|
||||
|
||||
if t.exitstatus != 0
|
||||
raise RuntimeError, "Couldn't inspect image #{image_path}:\n#{o}\n#{e}"
|
||||
end
|
||||
|
||||
o.split('x').map(&:to_i)
|
||||
end
|
||||
|
||||
end
|
@ -1,15 +0,0 @@
|
||||
require 'open3'
|
||||
|
||||
class PngGenerator
|
||||
|
||||
BINARY_PATH = (Rails.root + "bin" + "asciicast2png").to_s
|
||||
|
||||
def generate(page_path, png_path)
|
||||
o, e, t = Open3.capture3("#{BINARY_PATH} #{page_path} #{png_path}")
|
||||
|
||||
if t.exitstatus != 0
|
||||
raise RuntimeError, "Couldn't generate PNG for #{page_path}:\n#{o}\n#{e}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,15 @@
|
||||
require 'open3'
|
||||
|
||||
class Rasterizer
|
||||
|
||||
BINARY_PATH = (Rails.root + "bin" + "rasterize").to_s
|
||||
|
||||
def generate_image(page_path, image_path, format, selector, scale)
|
||||
o, e, t = Open3.capture3("#{BINARY_PATH} #{page_path} #{image_path} #{format} #{selector} #{scale}")
|
||||
|
||||
if t.exitstatus != 0
|
||||
raise RuntimeError, "Couldn't generate image from #{page_path}:\n#{o}\n#{e}"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Binary file not shown.
After Width: | Height: | Size: 747 B |
@ -0,0 +1,22 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe ImageInspector do
|
||||
|
||||
let(:image_inspector) { ImageInspector.new }
|
||||
|
||||
describe '#get_size' do
|
||||
it 'returns width and height of the image' do
|
||||
w, h = image_inspector.get_size("#{Rails.root}/spec/fixtures/new-logo-bars.png")
|
||||
|
||||
expect(w).to eq(154)
|
||||
expect(h).to eq(33)
|
||||
end
|
||||
|
||||
context 'when file is not an image' do
|
||||
it 'raises error' do
|
||||
expect { image_inspector.get_size("#{Rails.root}/spec/fixtures/snapshot.json") }.to raise_error(RuntimeError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue