Module: TakuhaiStatus

Defined in:
lib/takuhai_status.rb,
lib/takuhai_status/ups.rb,
lib/takuhai_status/usps.rb,
lib/takuhai_status/fedex.rb,
lib/takuhai_status/sagawa.rb,
lib/takuhai_status/version.rb,
lib/takuhai_status/japanpost.rb,
lib/takuhai_status/tmg_cargo.rb,
lib/takuhai_status/kuronekoyamato.rb

Defined Under Namespace

Classes: FedEx, JapanPost, KuronekoYamato, Multiple, NotFound, NotMyKey, Sagawa, TMGCargo, UPS, USPS

Constant Summary collapse

VERSION =
"1.8.7"
@@services =

loading plugins

[]

Class Method Summary collapse

Class Method Details

.add_service(service_name) ⇒ Object



17
18
19
# File 'lib/takuhai_status.rb', line 17

def self.add_service(service_name)
  @@services << service_name
end

.scan(key, timeout: 10, logger: Logger.new(nil)) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/takuhai_status.rb', line 24

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