realrand 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/random/online.rb +115 -18
  2. metadata +21 -7
data/lib/random/online.rb CHANGED
@@ -31,12 +31,22 @@ module Random
31
31
  ).start(@host) { |h|
32
32
  response = h.get("#{script}?#{parameters}")
33
33
  if response.class == Net::HTTPOK
34
- return response
34
+ return check_response(response)
35
35
  else
36
- raise "An HTTP error occured."
36
+ handle_response_error(response)
37
37
  end
38
38
  }
39
39
  end
40
+
41
+ protected
42
+
43
+ def check_response(response)
44
+ return response
45
+ end
46
+
47
+ def handle_response_error(response)
48
+ raise "An HTTP error occured."
49
+ end
40
50
  end
41
51
 
42
52
  class RandomOrg < OnlineGenerator
@@ -44,28 +54,24 @@ module Random
44
54
  super("www.random.org")
45
55
  end
46
56
 
47
- def randnum(num = 100, min = 1, max = 100)
48
- if num < 0 || num > 10_000
49
- raise RangeError, "Invalid amount: #{num}."
50
- end
51
- return [] if num == 0
52
- if min < -1_000_000_000
53
- raise RangeError, "Invalid minimum: #{min}."
54
- end
55
- if max > 1_000_000_000
56
- raise RangeError, "Invalid maximum: #{max}."
57
- end
58
- if max <= min
59
- raise RangeError, "Maximum has to be bigger than minimum."
60
- end
57
+ def randnum(num = 100, min = 1, max = 100, args = {})
58
+ check_amount_requested(num)
59
+ check_min_max_range(min, max)
61
60
 
62
61
  parameters = "num=#{num}&min=#{min}&max=#{max}&col=#{num}"
63
- response = get_response("/cgi-bin/randnum", parameters)
62
+ parameters << "&format=plain&base=10"
63
+ parameters << "&rnd=#{args[:rnd]}" if args[:rnd]
64
+
65
+ response = get_response("/integers/", parameters)
64
66
  convert_result(response.body)
65
67
  end
68
+ alias :integers :randnum
66
69
 
70
+ ##
71
+ # Note: randbyte is deprecated, should use '/integers/' instead.
72
+ # Network load decrease if using hex format instead here?
67
73
  def randbyte(nbytes = 256)
68
- if nbytes < 0 || nbytes > 16_384
74
+ if nbytes <= 0 || nbytes > 16_384
69
75
  raise RangeError, "Invalid amount: #{nbytes}."
70
76
  end
71
77
  return [] if nbytes == 0
@@ -74,6 +80,71 @@ module Random
74
80
  convert_result(response.body)
75
81
  end
76
82
 
83
+ def randseq(min = 1, max = 100, args = {})
84
+ check_min_max_range(min, max)
85
+
86
+ parameters = "min=#{min}&max=#{max}&col=1"
87
+ parameters << "&format=plain" # TODO: No need for "&base=10" here?
88
+ parameters << "&rnd=#{args[:rnd]}" if args[:rnd]
89
+
90
+ response = get_response("/sequences/", parameters)
91
+ convert_result(response.body)
92
+ end
93
+ alias :sequences :randseq
94
+
95
+
96
+ def randstring(num, len, args = {})
97
+ default_args = {
98
+ :digits => :on,
99
+ :upperalpha =>:on,
100
+ :loweralpha =>:on,
101
+ :unique => :off,
102
+ # :rnd => :new
103
+ }
104
+ args = default_args.update(args)
105
+
106
+ check_amount_requested(num)
107
+ check_num_within(len, 1, 20, 'length')
108
+
109
+ parameters = "num=#{num}&len=#{len}&format=plain"
110
+ parameters << "&" << hash_args_to_params(args)
111
+
112
+ response = get_response("/strings/", parameters)
113
+ response.body.split
114
+ end
115
+ alias :strings :randstring
116
+
117
+
118
+ def quota(ip=nil)
119
+ parameters = "format=plain"
120
+ parameters += "&ip=#{ip}" if ip
121
+ response = get_response("/quota/", parameters)
122
+ convert_result(response.body).first
123
+ end
124
+
125
+ protected
126
+
127
+ def check_response(response)
128
+ # RandomOrg returns 200 OK even for failures...
129
+ error = contains_error(response.body)
130
+ handle_response_error(response, error.to_s) if error
131
+ return response
132
+ end
133
+
134
+ def handle_response_error(response, message = nil)
135
+ unless message then
136
+ # Check the response body for an error message.
137
+ error = contains_error(response.body)
138
+ message = error.to_s if error
139
+ end
140
+ message ||= "An HTTP error occured."
141
+ raise message
142
+ end
143
+
144
+ def contains_error(body)
145
+ body.match(/error:.*/i)
146
+ end
147
+
77
148
  private
78
149
 
79
150
  def convert_result(response)
@@ -83,6 +154,32 @@ module Random
83
154
  }
84
155
  result
85
156
  end
157
+
158
+
159
+ def hash_args_to_params(args)
160
+ args.collect{|k,v| "#{k}=#{v}"}.join('&')
161
+ end
162
+
163
+ def check_amount_requested(num)
164
+ check_num_within(num, 1, 10_000, 'amount')
165
+ end
166
+
167
+ def check_num_within(num, min, max, desc = "number")
168
+ raise RangeError, "Invalid #{desc}: #{num}." if num < min || num > max
169
+ end
170
+
171
+ def check_min_max_range(min, max)
172
+ if min < -1_000_000_000
173
+ raise RangeError, "Invalid minimum: #{min}."
174
+ end
175
+ if max > 1_000_000_000
176
+ raise RangeError, "Invalid maximum: #{max}."
177
+ end
178
+ if max <= min
179
+ raise RangeError, "Maximum has to be bigger than minimum."
180
+ end
181
+ end
182
+
86
183
  end
87
184
 
88
185
  class FourmiLab < OnlineGenerator
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: realrand
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 4
10
+ version: 1.0.4
5
11
  platform: ruby
6
12
  authors:
7
13
  - Maik Schmidt
@@ -9,7 +15,7 @@ autorequire: random/online
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-02-13 00:00:00 +01:00
18
+ date: 2011-05-01 00:00:00 +02:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
@@ -23,31 +29,39 @@ extra_rdoc_files: []
23
29
 
24
30
  files:
25
31
  - lib/random/online.rb
26
- has_rdoc: false
32
+ has_rdoc: true
27
33
  homepage: http://realrand.rubyforge.org
34
+ licenses: []
35
+
28
36
  post_install_message:
29
37
  rdoc_options: []
30
38
 
31
39
  require_paths:
32
40
  - lib
33
41
  required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
34
43
  requirements:
35
44
  - - ">="
36
45
  - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
37
49
  version: "0"
38
- version:
39
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
40
52
  requirements:
41
53
  - - ">="
42
54
  - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
43
58
  version: "0"
44
- version:
45
59
  requirements: []
46
60
 
47
61
  rubyforge_project: realrand
48
- rubygems_version: 1.3.1
62
+ rubygems_version: 1.3.7
49
63
  signing_key:
50
- specification_version: 2
64
+ specification_version: 3
51
65
  summary: A simple API for creating real random numbers.
52
66
  test_files: []
53
67