lib/takuhai_status.rb in takuhai_status-1.3.2 vs lib/takuhai_status.rb in takuhai_status-1.4.0
- old
+ new
@@ -6,16 +6,44 @@
require "takuhai_status/ups"
module TakuhaiStatus
class NotFound < StandardError; end
class NotMyKey < StandardError; end
+ class Multiple < StandardError
+ attr_reader :services
+ def initialize(msg, services)
+ super(msg)
+ @services = services
+ end
+ end
def self.scan(key)
- [JapanPost, KuronekoYamato, Sagawa, TMGCargo, UPS].each do |service|
+ services = []
+ [].tap{|threads|
+ [Sagawa, JapanPost, KuronekoYamato, TMGCargo, UPS].each do |service|
+ threads.push(Thread.new{service.new(key)})
+ end
+ }.each{|thread|
begin
- return service.new(key)
+ services.push(thread.value)
rescue NotMyKey
end
+ }
+
+ case services.size
+ when 0
+ raise NotFound
+ when 1
+ return services.first
+ else
+ services.delete_if{|service| service.finish?}
+ case services.size
+ when 0
+ raise NotFound
+ when 1
+ return services.first
+ else
+ raise Multiple.new('some services found', services)
+ end
end
- raise NotFound
end
end