lib/takuhai_status.rb in takuhai_status-1.6.0 vs lib/takuhai_status.rb in takuhai_status-1.7.0

- old
+ new

@@ -4,10 +4,13 @@ require "takuhai_status/sagawa" require "takuhai_status/tmg_cargo" require "takuhai_status/ups" require "takuhai_status/fedex" +require "logger" +require "timeout" + module TakuhaiStatus class NotFound < StandardError; end class NotMyKey < StandardError; end class Multiple < StandardError attr_reader :services @@ -15,14 +18,24 @@ super(msg) @services = services end end - def self.scan(key) + def self.scan(key, timeout: 10, logger: Logger.new(nil)) services = [] [].tap{|threads| [Sagawa, JapanPost, KuronekoYamato, TMGCargo, UPS, FedEx].each do |service| - threads.push(Thread.new{service.new(key)}) + threads.push(Thread.new{ + name = service.to_s.sub(/^.*::/, '') + begin + Timeout.timeout(timeout, Timeout::Error, "Timeout in #{name}(#{key})") do + service.new(key) + end + rescue Timeout::Error, Faraday::TimeoutError => e + logger.error e.message + raise NotMyKey.new(e.message) + end + }) end }.each{|thread| begin services.push(thread.value) rescue NotMyKey