class RealRand::RandomOrg

Public Class Methods

new() click to toggle source
Calls superclass method RealRand::OnlineGenerator::new
# File lib/random/online.rb, line 55
def initialize
  super("www.random.org",true)
end

Public Instance Methods

integers(num = 100, min = 1, max = 100, args = {})
Alias for: randnum
quota(ip=nil) click to toggle source
# File lib/random/online.rb, line 120
def quota(ip=nil)
  parameters = "format=plain"
  parameters += "&ip=#{ip}" if ip
  response = get_response("/quota/", parameters)
  convert_result(response.body).first
end
randbyte(nbytes = 256) click to toggle source

Note: randbyte is deprecated, should use ‘/integers/’ instead. Network load decrease if using hex format instead here?

# File lib/random/online.rb, line 75
def randbyte(nbytes = 256)
  if nbytes <= 0 || nbytes > 16_384
    raise RangeError, "Invalid amount: #{nbytes}."
  end
  return [] if nbytes == 0
  parameters = "nbytes=#{nbytes}&format=d"
  response = get_response("/cgi-bin/randbyte", parameters)
  convert_result(response.body)
end
randnum(num = 100, min = 1, max = 100, args = {}) click to toggle source
# File lib/random/online.rb, line 59
def randnum(num = 100, min = 1, max = 100, args = {})
  check_amount_requested(num)
  check_min_max_range(min, max)

  parameters = "num=#{num}&min=#{min}&max=#{max}&col=#{num}"
  parameters << "&format=plain&base=10"
  parameters << "&rnd=#{args[:rnd]}" if args[:rnd]
  
  response = get_response("/integers/", parameters)
  convert_result(response.body)
end
Also aliased as: integers
randseq(min = 1, max = 100, args = {}) click to toggle source
# File lib/random/online.rb, line 85
def randseq(min = 1, max = 100, args = {})
  check_min_max_range(min, max)
  
  parameters = "min=#{min}&max=#{max}&col=1"
  parameters << "&format=plain" # TODO: No need for "&base=10" here?
  parameters << "&rnd=#{args[:rnd]}" if args[:rnd]
  
  response = get_response("/sequences/", parameters)
  convert_result(response.body)
end
Also aliased as: sequences
randstring(num, len, args = {}) click to toggle source
# File lib/random/online.rb, line 98
def randstring(num, len, args = {})
  default_args = { 
      :digits => :on, 
      :upperalpha =>:on, 
      :loweralpha =>:on,
      :unique => :off,
      # :rnd => :new
    }
  args = default_args.update(args)

  check_amount_requested(num)
  check_num_within(len, 1, 20, 'length')

  parameters = "num=#{num}&len=#{len}&format=plain"
  parameters << "&" << hash_args_to_params(args)
  
  response = get_response("/strings/", parameters)
  response.body.split
end
Also aliased as: strings
sequences(min = 1, max = 100, args = {})
Alias for: randseq
strings(num, len, args = {})
Alias for: randstring

Protected Instance Methods

check_response(response) click to toggle source
# File lib/random/online.rb, line 129
def  check_response(response)
  # RandomOrg returns 200 OK even for failures...
  error = contains_error(response.body)
  handle_response_error(response, error.to_s) if error
  return response
end
contains_error(body) click to toggle source
# File lib/random/online.rb, line 146
def contains_error(body)
  body.match(/error:.*/i)
end
handle_response_error(response, message = nil) click to toggle source
# File lib/random/online.rb, line 136
def  handle_response_error(response, message = nil)
  unless message then
    # Check the response body for an error message.
    error = contains_error(response.body)
    message = error.to_s if error
  end
  message ||= "An HTTP error occured."
  raise message
end

Private Instance Methods

check_amount_requested(num) click to toggle source
# File lib/random/online.rb, line 165
def check_amount_requested(num)
  check_num_within(num, 1, 10_000, 'amount')
end
check_min_max_range(min, max) click to toggle source
# File lib/random/online.rb, line 173
def check_min_max_range(min, max)
  if min < -1_000_000_000
    raise RangeError, "Invalid minimum: #{min}."
  end
  if max > 1_000_000_000
    raise RangeError, "Invalid maximum: #{max}."
  end
  if max <= min
    raise RangeError, "Maximum has to be bigger than minimum."
  end
end
check_num_within(num, min, max, desc = "number") click to toggle source
# File lib/random/online.rb, line 169
def check_num_within(num, min, max, desc = "number")
  raise RangeError, "Invalid #{desc}: #{num}." if num < min || num > max
end
convert_result(response) click to toggle source
# File lib/random/online.rb, line 152
def convert_result(response)
  result = []
  response.each_line { |line|
    result += line.chomp.split.map { |x| x.to_i }
  }
  result
end
hash_args_to_params(args) click to toggle source
# File lib/random/online.rb, line 161
def hash_args_to_params(args)
  args.collect{|k,v| "#{k}=#{v}"}.join('&')
end