reg.api2 0.0.6 → 0.0.7
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/README.md +5 -0
- data/lib/reg_api2/console_helpers.rb +6 -7
- data/lib/reg_api2/impl.rb +6 -5
- data/lib/reg_api2/request_contract.rb +30 -13
- data/lib/reg_api2/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -60,6 +60,11 @@ Your default username: "test", and You can change it with "~/.regapi2".
|
|
60
60
|
1.9.3-p194 :002 >
|
61
61
|
```
|
62
62
|
|
63
|
+
Console use test/test credentials by default, but you can assign new ones
|
64
|
+
before any operations over REG.API through {RegApi2} properties.
|
65
|
+
|
66
|
+
Also console reads `~/.regapi2` configuration file if exists, to assign properties from. It knows about these properties in configuration file: `username`, `password`, `lang`, `pem`, `pem_password` and `ca_cert_path`.
|
67
|
+
|
63
68
|
## Recommendations for effective use of REG.API
|
64
69
|
|
65
70
|
This section provides information that will help you to make you work with REG.API more productive and convenient.
|
@@ -6,19 +6,18 @@ include RegApi2
|
|
6
6
|
# Try to load defaults from ~/.regapi2
|
7
7
|
def try_lo_load_defaults
|
8
8
|
filename = "#{ENV['HOME']}/.regapi2"
|
9
|
-
|
10
|
-
password = 'test'
|
9
|
+
props = %w[ username password lang pem pem_password ca_cert_path ]
|
11
10
|
|
12
11
|
if File.readable?(filename)
|
13
12
|
IO.read(filename).split("\n").each do |line|
|
14
|
-
|
15
|
-
|
13
|
+
line.strip!
|
14
|
+
props.each do |prop|
|
15
|
+
RegApi2.send("#{prop}=", $1) if line =~ /\A\s*#{Regexp.escape(prop)}\s*\=(.+)\s*\z/;
|
16
|
+
end
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
RegApi2.username
|
20
|
-
RegApi2.password = password
|
21
|
-
puts "Your default username: \"#{username}\", and You can change it with \"#{filename}\"."
|
20
|
+
puts "Your default username: \"#{RegApi2.username}\", and You can change it with \"#{filename}\"."
|
22
21
|
end
|
23
22
|
|
24
23
|
try_lo_load_defaults
|
data/lib/reg_api2/impl.rb
CHANGED
@@ -14,11 +14,13 @@ module RegApi2
|
|
14
14
|
# Raised when input parameters doesn't pass Ruby client tests.
|
15
15
|
class ContractError < ArgumentError
|
16
16
|
# @!attribute [r] fields
|
17
|
-
# Wrong fields.
|
17
|
+
# @return [Array<String>] Wrong fields.
|
18
18
|
attr_reader :fields
|
19
19
|
|
20
20
|
def initialize message, fields = []
|
21
21
|
super message
|
22
|
+
fields = [] if fields.nil?
|
23
|
+
fields = [ fields ] unless fields.kind_of?(Array)
|
22
24
|
@fields = fields
|
23
25
|
end
|
24
26
|
end
|
@@ -27,16 +29,16 @@ module RegApi2
|
|
27
29
|
# Please refer to {file:README.md#Common_error_codes common error codes}.
|
28
30
|
class ApiError < StandardError
|
29
31
|
# @!attribute [r] description
|
30
|
-
# Localized error description.
|
32
|
+
# @return [String] Localized error description.
|
31
33
|
attr_reader :description
|
32
34
|
# @!attribute [r] params
|
33
|
-
# Optional error params.
|
35
|
+
# @return [Hash] Optional error params.
|
34
36
|
attr_reader :params
|
35
37
|
|
36
38
|
def initialize code, description, params
|
37
39
|
super code
|
38
40
|
@description = description
|
39
|
-
@params = params
|
41
|
+
@params = params || {}
|
40
42
|
end
|
41
43
|
|
42
44
|
# Extracts error arguments from specified json.
|
@@ -182,7 +184,6 @@ module RegApi2
|
|
182
184
|
# @return [Object] Contracted response.
|
183
185
|
# @raise [NetError]
|
184
186
|
# @raise [ApiError]
|
185
|
-
# @raise [ContractError]
|
186
187
|
def handle_response(defopts, res)
|
187
188
|
raise NetError.new(res.body) unless res.code == '200'
|
188
189
|
|
@@ -46,7 +46,7 @@ module RegApi2
|
|
46
46
|
if value.to_s !~ opts[:re]
|
47
47
|
raise RegApi2::ContractError.new(
|
48
48
|
"Field #{key} mismatch regular expression: #{value}",
|
49
|
-
|
49
|
+
key
|
50
50
|
)
|
51
51
|
end
|
52
52
|
end
|
@@ -57,6 +57,7 @@ module RegApi2
|
|
57
57
|
# @param [Object] key Value to validate.
|
58
58
|
# @param [Object] value Value to validate.
|
59
59
|
# @param [Hash] opts opts with optional re field.
|
60
|
+
# @return [Object] Updated `value`
|
60
61
|
def validate_iso_date key, value, opts
|
61
62
|
if opts[:iso_date]
|
62
63
|
return value.strftime("%Y-%m-%d") if value.respond_to?(:strftime)
|
@@ -68,6 +69,7 @@ module RegApi2
|
|
68
69
|
# @param [Object] key Value to validate.
|
69
70
|
# @param [Object] value Value to validate.
|
70
71
|
# @param [Hash] opts opts with optional ipaddr field.
|
72
|
+
# @return [String] Updated `value`
|
71
73
|
def validate_ipaddr key, value, opts
|
72
74
|
if opts[:ipaddr] == true && value.kind_of?(String)
|
73
75
|
value = IPAddr.new(value)
|
@@ -75,24 +77,18 @@ module RegApi2
|
|
75
77
|
value.to_s
|
76
78
|
end
|
77
79
|
|
78
|
-
# Validates specified `form`
|
80
|
+
# Validates specified `form` for presence of all required fields.
|
79
81
|
# @param [Hash] form Form to validate.
|
82
|
+
# @param [Hash] fields Fields to test.
|
83
|
+
# return void
|
80
84
|
# @raise ContractError
|
81
|
-
def
|
82
|
-
fields = fields_to_validate
|
83
|
-
return form if fields.empty?
|
85
|
+
def validate_presence_of_required_fields form, fields
|
84
86
|
absent_fields = []
|
85
87
|
fields.each_pair do |key, opts|
|
88
|
+
next unless opts[:required]
|
86
89
|
if !form.has_key?(key) || form[key].nil?
|
87
|
-
|
88
|
-
absent_fields << key
|
89
|
-
end
|
90
|
-
next
|
90
|
+
absent_fields << key
|
91
91
|
end
|
92
|
-
|
93
|
-
form[key] = validate_re key, form[key], opts
|
94
|
-
form[key] = validate_iso_date key, form[key], opts
|
95
|
-
form[key] = validate_ipaddr key, form[key], opts
|
96
92
|
end
|
97
93
|
unless absent_fields.empty?
|
98
94
|
raise RegApi2::ContractError.new(
|
@@ -100,6 +96,27 @@ module RegApi2
|
|
100
96
|
absent_fields
|
101
97
|
)
|
102
98
|
end
|
99
|
+
nil
|
100
|
+
end
|
101
|
+
|
102
|
+
# Validates specified `form` with `required` and `optional` fields.
|
103
|
+
# @param [Hash] form Form to validate.
|
104
|
+
# @return [Hash] Updated form.
|
105
|
+
# @raise ContractError
|
106
|
+
def validate(form)
|
107
|
+
fields = fields_to_validate
|
108
|
+
return form if fields.empty?
|
109
|
+
|
110
|
+
validate_presence_of_required_fields form, fields
|
111
|
+
|
112
|
+
fields.each_pair do |key, opts|
|
113
|
+
next if !form.has_key?(key) || form[key].nil?
|
114
|
+
|
115
|
+
form[key] = validate_re key, form[key], opts
|
116
|
+
form[key] = validate_iso_date key, form[key], opts
|
117
|
+
form[key] = validate_ipaddr key, form[key], opts
|
118
|
+
end
|
119
|
+
|
103
120
|
form
|
104
121
|
end
|
105
122
|
end
|
data/lib/reg_api2/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reg.api2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -252,7 +252,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
252
252
|
version: '0'
|
253
253
|
segments:
|
254
254
|
- 0
|
255
|
-
hash: -
|
255
|
+
hash: -3548838866357939780
|
256
256
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
257
257
|
none: false
|
258
258
|
requirements:
|
@@ -261,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
261
261
|
version: '0'
|
262
262
|
segments:
|
263
263
|
- 0
|
264
|
-
hash: -
|
264
|
+
hash: -3548838866357939780
|
265
265
|
requirements: []
|
266
266
|
rubyforge_project:
|
267
267
|
rubygems_version: 1.8.24
|