random_org_http_api 0.0.3.1 → 0.1.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -3
- data/Rakefile +2 -2
- data/lib/random_org_http_api/configuration.rb +27 -25
- data/lib/random_org_http_api/error.rb +12 -0
- data/lib/random_org_http_api/generator.rb +49 -52
- data/lib/random_org_http_api/version.rb +1 -1
- data/lib/random_org_http_api.rb +5 -4
- data/random_org_http_api.gemspec +16 -17
- data/spec/random_org_http_api/generator_spec.rb +54 -40
- data/spec/spec_helper.rb +3 -3
- metadata +20 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a71dd4b3a01bcce935056f7297cdd5169a9fadfb
|
4
|
+
data.tar.gz: f0fa6046ae5de636309b22bc4cbdaadc3fc0b41c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b01e9036de8feb91009704e5ff888b158b767bebd20c019d0fb8a887c232229967f265d1b4942bbaef638bd1c12a337f189146e46d3697eda114d5d4e75c429b
|
7
|
+
data.tar.gz: 1b3fb96d3e30229705f7968be11564623884d6fb42ccc490062adf5e986a81ba91c77a39631a4336ca96f5efd53b2a9d29b59d5cab7826934b9bb1bebdd38238
|
data/Gemfile
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
# Specify your gem's dependencies in random_org_http_api.gemspec
|
4
3
|
gemspec
|
5
4
|
|
6
5
|
group :test do
|
7
|
-
gem 'simplecov', :
|
6
|
+
gem 'simplecov', require: false
|
8
7
|
gem 'coveralls', require: false
|
9
|
-
|
8
|
+
gem 'rubocop', require: false
|
9
|
+
end
|
data/Rakefile
CHANGED
@@ -1,30 +1,32 @@
|
|
1
1
|
module RandomOrgHttpApi
|
2
2
|
module Configuration
|
3
|
-
|
4
|
-
DEFAULT_QUERY_PARAMS = {
|
5
|
-
format: 'plain',
|
6
|
-
base: '10',
|
7
|
-
rnd: 'new',
|
8
|
-
col: '1',
|
9
|
-
min: '1',
|
10
|
-
digits: 'on',
|
11
|
-
loweralpha: 'on',
|
12
|
-
upperalpha: 'on',
|
13
|
-
unique: 'on'
|
14
|
-
}
|
15
|
-
|
16
|
-
# Query templates
|
17
|
-
# Integer
|
18
|
-
INTEGER_QUERY_KEYS = [:num, :min, :max, :col, :base, :format, :rnd]
|
19
|
-
INTEGER_QUERY_TEMPLATE = "integers/?num=%{num}&min=%{min}&max=%{max}&col=%{col}&base=%{base}&format=%{format}&rnd=%{rnd}"
|
3
|
+
BASE_URI = 'https://www.random.org'.freeze
|
20
4
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
5
|
+
DEFAULT_QUERY_PARAMS = {
|
6
|
+
format: 'plain',
|
7
|
+
base: '10',
|
8
|
+
rnd: 'new',
|
9
|
+
col: '1',
|
10
|
+
min: '1',
|
11
|
+
digits: 'on',
|
12
|
+
loweralpha: 'on',
|
13
|
+
upperalpha: 'on',
|
14
|
+
unique: 'on'
|
15
|
+
}.freeze
|
28
16
|
|
17
|
+
QUERY_TEMPLATE = {
|
18
|
+
integer: {
|
19
|
+
path: '/integers',
|
20
|
+
query_keys: [:num, :min, :max, :col, :base, :format, :rnd]
|
21
|
+
},
|
22
|
+
string: {
|
23
|
+
path: '/strings',
|
24
|
+
query_keys: [:num, :len, :digits, :upperalpha, :loweralpha, :unique, :format, :rnd]
|
25
|
+
},
|
26
|
+
sequence: {
|
27
|
+
path: '/sequences',
|
28
|
+
query_keys: [:min, :max, :col, :format, :rnd]
|
29
|
+
}
|
30
|
+
}.freeze
|
29
31
|
end
|
30
|
-
end
|
32
|
+
end
|
@@ -1,76 +1,73 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
1
3
|
module RandomOrgHttpApi
|
2
4
|
class Generator
|
3
|
-
require 'net/http'
|
4
5
|
include RandomOrgHttpApi::Configuration
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
include RandomOrgHttpApi::Error
|
7
|
+
|
8
|
+
def generate_integers(params)
|
9
|
+
generate(:integer, params)
|
9
10
|
end
|
10
11
|
|
11
|
-
def generate_strings(params
|
12
|
-
|
13
|
-
request(DOMAIN, strings_query)
|
12
|
+
def generate_strings(params)
|
13
|
+
generate(:string, params)
|
14
14
|
end
|
15
15
|
|
16
|
-
def generate_sequence(params
|
17
|
-
|
18
|
-
request(DOMAIN, sequence_query)
|
16
|
+
def generate_sequence(params)
|
17
|
+
generate(:sequence, params)
|
19
18
|
end
|
20
19
|
|
21
|
-
# Quota Checker
|
22
20
|
def quota
|
23
|
-
|
24
|
-
|
21
|
+
params = { path: '/quota', params: { ip: ip, format: 'plain' } }
|
22
|
+
api_request(params).first.to_i
|
23
|
+
end
|
24
|
+
|
25
|
+
def ip
|
26
|
+
request(uri: 'http://whatismyip.akamai.com').strip
|
25
27
|
end
|
26
28
|
|
27
29
|
private
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
uri = URI.join(domain, query)
|
33
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
34
|
-
http.use_ssl = true
|
35
|
-
res = http.get(uri.request_uri)
|
36
|
-
# Если все хорошо, то возвращаем форматированный ответ,
|
37
|
-
# если нет, то показываем ответ ошибки сервера.
|
38
|
-
res.is_a?(Net::HTTPSuccess) ? res.body.split : res.body.strip
|
31
|
+
def generate(type, params = {})
|
32
|
+
params = query(type, params)
|
33
|
+
api_request(params)
|
39
34
|
end
|
40
35
|
|
41
|
-
|
42
|
-
|
43
|
-
# Совмещаем параметры по умолчанию с параметами, который передал пользователь
|
44
|
-
all_params = DEFAULT_QUERY_PARAMS.merge(params)
|
45
|
-
# В зависимости от типа объекта, который нужно сгенерировать, оставляем только нужные для него параметры.
|
46
|
-
case object_type
|
47
|
-
when 'integer' then
|
48
|
-
right_params = permit_params(all_params, INTEGER_QUERY_KEYS)
|
49
|
-
insert_params(right_params, INTEGER_QUERY_TEMPLATE)
|
50
|
-
when 'string' then
|
51
|
-
right_params = permit_params(all_params, STRING_QUERY_KEYS)
|
52
|
-
insert_params(right_params, STRING_QUERY_TEMPLATE)
|
53
|
-
when 'sequence' then
|
54
|
-
right_params = permit_params(all_params, SEQUENCE_QUERY_KEYS)
|
55
|
-
insert_params(right_params, SEQUENCE_QUERY_TEMPLATE)
|
56
|
-
end
|
36
|
+
def api_request(hash)
|
37
|
+
request(uri: URI(BASE_URI), path: hash[:path], query: hash[:params]).split
|
57
38
|
end
|
58
39
|
|
59
|
-
# Метод оставляющий только нужные параметры.
|
60
|
-
def permit_params(params, required_keys)
|
61
|
-
params.select { |k| required_keys.include?(k) }
|
62
|
-
end
|
63
40
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
41
|
+
def request(params)
|
42
|
+
uri = URI params[:uri]
|
43
|
+
uri.path = params[:path] if params[:path]
|
44
|
+
uri.query = URI.encode_www_form(params[:query]) if params[:query]
|
45
|
+
|
46
|
+
exception_cb = Proc.new do |exception|
|
47
|
+
puts exception
|
48
|
+
end
|
49
|
+
|
50
|
+
Retryable.retryable(tries: 3, on: OpenURI::HTTPError, exception_cb: exception_cb) do |retries, exception|
|
51
|
+
if retries > 1
|
52
|
+
raise QuotaError if quota == 0
|
53
|
+
end
|
54
|
+
open(uri).read
|
55
|
+
end
|
68
56
|
end
|
69
57
|
|
70
|
-
|
71
|
-
|
72
|
-
|
58
|
+
|
59
|
+
def query(generator_type, params)
|
60
|
+
all_params = DEFAULT_QUERY_PARAMS.merge(params)
|
61
|
+
template = QUERY_TEMPLATE[generator_type]
|
62
|
+
|
63
|
+
{
|
64
|
+
path: template[:path],
|
65
|
+
params: permit_params(all_params, template[:query_keys])
|
66
|
+
}
|
73
67
|
end
|
74
68
|
|
69
|
+
def permit_params(params, required_keys)
|
70
|
+
params.select { |k| required_keys.include?(k) }
|
71
|
+
end
|
75
72
|
end
|
76
|
-
end
|
73
|
+
end
|
data/lib/random_org_http_api.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'random_org_http_api/version'
|
2
|
+
require 'random_org_http_api/error'
|
3
|
+
require 'random_org_http_api/configuration'
|
4
|
+
require 'random_org_http_api/generator'
|
4
5
|
|
5
|
-
module RandomOrgHttpApi;end
|
6
|
+
module RandomOrgHttpApi; end
|
data/random_org_http_api.gemspec
CHANGED
@@ -4,21 +4,20 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'random_org_http_api/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
11
|
-
spec.summary
|
12
|
-
spec.description
|
13
|
-
spec.homepage
|
14
|
-
spec.license
|
15
|
-
|
16
|
-
spec.
|
17
|
-
spec.
|
18
|
-
spec.
|
19
|
-
spec.
|
20
|
-
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency "rspec"
|
7
|
+
spec.name = 'random_org_http_api'
|
8
|
+
spec.version = RandomOrgHttpApi::VERSION
|
9
|
+
spec.authors = ['Temur Fatkulin']
|
10
|
+
spec.email = ['temur.fatkulin@gmail.com']
|
11
|
+
spec.summary = 'Realization of old HTTP API provided by the random.org service'
|
12
|
+
spec.description = 'At present realized generation for numbers, strings and sequences'
|
13
|
+
spec.homepage = 'https://github.com/istickz/random_org_http_api'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
spec.add_dependency 'retryable'
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_development_dependency 'rspec'
|
24
23
|
end
|
@@ -1,63 +1,77 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
describe RandomOrgHttpApi::Generator do
|
3
3
|
|
4
4
|
before(:all) do
|
5
5
|
@generator = RandomOrgHttpApi::Generator.new
|
6
6
|
end
|
7
7
|
|
8
|
-
describe '#insert_params' do
|
9
|
-
it 'should do insert params in string' do
|
10
|
-
params = {name: 'World'}
|
11
|
-
string = 'Hello %{name}'
|
12
|
-
expect(@generator.send(:insert_params, params, string)).to eq('Hello World')
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
8
|
describe '#permit_params' do
|
17
9
|
it 'should do permit only required params' do
|
18
|
-
|
10
|
+
input_params = { a: 'A', b: 'B', c: 'C' }
|
19
11
|
required_keys = [:a, :b]
|
20
|
-
|
12
|
+
output_params = { a: 'A', b: 'B' }
|
13
|
+
expect(@generator.send(:permit_params, input_params, required_keys)).to eq(output_params)
|
21
14
|
end
|
22
15
|
end
|
23
16
|
|
24
17
|
describe '#query' do
|
25
|
-
it 'should do generate right
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
18
|
+
it 'should do generate right params for integer' do
|
19
|
+
generator_type = :integer
|
20
|
+
input_params = { num: 4, max: 100 }
|
21
|
+
output_params =
|
22
|
+
{
|
23
|
+
path: '/integers',
|
24
|
+
params: {
|
25
|
+
format: 'plain',
|
26
|
+
base: '10',
|
27
|
+
rnd: 'new',
|
28
|
+
col: '1',
|
29
|
+
min: '1',
|
30
|
+
num: 4,
|
31
|
+
max: 100
|
32
|
+
}
|
33
|
+
}
|
31
34
|
|
32
|
-
|
33
|
-
object_type = 'string'
|
34
|
-
params = {min: 1, max: 10, num: 3, len: 5}
|
35
|
-
right_query = 'strings/?num=3&len=5&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new'
|
36
|
-
expect(@generator.send(:query, object_type, params)).to eq(right_query)
|
35
|
+
expect(@generator.send(:query, generator_type, input_params)).to eq(output_params)
|
37
36
|
end
|
38
37
|
|
39
|
-
it 'should do generate right
|
40
|
-
|
41
|
-
|
42
|
-
right_query = 'sequences/?min=1&max=10&col=1&format=plain&rnd=new'
|
43
|
-
expect(@generator.send(:query, object_type, params)).to eq(right_query)
|
44
|
-
end
|
45
|
-
end
|
38
|
+
it 'should do generate right params for string' do
|
39
|
+
generator_type = :string
|
40
|
+
input_params = { min: 1, max: 10, num: 3, len: 5 }
|
46
41
|
|
47
|
-
|
48
|
-
|
42
|
+
output_params = {
|
43
|
+
path: '/strings',
|
44
|
+
params: {
|
45
|
+
format: 'plain',
|
46
|
+
rnd: 'new',
|
47
|
+
digits: 'on',
|
48
|
+
loweralpha: 'on',
|
49
|
+
upperalpha: 'on',
|
50
|
+
unique: 'on',
|
51
|
+
num: 3,
|
52
|
+
len: 5
|
53
|
+
}
|
54
|
+
}
|
49
55
|
|
50
|
-
|
51
|
-
bad_query = 'strings/?num=HelloWorld&len=5&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=new'
|
52
|
-
expect(@generator.send(:request, domain, bad_query).class).to eq(String)
|
56
|
+
expect(@generator.send(:query, generator_type, input_params)).to eq(output_params)
|
53
57
|
end
|
54
58
|
|
55
|
-
it 'should do
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
59
|
+
it 'should do generate right params for sequence' do
|
60
|
+
generator_type = :sequence
|
61
|
+
input_params = { num: 5, len: 10, max: 10 }
|
62
|
+
|
63
|
+
output_params = {
|
64
|
+
path: '/sequences',
|
65
|
+
params: {
|
66
|
+
format: 'plain',
|
67
|
+
rnd: 'new',
|
68
|
+
col: '1',
|
69
|
+
min: '1',
|
70
|
+
max: 10
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
expect(@generator.send(:query, generator_type, input_params)).to eq(output_params)
|
60
75
|
end
|
61
76
|
end
|
62
|
-
|
63
77
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,9 +2,9 @@ require 'simplecov'
|
|
2
2
|
require 'coveralls'
|
3
3
|
|
4
4
|
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
5
|
-
|
6
|
-
|
5
|
+
SimpleCov::Formatter::HTMLFormatter,
|
6
|
+
Coveralls::SimpleCov::Formatter
|
7
7
|
]
|
8
8
|
SimpleCov.start
|
9
9
|
|
10
|
-
require File.expand_path(
|
10
|
+
require File.expand_path('../../lib/random_org_http_api', __FILE__)
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_org_http_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.alpha
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Temur Fatkulin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: retryable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -69,6 +83,7 @@ files:
|
|
69
83
|
- Rakefile
|
70
84
|
- lib/random_org_http_api.rb
|
71
85
|
- lib/random_org_http_api/configuration.rb
|
86
|
+
- lib/random_org_http_api/error.rb
|
72
87
|
- lib/random_org_http_api/generator.rb
|
73
88
|
- lib/random_org_http_api/version.rb
|
74
89
|
- random_org_http_api.gemspec
|
@@ -89,12 +104,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
104
|
version: '0'
|
90
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
106
|
requirements:
|
92
|
-
- - "
|
107
|
+
- - ">"
|
93
108
|
- !ruby/object:Gem::Version
|
94
|
-
version:
|
109
|
+
version: 1.3.1
|
95
110
|
requirements: []
|
96
111
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
112
|
+
rubygems_version: 2.5.1
|
98
113
|
signing_key:
|
99
114
|
specification_version: 4
|
100
115
|
summary: Realization of old HTTP API provided by the random.org service
|