Sha256: d7b0f5d45b5368b460c694d170bf87d6caafdee4e0ea9a2f46c7c05280ea8ca5

Contents?: true

Size: 1.29 KB

Versions: 13

Compression:

Stored size: 1.29 KB

Contents

require 'timeout'

module SecondsHandle #:nodoc: all
  def transform(secs)
    case secs
    when 0
      to_s
    when 1..59
      "#{secs} seconds"
    when 60..3559
      "#{secs/60} minutes and #{secs%60} seconds"
    else
      hours = secs/3600
      secs -= (hours*3600)
      "#{hours} hours, #{secs/60} minutes and #{secs%60} seconds"
    end
  end
end

class DnsTimeout # :nodoc: all

  include SecondsHandle
  
  def initialize(seconds)
    if seconds.is_a? Numeric and seconds >= 0
      @timeout = seconds
    else
      raise DnsTimeoutArgumentError, "Invalid value for tcp timeout"
    end    
  end
  
  def to_s
    if @timeout == 0 
      @output
    else
      @timeout.to_s
    end
  end
  
  def pretty_to_s
    transform(@timeout)
  end
  
  def timeout
    unless block_given?
      raise DnsTimeoutArgumentError, "Block required but missing"
    end
    if @timeout == 0
      yield
    else
      return Timeout.timeout(@timeout) do
        yield
      end
    end
  end
end

class TcpTimeout < DnsTimeout # :nodoc: all
  def initialize(seconds)
    @output = "infinite"
    super(seconds)
  end
end

class UdpTimeout < DnsTimeout # :nodoc: all
  def initialize(seconds)
    @output = "not defined"
    super(seconds)
  end
end

class DnsTimeoutArgumentError < ArgumentError # :nodoc: all
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
bluemonk-net-dns-0.5.0 lib/net/dns/resolver/timeouts.rb
bluemonk-net-dns-0.5.1 lib/net/dns/resolver/timeouts.rb
bluemonk-net-dns-0.5.2 lib/net/dns/resolver/timeouts.rb
bluemonk-net-dns-0.5.3 lib/net/dns/resolver/timeouts.rb
net-dns-0.5.3 lib/net/dns/resolver/timeouts.rb
net-dns-0.1 lib/net/dns/resolver/timeouts.rb
net-dns-0.2.5 lib/net/dns/resolver/timeouts.rb
net-dns-0.5.1 lib/net/dns/resolver/timeouts.rb
net-dns-0.3 lib/net/dns/resolver/timeouts.rb
net-dns-0.5.2 lib/net/dns/resolver/timeouts.rb
net-dns-0.2 lib/net/dns/resolver/timeouts.rb
net-dns-0.5.0 lib/net/dns/resolver/timeouts.rb
net-dns-0.4 lib/net/dns/resolver/timeouts.rb