#Written by Ben Aroia :: benaroia@gmail.com :: d0ve.particalburst.com 
#2008-03-04
#version 0.61
#Define: HTTP GET code for RANDOM.ORG that retrieves random numbers and outputs
#		 them in the order they appear. It is/will be possible to allow outputing
#		 in several different formats. (i.e. print them/use them one by one, 
#		 save them to a file in several formats, command line options, etc) This
#		 is a basic version that will allow you to use the Integer Generator and
#		 output in a single line at the terminal.
#
#Usage:
#		ruby random-get.rb [options]
# -n [num]			-	number of numbers to generate
# -mn [num]			-	lowest number possible
# -mx [num]			-	highest number to generate
# -c [num]			-	number of columns to format to (default is one, enter 0 to output without line breaks)
# -b [2/8/10/16]	-	output in base [2/8/10/16]


require 'net/http'
require 'uri'

def bitsLeft()
    bit_left = Net::HTTP.get_response(URI.parse("http://www.random.org/quota/?format=plain"))
    if(bit_left.code.to_i != 200) then
        return "Error recieving the code. Please try again later.\n"
    end
    return bit_left.body+" bits left.\n"
end

def bitCheck()
    bit_left = Net::HTTP.get_response(URI.parse("http://www.random.org/quota/?format=plain"))
    if(bit_left.code.to_i > 0) then return true end
    return false
end

versioninfo =
"RandomGet version 0.61 by Ben Aroia && Josh Kehn
http://d0ve.particalburst.com
benaroia@gmail.com
(c)2008 Programmers, Ltd.
DISTRIBUTED UNDER THE GNU GPL
"
usage = 
"Usage:
ruby random-get.rb [options]
 -n [num]		-	number of numbers to generate
 -mn [num]		-	lowest number possible
 -mx [num]		-	highest number to generate
 -c [num]		-	number of columns to format to (default is one, enter 0 to output without line breaks)
 -b [2/8/10/16]		-	output in base [2/8/10/16]
--------------------------------------------------------------------------
 -h		-	prints help
 -v		-	print version
 -left	-	prints the number of random bits left
"


if ARGV.empty? then
print "Error. No arguments\n"
print usage
Process.exit
end

0.upto(ARGV.length) { |i|
	if(ARGV[i] == "-v" || ARGV[i] == "-version") then
	    print versioninfo
	    Process.exit
	end
	if(ARGV[i] == "-h" || ARGV[i] == "-help") then
	    print usage
	    Process.exit
	end
	if(ARGV[i] == "-left") then
	    print bitsLeft()
	    Process.exit
	end
}

if(!bitCheck()) then
    print "Something appears to be wrong with your balance. Please use -left to check the number of available bits.\n"
    Process.exit
end

url_code = "http://www.random.org/integers/?"

#define variables
number = "num=100"
min = "min=1000000"
max = "max=9999999"
col = "col=1"
base = "base=10" #I just love hex for some reason
#tail (options that shouldn't change)
tail = "&format=plain&rnd=new"
#and sign
s_and = "&"

#breaks boolean
breaks = true

#modify variables
0.upto(ARGV.length) { |i|
	if(ARGV[i] == "-n") then number = "num="+ARGV[i+1].to_s end
	if(ARGV[i] == "-mn") then min = "min="+ARGV[i+1].to_s end
	if(ARGV[i] == "-mx") then max = "max="+ARGV[i+1].to_s end
	if(ARGV[i] == "-c") then col = "col="+ARGV[i+1].to_s end
	if(ARGV[i] == "-b") then base = "base="+ARGV[i+1].to_s end
	if(ARGV[i] == "-c") then
	    if(ARGV[i+1].to_s == "0") then
	        col = "col=1"
	        breaks = false
	    end
	end
}

#create GET command
url_code += number.to_s+s_and+min.to_s+s_and+max.to_s+s_and+col.to_s+s_and+base.to_s+tail

requ = Net::HTTP.get_response(URI.parse(url_code.to_s))

#if requ.code == "200" then print "Code: 200 is good\n" end
if requ.code != "200" 
then 
print "Code: "+requ.code+" is not correct"
Process.exit
end
if(breaks) then
    print requ.body
    Process.exit
end

body = requ.body.split

0.upto(body.length-1) { |i|
    print body[i]
}