Skip to content

Commit

Permalink
Refactor Client class
Browse files Browse the repository at this point in the history
  • Loading branch information
dtan4 committed May 29, 2014
1 parent 28fcca5 commit 2e6b3c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
58 changes: 43 additions & 15 deletions lib/photomosaic/client.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
module Photomosaic
class Client
def self.execute(argv)
options = Photomosaic::Options.parse(argv)
search_engine = options.search_engine.new(options.api_key, options.results)
image_url_list = search_engine.get_image_list(options.keyword)
base_image = Photomosaic::Image.preprocess_image(options.base_image, options.width, options.height, 4, options.colors)
image_downloader = Photomosaic::ImageDownloader.new
self.new(argv).execute
end

def initialize(argv)
@options = Photomosaic::Options.parse(argv)
end

def execute
@image_downloader = Photomosaic::ImageDownloader.new

begin
image_path_list = image_downloader.download_images(image_url_list)
image_list = get_image_list(image_path_list)
images = base_image.dispatch_images(image_list, 1, 2, options.color_model)
resize_to_pixel(images)
Photomosaic::Image.create_mosaic_image(images, options.output_path)
resize_to_pixel_size(pixel_images)
Photomosaic::Image.create_mosaic_image(pixel_images, @options.output_path)
ensure
image_downloader.remove_save_dir
@image_downloader.remove_save_dir
end
end

private

def self.get_image_list(image_path_list)
image_path_list.map do |path|
def base_image
@base_image ||= Photomosaic::Image.preprocess_image(
@options.base_image,
@options.width,
@options.height,
4,
@options.colors
)
end

def image_path_list
@image_path_list ||= @image_downloader.download_images(image_url_list)
end

def image_url_list
@image_url_list ||= search_engine.get_image_list(@options.keyword)
end

def image_list
@image_list ||= image_path_list.map do |path|
begin
Photomosaic::Image.new(path)
rescue
Expand All @@ -30,10 +49,19 @@ def self.get_image_list(image_path_list)
end.compact
end

def self.resize_to_pixel(images)
def pixel_images
@pixel_images ||=
base_image.dispatch_images(image_list, 1, 2, @options.color_model)
end

def resize_to_pixel_size(images)
images.map! do |row|
row.map {|image| image.resize!(40, 20, false); image }
row.map { |image| image.resize!(40, 20, false) }
end
end

def search_engine
@options.search_engine.new(@options.api_key, @options.results)
end
end
end
46 changes: 9 additions & 37 deletions spec/photomosaic/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,24 @@ module Photomosaic
}
end

let(:argv) { "argv" }

let(:image_name_list) do
(0..5).map { |i| "lena_#{i}.png" }
end

let(:image_path_list) do
image_name_list.map { |name| fixture_path(name) }
end

let(:image_url_list) do
image_name_list.map { |name| "http://example.com/#{name}" }
let(:client) do
described_class.new("argv")
end

let(:preprocessed_image) do
# TODO: Use mock
Photomosaic::Image.new(base_image)
end

let(:dispatched_images) do
5.times.inject([]) do |images, _|
# TODO: Use mock
images << image_path_list.map { |path| Photomosaic::Image.new(path) }
images
end
before do
allow(Photomosaic::Options).to receive(:parse).and_return(OpenStruct.new(options))
end

describe "#execute" do
before do
allow(Photomosaic::Options).to receive(:parse).and_return(OpenStruct.new(options))
allow_any_instance_of(Photomosaic::SearchEngine::Bing).to receive(:get_image_list)
.with(keyword).and_return(image_url_list)
allow(Photomosaic::Image).to receive(:preprocess_image)
.with(base_image, width, height, 4, colors).and_return(preprocessed_image)
allow_any_instance_of(Photomosaic::ImageDownloader).to receive(:download_images)
.and_return(image_path_list)
allow_any_instance_of(Photomosaic::Image).to receive(:dispatch_images)
.and_return(dispatched_images)

FileUtils.rm_rf(tmp_dir) if Dir.exist?(tmp_dir)
Dir.mkdir(tmp_dir)
allow_any_instance_of(described_class).to receive(:pixel_images)
allow_any_instance_of(described_class).to receive(:resize_to_pixel_size).and_return(true)
allow(Photomosaic::Image).to receive(:create_mosaic_image).and_return(true)
end

it "should execute the program" do
described_class.execute(argv)
expect(File.exist?(output_path)).to be_true
expect(Photomosaic::Image).to receive(:create_mosaic_image)
client.execute
end

after do
Expand Down

0 comments on commit 2e6b3c4

Please sign in to comment.