Skip to content

Commit

Permalink
Create Logger class
Browse files Browse the repository at this point in the history
  • Loading branch information
dtan4 committed May 22, 2014
1 parent 796d57d commit f7db20b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/ramesh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end

argv = parser.parse(ARGV)

client = Ramesh::Client.new
client = Ramesh::Client.new(Ramesh::Logger.new(STDOUT))

case argv.length
when 0
Expand Down
1 change: 1 addition & 0 deletions lib/ramesh.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "ramesh/client"
require "ramesh/image"
require "ramesh/logger"
require "ramesh/version"
6 changes: 6 additions & 0 deletions lib/ramesh/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Ramesh
class Client
MESHES_INDEX_URL = "http://tokyo-ame.jwa.or.jp/scripts/mesh_index.js"

def initialize(logger)
@logger = logger
end

def download_image(save_dir, minute = 0)
unless valid_minutes?(minute)
raise ArgumentError, "minutes must be a number; 0, 5, 10, ... 120"
Expand All @@ -10,6 +14,8 @@ def download_image(save_dir, minute = 0)
image_name = name_from_minute(minute)
image = Image.new(image_name)
image.save(save_dir, image_name)

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

def download_sequential_images(save_dir, from, to)
Expand Down
11 changes: 11 additions & 0 deletions lib/ramesh/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Ramesh
class Logger
def initialize(output)
@output = output
end

def info(msg)
@output.puts msg
end
end
end
16 changes: 15 additions & 1 deletion spec/ramesh/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

module Ramesh
describe Client do
let(:logger) do
double("logger", info: true)
end

let(:client) do
Ramesh::Client.new
Ramesh::Client.new(logger)
end

let(:tmpdir) do
Expand Down Expand Up @@ -33,13 +37,23 @@ module Ramesh
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)
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)
end

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

context "when invalid minute is specified" do
Expand Down
20 changes: 20 additions & 0 deletions spec/ramesh/logger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "spec_helper"

module Ramesh
describe Logger do
let(:output) do
double("output", puts: true)
end

let(:logger) do
described_class.new(output)
end

describe "#info" do
it "should flush message" do
logger.info("message")
expect(output).to have_received(:puts).with("message")
end
end
end
end

0 comments on commit f7db20b

Please sign in to comment.