Last active
December 18, 2015 02:48
-
-
Save nareshov/5713351 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
* Jun 5th, 2013 - Naresh V. <nvenkateshappa@kiwiup.com> | |
- Modified version to work with ruby 1.8.7 (stock CentOS 6) | |
- Added a random password method. | |
::password(num, len) returns an array of 'num' passwords each of length 'len' | |
Original code from: https://dl.dropboxusercontent.com/u/84400100/randomorg.rb | |
Original doc below: | |
Random.org HTTP Ruby Library by Devin Akman | |
Distributed under the same license as Ruby. | |
*Uses the new hash syntax that probably won't work for versions < 1.9 | |
This libray provides the following methods: | |
TrueRandom::quota returns your remaining quota. | |
The quota is also automatically checked during each | |
data request and a QuotaNotPositiveError is thrown | |
if it is not positive (Duh!) | |
::integers(num, min, max) returns an array of integers. | |
::sequence(min, max) also returns an array of integers. | |
::strings(num, len, digits?, upperalpha?, loweralpha?, unique?) returns | |
an array of strings. | |
No exception handling is perfomed; that is left up to the implementation. | |
Below is a summary of the most likely exceptions: | |
ArgumentError is thrown when a bad argument is passed. | |
SocketError is thrown when there is a probelm with the connection. | |
QuotaNotPositiveError is thrown if the quota is not positive (Duh!) | |
The USER_AGENT constant should be changed to include the email of whoever is | |
using this library in their program so that Dr. Haahr can drop them a line | |
if it misbehaves. | |
=end | |
require 'socket' | |
class QuotaNotPositiveError < StandardError | |
end | |
class TrueRandom | |
HOST = 'www.random.org' | |
PORT = 80 | |
CRLF = "\r\n" | |
TIMEOUT = 5 * 60 | |
USER_AGENT = 'foo@bar.baz' | |
class << self | |
private | |
def get(path, options_hash) | |
request = [] | |
options_hash.each { |key, value| request << "#{key}=#{value}" } | |
request = "/#{path}/?" + request.join('&') | |
s = TCPSocket.new(HOST, PORT) | |
s << (["GET #{request} HTTP/1.1", "Host: #{HOST}", | |
"User-Agent: #{USER_AGENT}"].join(CRLF) + CRLF * 2) | |
fail(SocketError, 'Timed out!') unless IO.select([s], nil, nil, TIMEOUT) | |
response = s.read | |
unless response.split(CRLF)[0]['200 OK'] | |
fail(SocketError, "Bad Server Response: #{response}") | |
end | |
response.split(CRLF * 2, 2)[1].chomp | |
end | |
def check_quota | |
fail QuotaNotPositiveError unless quota > 0 | |
end | |
def pre_check(*arg_bounds) | |
check_quota | |
arg_bounds.all? { |e| e[0].between?(e[1], e[2]) } | |
end | |
public | |
def quota | |
get('quota', { 'format' => 'plain' }).to_i | |
end | |
def integers(num, min, max) | |
fail ArumentError unless pre_check([num, 1, 10**4], [min, -10**9, 10**9], | |
[max, -10**9, 10**9]) && max > min | |
raw = get('integers', { 'num' => num, 'min' => min, 'max' => max, 'col' => 1, 'base' => 10, | |
'format' => 'plain', 'rnd' => 'new' }) | |
raw.split("\n").map! { |x| x.to_i } | |
end | |
def sequence(min, max) | |
fail ArgumentError unless pre_check([min, -10**9, 10**9], [max, -10**9, | |
10**9]) | |
get('sequences', { 'min' => min, 'max' => max, 'col' => 1, 'format' => 'plain', | |
'rnd' => 'new' }).split("\n").map! { |x| x.to_i } | |
end | |
def password(num, len) | |
fail ArgumentError unless pre_check([num, 1, 100], [len, 6, 24]) | |
get('passwords', { 'num' => num, 'len' => len, 'format' => 'plain', | |
'rnd' => 'new' }).split("\n") | |
end | |
def strings(num, len, digits, ualpha, lalpha, unique) | |
fail ArgumentError unless digits || ualpha || lalpha | |
if unique | |
acc = (digits ? 10 : 0) | |
ualpha ? acc += 26 : nil | |
lalpha ? acc += 26 : nil | |
fail(ArgumentError, "num greater than string space!") if num > acc**len | |
end | |
fail ArgumentError unless pre_check([num, 1, 10**4], [len, 1, 20]) | |
bools = [digits, ualpha, lalpha, unique].map! { |x| x ? 'on' : 'off' } | |
d, up, lo, u = *bools | |
get('strings', { 'num' => num, 'len' => len, 'digits' => d, 'upperalpha' => up, | |
'loweralpha' => lo, 'unique' => u, 'format' => 'plain', | |
'rnd' => 'new' }).split("\n") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment