lib/ramesh/client.rb in ramesh-1.0.0 vs lib/ramesh/client.rb in ramesh-1.1.0

- old
+ new

@@ -1,45 +1,45 @@ module Ramesh class Client - include Util - include ImageUtil + MESHES_INDEX_URL = "http://tokyo-ame.jwa.or.jp/scripts/mesh_index.js" - def initialize - @indexes = get_mesh_indexes + def initialize(logger) + @logger = logger end - def download_sequential_image(minute_range) - range = minute_range.split('-').map { |num| num.to_i }.sort - - unless range.length == 2 - $stderr.puts "error: invalid range" - exit 1 + def download_image(save_dir, minute = 0) + unless valid_minutes?(minute) + raise ArgumentError, "minutes must be a number; 0, 5, 10, ... 120" end - range.each do |min| - unless validate_minutes(min) - $stderr.puts "error: minutes must be a number; 0, 5, 10, ... 120" - exit 1 - end - end + image_name = name_from_minute(minute) + image = Image.new(image_name) + image.save(save_dir, image_name) - minute = range[0] + @logger.info("Downloaded: #{image_name}.jpg") + end - while minute <= range[1] - download_moment_image(minute) - minute += 5 + def download_sequential_images(save_dir, from, to) + unless valid_minutes?(from) && valid_minutes?(to) + raise ArgumentError, "minutes must be a number; 0, 5, 10, ... 120" end - end - def download_moment_image(minutes = 0) - unless validate_minutes(minutes) - $stderr.puts "error: minutes must be a number; 0, 5, 10, ... 120" - exit 1 + (from..to).step(5) do |minute| + download_image(save_dir, minute) end + end - download_index = @indexes[minutes / 5] - create_moment_image("#{download_index}.gif") + private - puts "Successfully downloaded: #{download_index}.gif" + def meshes_index + @meshes_index ||= open(MESHES_INDEX_URL).read.gsub(/[^0-9,]/, "").split(",") + end + + def name_from_minute(minute) + meshes_index[minute / 5] + end + + def valid_minutes?(minutes) + (minutes >= 0) && (minutes <= 120) && (minutes % 5 == 0) end end end