From 2e6b3c4382666c29dfd02812b8871670cd2244ad Mon Sep 17 00:00:00 2001 From: dtan4 Date: Fri, 30 May 2014 01:19:29 +0900 Subject: [PATCH] Refactor Client class --- lib/photomosaic/client.rb | 58 ++++++++++++++++++++++++--------- spec/photomosaic/client_spec.rb | 46 +++++--------------------- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/lib/photomosaic/client.rb b/lib/photomosaic/client.rb index 47a37d4..4955239 100644 --- a/lib/photomosaic/client.rb +++ b/lib/photomosaic/client.rb @@ -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 @@ -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 diff --git a/spec/photomosaic/client_spec.rb b/spec/photomosaic/client_spec.rb index c5db01d..bef51c8 100644 --- a/spec/photomosaic/client_spec.rb +++ b/spec/photomosaic/client_spec.rb @@ -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