reg.api2 0.0.1 → 0.0.2
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.
- data/.yard_redcarpet_ext +1 -0
- data/.yardopts +1 -0
- data/README.md +119 -7
- data/Rakefile +6 -2
- data/bin/regapi2console +10 -0
- data/lib/reg_api2/bill.rb +76 -0
- data/lib/reg_api2/common.rb +2 -4
- data/lib/reg_api2/console.rb +15 -0
- data/lib/reg_api2/console_helpers.rb +24 -0
- data/lib/reg_api2/domain.rb +96 -0
- data/lib/reg_api2/folder.rb +87 -0
- data/lib/reg_api2/impl.rb +18 -36
- data/lib/reg_api2/request_contract.rb +106 -0
- data/lib/reg_api2/result_contract.rb +118 -0
- data/lib/reg_api2/service.rb +122 -1
- data/lib/reg_api2/sym_hash.rb +63 -0
- data/lib/reg_api2/user.rb +15 -1
- data/lib/reg_api2/version.rb +1 -1
- data/lib/reg_api2/zone.rb +236 -0
- data/lib/reg_api2.rb +57 -17
- data/reg.api2.gemspec +3 -1
- data/spec/lib/reg_api2/bill_spec.rb +80 -0
- data/spec/lib/reg_api2/common_spec.rb +11 -9
- data/spec/lib/reg_api2/domain_spec.rb +26 -0
- data/spec/lib/reg_api2/folder_spec.rb +52 -0
- data/spec/lib/reg_api2/{request_contract/default_spec.rb → request_contract_spec.rb} +2 -2
- data/spec/lib/reg_api2/result_contract_spec.rb +58 -0
- data/spec/lib/reg_api2/service_spec.rb +27 -9
- data/spec/lib/reg_api2/sym_hash_spec.rb +81 -0
- data/spec/lib/reg_api2/user_spec.rb +35 -15
- data/spec/lib/reg_api2/zone_spec.rb +228 -0
- data/spec/spec_helper.rb +0 -1
- metadata +48 -22
- data/lib/reg_api2/clients.rb +0 -14
- data/lib/reg_api2/domains.rb +0 -13
- data/lib/reg_api2/request_contract/default.rb +0 -66
- data/lib/reg_api2/result_contract/default.rb +0 -25
- data/lib/reg_api2/result_contract/single_field.rb +0 -16
- data/lib/reg_api2/util.rb +0 -13
- data/spec/lib/reg_api2/clients_spec.rb +0 -10
- data/spec/lib/reg_api2/domains_spec.rb +0 -9
- data/spec/lib/reg_api2/result_contract/default_spec.rb +0 -25
- data/spec/lib/reg_api2/result_contract/single_field_spec.rb +0 -22
@@ -1,66 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
module RegApi2
|
3
|
-
# Contracts for API requests.
|
4
|
-
# Take a look at {RegApi2::DEFAULT_REQUEST_CONTRACT} for defaults.
|
5
|
-
module RequestContract
|
6
|
-
# Checks for specified `required` fields.
|
7
|
-
# Also checks for `optional` fields.
|
8
|
-
# Take in care :re option.
|
9
|
-
class Default
|
10
|
-
attr_reader :opts
|
11
|
-
|
12
|
-
def initialize(opts = {})
|
13
|
-
@opts = opts
|
14
|
-
end
|
15
|
-
|
16
|
-
# Normalizes `required` and `optional` fields to the form of Hash with options.
|
17
|
-
# @param [NilClass,Hash,Array, etc.] arr Something to normalize.
|
18
|
-
def to_hash arr
|
19
|
-
return {} if arr.nil?
|
20
|
-
return arr if arr.kind_of?(Hash)
|
21
|
-
arr = [ arr.to_sym ] unless arr.kind_of?(Array)
|
22
|
-
ret = {}
|
23
|
-
arr.each { |key| ret[key.to_sym] = {} }
|
24
|
-
ret
|
25
|
-
end
|
26
|
-
|
27
|
-
# Gets fields to validate
|
28
|
-
# @return [Hash] Fields to validate.
|
29
|
-
def fields_to_validate
|
30
|
-
required_fields = to_hash opts[:required]
|
31
|
-
optional_fields = to_hash opts[:optional]
|
32
|
-
required_fields.keys.each { |key| required_fields[key][:required] = true }
|
33
|
-
optional_fields.merge(required_fields)
|
34
|
-
end
|
35
|
-
|
36
|
-
# Validates specified `form` with `required` and `optional` fields.
|
37
|
-
# @param [Hash] form Form to validate.
|
38
|
-
# @raise ContractError
|
39
|
-
def validate(form)
|
40
|
-
fields = fields_to_validate
|
41
|
-
return if fields.empty?
|
42
|
-
absent_fields = []
|
43
|
-
fields.each_pair do |key, opts|
|
44
|
-
if !form.has_key?(key) || form[key].nil?
|
45
|
-
if opts[:required]
|
46
|
-
absent_fields << key
|
47
|
-
end
|
48
|
-
next
|
49
|
-
end
|
50
|
-
if opts[:re]
|
51
|
-
if form[key] !~ opts[:re]
|
52
|
-
raise RegApi2::ContractError.new(
|
53
|
-
"Field #{key} mismatch regular expression: #{form[key]}"
|
54
|
-
)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
unless absent_fields.empty?
|
59
|
-
raise RegApi2::ContractError.new(
|
60
|
-
"Required fields missed: #{absent_fields.join(', ')}"
|
61
|
-
)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
module RegApi2
|
3
|
-
# Contracts for API results.
|
4
|
-
# Take a look at {RegApi2::DEFAULT_RESULT_CONTRACT} for defaults.
|
5
|
-
module ResultContract
|
6
|
-
# Waits for answer field and returns it only.
|
7
|
-
class Default
|
8
|
-
attr_reader :opts
|
9
|
-
|
10
|
-
def initialize(opts = {})
|
11
|
-
@opts = opts
|
12
|
-
end
|
13
|
-
|
14
|
-
# Extracts answer field and returns it wrapped by {#handle_answer}.
|
15
|
-
def handle_result(result)
|
16
|
-
handle_answer(result['answer'])
|
17
|
-
end
|
18
|
-
|
19
|
-
# Return passed argument by default.
|
20
|
-
def handle_answer(answer)
|
21
|
-
answer
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
|
3
|
-
require 'reg_api2/result_contract/default'
|
4
|
-
|
5
|
-
# Waits for single field in answer field and returns it only.
|
6
|
-
class RegApi2::ResultContract::SingleField < RegApi2::ResultContract::Default
|
7
|
-
def handle_answer answer
|
8
|
-
field = opts[:field]
|
9
|
-
unless answer[field]
|
10
|
-
raise RegApi2::ContractError.new(
|
11
|
-
"#{field} field should be found in API result."
|
12
|
-
)
|
13
|
-
end
|
14
|
-
answer[field]
|
15
|
-
end
|
16
|
-
end
|
data/lib/reg_api2/util.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
module RegApi2
|
2
|
-
# Internal utilities
|
3
|
-
module Util
|
4
|
-
# Constantizes specified str
|
5
|
-
# @param [String] str String to constantize.
|
6
|
-
# @return [String] Constantized string.
|
7
|
-
def constantize str
|
8
|
-
str.to_s.split('_').map { |w| w.capitalize }.join('')
|
9
|
-
end
|
10
|
-
|
11
|
-
extend self
|
12
|
-
end
|
13
|
-
end
|
@@ -1,10 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
describe RegApi2 do
|
3
|
-
|
4
|
-
describe :nop do
|
5
|
-
it "should raise NO_SUCH_COMMAND" do
|
6
|
-
lambda { RegApi2.clients.nop }.should raise_error RegApi2::ApiError
|
7
|
-
lambda { RegApi2.clients.nop }.should raise_error /NO_SUCH_COMMAND/
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
describe RegApi2 do
|
3
|
-
describe :nop do
|
4
|
-
it "should raise NO_SUCH_COMMAND" do
|
5
|
-
lambda { RegApi2::Domains.nop }.should raise_error RegApi2::ApiError
|
6
|
-
lambda { RegApi2.domains.nop }.should raise_error /NO_SUCH_COMMAND/
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
describe RegApi2::ResultContract::Default do
|
3
|
-
|
4
|
-
let!(:contract) { RegApi2::ResultContract::Default.new(a: 1, b: 4) }
|
5
|
-
|
6
|
-
describe :initialize do
|
7
|
-
it "should assign opts" do
|
8
|
-
contract.opts.should == { a: 1, b: 4 }
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe :handle_result do
|
13
|
-
it "should return handle_answer" do
|
14
|
-
expected = 'OOLOLLO'
|
15
|
-
mock(contract).handle_answer({}) { expected }
|
16
|
-
contract.handle_result({ "answer" => {} }).should == expected
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe :handle_answer do
|
21
|
-
it "should return specified value" do
|
22
|
-
contract.handle_answer("FX").should == "FX"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
describe RegApi2::ResultContract::SingleField do
|
3
|
-
|
4
|
-
let!(:contract) { RegApi2::ResultContract::SingleField.new(field: 'one') }
|
5
|
-
|
6
|
-
describe :initialize do
|
7
|
-
it "should assign opts" do
|
8
|
-
contract.opts.should == { field: 'one' }
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe :handle_answer do
|
13
|
-
it "should return field value if exists" do
|
14
|
-
contract.handle_answer({ "one" => "FX" }).should == "FX"
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should raise ContractError on field unless exists" do
|
18
|
-
lambda { contract.handle_answer({}) }.should raise_error RegApi2::ContractError
|
19
|
-
lambda { contract.handle_answer({}) }.should raise_error /one/
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|