module TakuhaiStatus

Constants

VERSION

Public Class Methods

add_service(service_class) click to toggle source

service_class as class

# File lib/takuhai_status.rb, line 18
def self.add_service(service_class)
        @@services << service_class
end
ignore_service(service_name) click to toggle source

service_name as symbol, cf. :USPS

# File lib/takuhai_status.rb, line 26
def self.ignore_service(service_name)
        begin
                @@services.delete(TakuhaiStatus.const_get(service_name))
        rescue NameError
                return nil
        end
end
scan(key, timeout: 10, logger: Logger.new(nil)) click to toggle source
# File lib/takuhai_status.rb, line 34
def self.scan(key, timeout: 10, logger: Logger.new(nil))
        services = []
        [].tap{|threads|
                @@services.each do |service|
                        threads.push(Thread.new{
                                if Thread.method_defined?(:report_on_exception)
                                        Thread.current.report_on_exception = false
                                end
                                name = service.to_s.sub(/^.*::/, '')
                                begin
                                        Timeout.timeout(timeout, Timeout::Error) do
                                                service.new(key)
                                        end
                                rescue Timeout::Error, Faraday::TimeoutError
                                        m = "Timeout in #{name}(#{key})"
                                        logger.error m
                                        raise NotMyKey.new(m)
                                end
                        })
                end
        }.each{|thread|
                begin
                        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
end