Skip to content

Commit

Permalink
Merge pull request #2 from dtan4/specify-output-filename
Browse files Browse the repository at this point in the history
Specify output filename
  • Loading branch information
dtan4 committed Jul 4, 2014
2 parents 0a7618e + a612d88 commit f6c90fd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 28 deletions.
8 changes: 5 additions & 3 deletions bin/ramesh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Usage:
EOS

save_dir = Dir.pwd
filename = nil

parser = OptionParser.new(USAGE) do |opt|
opt.on("-d", "--dir=VAL", "Save directory (default: current directory)") { |val| save_dir = val }
opt.on("-f", "--filename=VAL", "Save file name (default: timestamp index of downloaded image)") { |val| filename = val }
end

argv = parser.parse(ARGV)
Expand All @@ -23,11 +25,11 @@ client = Ramesh::Client.new(Ramesh::Logger.new(STDOUT))

case argv.length
when 0
client.download_image(save_dir)
client.download_image(0, save_dir, filename)
when 1
client.download_image(save_dir, argv[0].to_i)
client.download_image(argv[0].to_i, save_dir, filename)
when 2
client.download_sequential_images(save_dir, argv[0].to_i, argv[1].to_i)
client.download_sequential_images(argv[0].to_i, argv[1].to_i, save_dir)
else
$stderr.puts USAGE
exit 1
Expand Down
9 changes: 5 additions & 4 deletions lib/ramesh/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ def initialize(logger)
@logger = logger
end

def download_image(save_dir, minute = 0)
def download_image(minute, save_dir, filename = nil)
unless valid_minutes?(minute)
raise ArgumentError, "minutes must be a number; 0, 5, 10, ... 120"
end

image_name = name_from_minute(minute)
filename ||= "#{image_name}.jpg"
image = Image.new(image_name, background_image, mask_image)
image.save(save_dir, image_name)
image.save(save_dir, filename)

@logger.info("Downloaded: #{image_name}.jpg")
@logger.info("Downloaded: #{filename}")
end

def download_sequential_images(save_dir, from, to)
def download_sequential_images(from, to, save_dir)
unless valid_minutes?(from) && valid_minutes?(to)
raise ArgumentError, "minutes must be a number; 0, 5, 10, ... 120"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ramesh/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def initialize(image_name,
@image = composite_images(image_list)
end

def save(save_dir, image_name)
save_path = File.join(save_dir, "#{image_name}.jpg")
def save(save_dir, filename)
save_path = File.join(save_dir, filename)
@image.write(save_path)
end

Expand Down
73 changes: 56 additions & 17 deletions spec/ramesh/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,96 @@ module Ramesh
end

describe "#download_image" do
let(:download_image) do
client.download_image(minute, tmpdir, filename)
end

let(:minute) do
0
end

let(:filename) do
nil
end

before do
image = double(write: true)
allow(Image).to receive(:download_image).and_return(image)
allow_any_instance_of(Image).to receive(:composite_images).and_return(image)
end

context "when minute is not specified" do
it "should download the current image" do
expect_any_instance_of(Image).to receive(:save).with(tmpdir, "201405091845").once
client.download_image(tmpdir)
end

it "should log the result" do
expect(logger).to receive(:info).with("Downloaded: 201405091845.jpg")
client.download_image(tmpdir)
context "when valid minute is specified" do
let(:minute) do
30
end
end

context "when valid minute is specified" do
it "should download the image of the specified minutes ago" do
expect_any_instance_of(Image).to receive(:save).with(tmpdir, "201405091815").once
client.download_image(tmpdir, 30)
expect_any_instance_of(Image).to receive(:save).with(tmpdir, "201405091815.jpg").once
download_image
end

it "should log the result" do
expect(logger).to receive(:info).with("Downloaded: 201405091815.jpg")
client.download_image(tmpdir, 30)
download_image
end
end

context "when invalid minute is specified" do
let(:minute) do
7
end

it "should raise ArgumentError" do
expect do
client.download_image(tmpdir, 7)
download_image
end.to raise_error ArgumentError
end
end

context "when filename is specified" do
let(:filename) do
"out.jpg"
end

it "should download the image with specified name" do
expect_any_instance_of(Image).to receive(:save).with(tmpdir, "out.jpg").once
download_image
end
end
end

describe "#download_sequential_images" do
let(:download_sequential_images) do
client.download_sequential_images(from, to, tmpdir)
end

context "when valid section is specified" do
let(:from) do
0
end

let(:to) do
30
end

it "should download the images" do
expect_any_instance_of(Client).to receive(:download_image).exactly(7).times
client.download_sequential_images(tmpdir, 0, 30)
download_sequential_images
end
end

context "when invalid section is specified" do
let(:from) do
1
end

let(:to) do
2
end

it "should raise ArgumentError" do
expect do
client.download_sequential_images(tmpdir, 1, 2)
download_sequential_images
end.to raise_error ArgumentError
end
end
Expand Down
8 changes: 6 additions & 2 deletions spec/ramesh/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module Ramesh
"201405091845"
end

let(:filename) do
"201405091845.jpg"
end

let(:tmpdir) do
File.expand_path(File.join("..", "..", "tmp"), __FILE__)
end
Expand Down Expand Up @@ -81,8 +85,8 @@ module Ramesh

it "should save itself to the file" do
image = described_class.new(image_name)
image.save(tmpdir, image_name)
expect(File.exist?(File.join(tmpdir, "#{image_name}.jpg"))).to be_truthy
image.save(tmpdir, filename)
expect(File.exist?(File.join(tmpdir, filename))).to be_truthy
end

after do
Expand Down

0 comments on commit f6c90fd

Please sign in to comment.