random_org_http_api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +76 -0
- data/Rakefile +2 -0
- data/lib/random_org_http_api/configuration.rb +30 -0
- data/lib/random_org_http_api/generator.rb +53 -0
- data/lib/random_org_http_api/version.rb +3 -0
- data/lib/random_org_http_api.rb +5 -0
- data/random_org_http_api.gemspec +23 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dbea6bdd5b9f7d7494b0d5f01ae6c2d96ea71148
|
4
|
+
data.tar.gz: a0ab30808e48caa6b4abbd9fed281a271014da6d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b988426d9015db7afe1a2ebd7c3a88992a3bcd259a54450a7046bffdde10211ec211d2c71bba120d8ba07c1aab7ee3d7e56578a39453fea801b942921866329
|
7
|
+
data.tar.gz: 2dc4c7bce8019a3b753206333f7960019257dfb8d8a29b01df22aeaecb4cb3f03c2a27e8edadbc7eaaf587befceee6c70ef81428c0f5f13aadd6f70107c8ede4
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Temur Fatkulin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# RandomOrgHttpApi
|
2
|
+
|
3
|
+
Realization of old [HTTP API](http://www.random.org/clients/http/) provided by the random.org service.
|
4
|
+
Not to confuse to new [JSON-RPC API](https://api.random.org/json-rpc/1/) which now is in beta release.
|
5
|
+
|
6
|
+
At present realized:
|
7
|
+
|
8
|
+
* generation of numbers
|
9
|
+
* generation of numbers
|
10
|
+
* generation of numbers
|
11
|
+
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'random_org_http_api'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install random_org_http_api
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
require 'random_org_http_api'
|
30
|
+
|
31
|
+
g = RandomOrgHttpApi::Generator.new
|
32
|
+
|
33
|
+
g.generate_integers(num: 4, max: 100)
|
34
|
+
=> ["10", "23", "42", "28"]
|
35
|
+
|
36
|
+
g.generate_strings(min: 1, max: 10, num: 3, len: 5)
|
37
|
+
=> ["XxH06", "3UHOu", "Asjd0"]
|
38
|
+
|
39
|
+
g.generate_sequence(num: 5, len: 10, max: 10)
|
40
|
+
=> ["8", "9", "10", "3", "2", "7", "1", "5", "4", "6"]
|
41
|
+
|
42
|
+
## More available parameters for generate_* methods
|
43
|
+
|
44
|
+
### generate_integers
|
45
|
+
|
46
|
+
:min
|
47
|
+
:col
|
48
|
+
:base
|
49
|
+
:format
|
50
|
+
:rnd
|
51
|
+
|
52
|
+
|
53
|
+
### generate_sequence
|
54
|
+
|
55
|
+
:col
|
56
|
+
:format
|
57
|
+
:rnd
|
58
|
+
|
59
|
+
### generate_strings
|
60
|
+
|
61
|
+
:digits
|
62
|
+
:upperalpha
|
63
|
+
:loweralpha
|
64
|
+
:unique
|
65
|
+
:format
|
66
|
+
:rnd
|
67
|
+
|
68
|
+
More information about available parameters on [http://www.random.org/clients/http/](http://www.random.org/clients/http/)
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it ( https://github.com/[my-github-username]/random_org_http_api/fork )
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module RandomOrgHttpApi
|
2
|
+
module Configuration
|
3
|
+
DOMAIN = 'www.random.org'
|
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_PARAMS = [: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}"
|
20
|
+
|
21
|
+
# String
|
22
|
+
STRING_QUERY_PARAMS = [:num, :len, :digits, :upperalpha, :loweralpha, :unique, :format, :rnd]
|
23
|
+
STRING_QUERY_TEMPLATE = "/strings/?num=%{num}&len=%{len}&digits=%{digits}&upperalpha=%{upperalpha}&loweralpha=%{loweralpha}&unique=%{unique}&format=%{format}&rnd=%{rnd}"
|
24
|
+
|
25
|
+
# Sequence
|
26
|
+
SEQUENCE_QUERY_PARAMS = [:min, :max, :col, :format, :rnd]
|
27
|
+
SEQUENCE_QUERY_TEMPLATE = "/sequences/?min=%{min}&max=%{max}&col=%{col}&format=%{format}&rnd=%{rnd}"
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module RandomOrgHttpApi
|
2
|
+
class Generator
|
3
|
+
require 'net/http'
|
4
|
+
include RandomOrgHttpApi::Configuration
|
5
|
+
|
6
|
+
def generate_integers(params = {})
|
7
|
+
request('integer', params)
|
8
|
+
end
|
9
|
+
|
10
|
+
def generate_strings(params = {})
|
11
|
+
request('string', params)
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate_sequence(params = {})
|
15
|
+
request('sequence', params)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# Метод запроса на сайт random.org
|
21
|
+
def request(object_type, params)
|
22
|
+
# В зависимости от типа получаемого объекта валидируем параметры и подставляем в нужный темплейт
|
23
|
+
case object_type
|
24
|
+
when 'integer' then
|
25
|
+
all_params = permit_params(DEFAULT_QUERY_PARAMS, params, INTEGER_QUERY_PARAMS)
|
26
|
+
query = generate_query(all_params, INTEGER_QUERY_TEMPLATE)
|
27
|
+
when 'string' then
|
28
|
+
all_params = permit_params(DEFAULT_QUERY_PARAMS, params, STRING_QUERY_PARAMS)
|
29
|
+
query = generate_query(all_params, STRING_QUERY_TEMPLATE)
|
30
|
+
when 'sequence' then
|
31
|
+
all_params = permit_params(DEFAULT_QUERY_PARAMS, params, SEQUENCE_QUERY_PARAMS)
|
32
|
+
query = generate_query(all_params, SEQUENCE_QUERY_TEMPLATE)
|
33
|
+
end
|
34
|
+
# Создаем запрос
|
35
|
+
res = Net::HTTP.get_response(DOMAIN, query)
|
36
|
+
# Если все хорошо, то возвращаем форматированный ответ,
|
37
|
+
# если нет, то показываем ответ ошибки сервера.
|
38
|
+
res.is_a?(Net::HTTPSuccess) ? res.body.split : res.body.strip
|
39
|
+
end
|
40
|
+
|
41
|
+
# Метод оставляющий только нужные параметры.
|
42
|
+
def permit_params(default_params, params, required_params )
|
43
|
+
@params = default_params.merge(params)
|
44
|
+
@params = @params.select {|k,v| required_params.include?(k) }
|
45
|
+
end
|
46
|
+
|
47
|
+
# Метод подстановки параметров в темплейт. Параметр должен быть хешем.
|
48
|
+
def generate_query(params, template)
|
49
|
+
template % params
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'random_org_http_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
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 = %q{Realization of old HTTP API provided by the random.org service}
|
12
|
+
spec.description = %q{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
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: random_org_http_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Temur Fatkulin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: At present realized generation for numbers, strings and sequences
|
42
|
+
email:
|
43
|
+
- temur.fatkulin@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/random_org_http_api.rb
|
54
|
+
- lib/random_org_http_api/configuration.rb
|
55
|
+
- lib/random_org_http_api/generator.rb
|
56
|
+
- lib/random_org_http_api/version.rb
|
57
|
+
- random_org_http_api.gemspec
|
58
|
+
homepage: https://github.com/istickz/random_org_http_api
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.2.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Realization of old HTTP API provided by the random.org service
|
82
|
+
test_files: []
|