reg.api2 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.yard_redcarpet_ext +1 -0
  2. data/.yardopts +1 -0
  3. data/README.md +119 -7
  4. data/Rakefile +6 -2
  5. data/bin/regapi2console +10 -0
  6. data/lib/reg_api2/bill.rb +76 -0
  7. data/lib/reg_api2/common.rb +2 -4
  8. data/lib/reg_api2/console.rb +15 -0
  9. data/lib/reg_api2/console_helpers.rb +24 -0
  10. data/lib/reg_api2/domain.rb +96 -0
  11. data/lib/reg_api2/folder.rb +87 -0
  12. data/lib/reg_api2/impl.rb +18 -36
  13. data/lib/reg_api2/request_contract.rb +106 -0
  14. data/lib/reg_api2/result_contract.rb +118 -0
  15. data/lib/reg_api2/service.rb +122 -1
  16. data/lib/reg_api2/sym_hash.rb +63 -0
  17. data/lib/reg_api2/user.rb +15 -1
  18. data/lib/reg_api2/version.rb +1 -1
  19. data/lib/reg_api2/zone.rb +236 -0
  20. data/lib/reg_api2.rb +57 -17
  21. data/reg.api2.gemspec +3 -1
  22. data/spec/lib/reg_api2/bill_spec.rb +80 -0
  23. data/spec/lib/reg_api2/common_spec.rb +11 -9
  24. data/spec/lib/reg_api2/domain_spec.rb +26 -0
  25. data/spec/lib/reg_api2/folder_spec.rb +52 -0
  26. data/spec/lib/reg_api2/{request_contract/default_spec.rb → request_contract_spec.rb} +2 -2
  27. data/spec/lib/reg_api2/result_contract_spec.rb +58 -0
  28. data/spec/lib/reg_api2/service_spec.rb +27 -9
  29. data/spec/lib/reg_api2/sym_hash_spec.rb +81 -0
  30. data/spec/lib/reg_api2/user_spec.rb +35 -15
  31. data/spec/lib/reg_api2/zone_spec.rb +228 -0
  32. data/spec/spec_helper.rb +0 -1
  33. metadata +48 -22
  34. data/lib/reg_api2/clients.rb +0 -14
  35. data/lib/reg_api2/domains.rb +0 -13
  36. data/lib/reg_api2/request_contract/default.rb +0 -66
  37. data/lib/reg_api2/result_contract/default.rb +0 -25
  38. data/lib/reg_api2/result_contract/single_field.rb +0 -16
  39. data/lib/reg_api2/util.rb +0 -13
  40. data/spec/lib/reg_api2/clients_spec.rb +0 -10
  41. data/spec/lib/reg_api2/domains_spec.rb +0 -9
  42. data/spec/lib/reg_api2/result_contract/default_spec.rb +0 -25
  43. data/spec/lib/reg_api2/result_contract/single_field_spec.rb +0 -22
@@ -0,0 +1,106 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'ipaddr'
3
+
4
+ module RegApi2
5
+ # Contract for API requests.
6
+ # Checks for specified `required` fields.
7
+ # Also checks for `optional` fields.
8
+ # Take in care :re option.
9
+ class RequestContract
10
+
11
+ # Options of contract.
12
+ # @return [Hash] Options hash.
13
+ attr_reader :opts
14
+
15
+ def initialize(opts = {})
16
+ @opts = opts
17
+ end
18
+
19
+ # Normalizes `required` and `optional` fields to the form of Hash with options.
20
+ # @param [NilClass,Hash,Array, etc.] arr Something to normalize.
21
+ def to_hash arr
22
+ return {} if arr.nil?
23
+ return arr if arr.kind_of?(Hash)
24
+ arr = [ arr.to_sym ] unless arr.kind_of?(Array)
25
+ ret = {}
26
+ arr.each { |key| ret[key.to_sym] = {} }
27
+ ret
28
+ end
29
+
30
+ # Gets fields to validate
31
+ # @return [Hash] Fields to validate.
32
+ def fields_to_validate
33
+ required_fields = to_hash opts[:required]
34
+ optional_fields = to_hash opts[:optional]
35
+ required_fields.keys.each { |key| required_fields[key][:required] = true }
36
+ optional_fields.merge(required_fields)
37
+ end
38
+
39
+ # Validates specified `value` with `re` field.
40
+ # @param [Object] key Value to validate.
41
+ # @param [Object] value Value to validate.
42
+ # @param [Hash] opts opts with optional re field.
43
+ # @raise ContractError
44
+ def validate_re key, value, opts
45
+ if opts[:re]
46
+ if value.to_s !~ opts[:re]
47
+ raise RegApi2::ContractError.new(
48
+ "Field #{key} mismatch regular expression: #{value}",
49
+ [ key ]
50
+ )
51
+ end
52
+ end
53
+ value
54
+ end
55
+
56
+ # Validates specified `value` with `re` field.
57
+ # @param [Object] key Value to validate.
58
+ # @param [Object] value Value to validate.
59
+ # @param [Hash] opts opts with optional re field.
60
+ def validate_iso_date key, value, opts
61
+ if opts[:iso_date]
62
+ return value.strftime("%Y-%m-%d") if value.respond_to?(:strftime)
63
+ end
64
+ value
65
+ end
66
+
67
+ # Validates specified `value` with `ipaddr` field.
68
+ # @param [Object] key Value to validate.
69
+ # @param [Object] value Value to validate.
70
+ # @param [Hash] opts opts with optional ipaddr field.
71
+ def validate_ipaddr key, value, opts
72
+ if opts[:ipaddr] == true && value.kind_of?(String)
73
+ value = IPAddr.new(value)
74
+ end
75
+ value.to_s
76
+ end
77
+
78
+ # Validates specified `form` with `required` and `optional` fields.
79
+ # @param [Hash] form Form to validate.
80
+ # @raise ContractError
81
+ def validate(form)
82
+ fields = fields_to_validate
83
+ return form if fields.empty?
84
+ absent_fields = []
85
+ fields.each_pair do |key, opts|
86
+ if !form.has_key?(key) || form[key].nil?
87
+ if opts[:required]
88
+ absent_fields << key
89
+ end
90
+ next
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
+ end
97
+ unless absent_fields.empty?
98
+ raise RegApi2::ContractError.new(
99
+ "Required fields missed: #{absent_fields.join(', ')}",
100
+ absent_fields
101
+ )
102
+ end
103
+ form
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,118 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ require 'reg_api2/sym_hash'
4
+
5
+ module RegApi2
6
+ # Contract for API results.
7
+ # Waits for answer field and returns it only by default.
8
+ # @see SymHash
9
+ class ResultContract
10
+ # Fields that will be converted to {Fixnum}.
11
+ # @see #convert
12
+ INT_FIELDS = %w[
13
+ active_domains_cnt
14
+ active_domains_get_ctrl_cnt
15
+ domain_folders_cnt
16
+ renew_domains_cnt
17
+ renew_domains_get_ctrl_cnt
18
+ undelegated_domains_cnt
19
+ bill_id
20
+ ].freeze
21
+
22
+ # Fields that will be converted to {Float}.
23
+ # @see #convert
24
+ FLOAT_FIELDS = %w[
25
+ amount
26
+ total_amount
27
+ payment
28
+ total_payment
29
+ ].freeze
30
+
31
+ # Fields that will be converted to {Boolean}.
32
+ # @see #convert
33
+ BOOL_FIELDS = %w[
34
+ success
35
+ ].freeze
36
+
37
+ # Options of contract.
38
+ # @return [Hash] Options hash.
39
+ attr_reader :opts
40
+
41
+ def initialize(opts = {})
42
+ @opts = opts
43
+ end
44
+
45
+ # Extracts answer field and returns it wrapped by {#handle_answer}.
46
+ # Result is unified using {SymHash.from}.
47
+ # @param [Hash] result API result.
48
+ # @return Reworked API answer field.
49
+ # @see SymHash
50
+ def handle_result(result)
51
+ result = SymHash.from(result)
52
+ handle_answer(result[:answer])
53
+ end
54
+
55
+ def convert_hash(answer)
56
+ answer.each_pair do |key, value|
57
+ case value
58
+ when String
59
+ if INT_FIELDS.include?(key.to_s)
60
+ answer[key] = value.to_i
61
+ elsif FLOAT_FIELDS.include?(key.to_s)
62
+ answer[key] = value.to_f
63
+ elsif BOOL_FIELDS.include?(key.to_s)
64
+ answer[key] = value.to_i.nonzero? ? true : false
65
+ end
66
+ when Hash
67
+ answer[key] = convert_hash(value)
68
+ when Array
69
+ answer[key] = convert_array(value)
70
+ end
71
+ end
72
+ answer
73
+ end
74
+
75
+ def convert_array(answer)
76
+ answer.map do |value|
77
+ case value
78
+ when Hash
79
+ convert_hash(value)
80
+ when Array
81
+ convert_array(value)
82
+ else
83
+ value
84
+ end
85
+ end
86
+ end
87
+
88
+ # Reworks answer to translate {INT_FIELDS}, {FLOAT_FIELDS} and {BOOL_FIELDS}.
89
+ # @param answer API answer field.
90
+ # @return Translated answer.
91
+ def convert(answer)
92
+ case answer
93
+ when Hash
94
+ convert_hash(answer)
95
+ when Array
96
+ convert_array(answer)
97
+ else
98
+ answer
99
+ end
100
+ end
101
+
102
+ # Handles API answer. Take in care `:field` option.
103
+ # @param answer API answer field.
104
+ # @return Converted answer by default.
105
+ # @see #convert
106
+ # @see #opts
107
+ def handle_answer(answer)
108
+ answer = convert(answer)
109
+ field = opts[:field]
110
+ if field
111
+ answer = answer[field]
112
+ end
113
+ answer
114
+ end
115
+
116
+ private :convert_array, :convert_hash
117
+ end
118
+ end
@@ -8,9 +8,9 @@ module RegApi2
8
8
  category :service
9
9
 
10
10
  # @!method nop(opts = {})
11
+ # Return list of specified services with its stats if specified.
11
12
  # @param opts
12
13
  # @option opts [Array] services
13
- # Return list of specified services with its stats if specified.
14
14
  # @return [Hash("services" => [])]
15
15
  # @example List of services by specified identifiers
16
16
  # RegApi2.service.nop(services: [
@@ -22,6 +22,127 @@ module RegApi2
22
22
  # ])
23
23
  define :nop
24
24
 
25
+ # @!method get_prices(opts = {})
26
+ # Get services registration/renewal prices.
27
+ # @param opts
28
+ # @option opts [Boolean] :show_renew_data With this flag enabled the system will return the price of renewal (1/0). Optional field.
29
+ # @option opts [String, Symbol] :currency Identifier of the currency in which the prices will be quoted: rur (default), uah, usd, eur. Optional field.
30
+ # @return [Hash(currency, price_group, show_renew_data, prices)] Prices.
31
+ # @example Get prices.
32
+ # RegApi2.service.get_prices
33
+ define :get_prices
34
+
35
+ # @!method get_servtype_details(opts = {})
36
+ # Use this function to get prices for services and general data.
37
+ # @note To obtain prices for several service types, you can define them in the servtype field delimiting them with commas or include several servtype fields into the request. In this case the field subtype is ignored.
38
+ # @param opts
39
+ # @option opts [String, Symbol] :servtype Type of service: srv_webfwd — web forwarding, srv_parking — domain parking, srv_dns_both — DNS support, srv_hosting_ispmgr — hosting, srv_certificate — domain certificate, srv_voucher — domain voucher, srv_kvm — KVM access.
40
+ # @option opts [String, Symbol] :subtype Service subtype.
41
+ # @option opts [Boolean] :unroll_prices Show prices in expanded form.
42
+ # @return [Array(Hash(commonname, ...))] Service type details.
43
+ # @example Get service type details.
44
+ # RegApi2.service.get_servtype_details servtype: :srv_hosting_ispmgr
45
+ define :get_servtype_details
46
+
47
+
48
+ # @!method create(opts = {})
49
+ # Use the function to order new services.
50
+ # `servtype` values are supported:
51
+ #
52
+ # | servtype | service type |
53
+ # | ---------|--------------|
54
+ # | srv_webfwd | web forwarding |
55
+ # | srv_parking | domain parking |
56
+ # | srv_dns_both | DNS support |
57
+ # | srv_hosting_ispmgr | ISPManager hosting |
58
+ # | srv_hosting_cpanel | CPanel hosting |
59
+ # | srv_hosting_plesk | PLesk hosting |
60
+ # | srv_ssl_certificate | SSL certificate |
61
+ # | srv_certificate | Domain certificate |
62
+ # | srv_voucher | Domain voucher |
63
+ # | srv_vps | VPS server |
64
+ # | srv_license_isp | ISP Manager license |
65
+ # | srv_addip | additional IP address |
66
+ # | srv_disk_space | additional disk space |
67
+ # | srv_antispam | advanced spam protection |
68
+ # | srv_dedicated | dedicated server |
69
+ # | srv_kvm | KVM access |
70
+ #
71
+ # Common payment options are allowed.
72
+ # folder_name or folder_id are allowed.
73
+ # @option opts [String] :domain_name Name of the domain, for which the service is ordered.
74
+ # @option opts [String, Symbol] :servtype The type of the ordered service.
75
+ # @option opts [Object] :period The period for which the service is ordered, the unit of measurement (year or month) depends on the type of the service being ordered. To learn about the measurement units used for each service, use the {#get_servtype_details} function.
76
+ # @option opts [String] :user_servid Domain ID set by the user. Allowed symbols: digits (from 0 through 9), Latin letters (from a through f). Maximum length: 32 symbols. This ID cannot be generated automatically; if it is not defined upon service ordering, the field will be empty. Optional field.
77
+ # @option opts :point_of_sale See common payment parameters in {file:README.md}.
78
+ # @option opts :pay_type See common payment parameters in {file:README.md}.
79
+ # @option opts :ok_if_no_money See common payment parameters in {file:README.md}.
80
+ # @option opts :folder_name Defines the name of the folder, to which the services will be added (see the list of common folder identification parameters in {file:README.md}).
81
+ # @option opts :folder_id Defines the name of the folder, to which the services will be added (see the list of common folder identification parameters in {file:README.md}).
82
+ # @option opts [Boolean] :no_new_folder Dont't create non-existing folder; otherwise create if not exists.
83
+ # @option opts [String] :comment An arbitrary text describing the order. Optional field.
84
+ # @option opts [String] :admin_comment A comment for administrators. An arbitrary text describing the order. Optional field.
85
+ # TODO: specific options.
86
+ # @return [Hash(descr, service_id, ...)] Information about ordered service.
87
+ # @see #get_servtype_details
88
+ # @example Create srv_hosting_ispmgr service.
89
+ # RegApi2.service.create dname: 'qqq.ru', servtype: :srv_hosting_ispmgr, period: 1, plan: 'Host-2-1209'
90
+ define :create, required: %w[ servtype ]
91
+
92
+ # @!method delete(opts = {})
93
+ define :delete, required: %w[ servtype ]
94
+
95
+ # @!method get_info(opts = {})
96
+ define :get_info
97
+
98
+ # @!method get_list(opts = {})
99
+ define :get_list
100
+
101
+ # @!method get_folders(opts = {})
102
+ define :get_folders
103
+
104
+ # @!method get_details(opts = {})
105
+ define :get_details
106
+
107
+ # @!method service_get_details(opts = {})
108
+ define :service_get_details
109
+
110
+ # @!method get_dedicated_server_list(opts = {})
111
+ define :get_dedicated_server_list
112
+
113
+ # @!method update(opts = {})
114
+ define :update
115
+
116
+ # @!method renew(opts = {})
117
+ define :renew
118
+
119
+ # @!method get_bills(opts = {})
120
+ define :get_bills
121
+
122
+ # @!method set_autorenew_flag(opts = {})
123
+ define :set_autorenew_flag
124
+
125
+ # @!method suspend(opts = {})
126
+ define :suspend
127
+
128
+ # @!method resume(opts = {})
129
+ define :resume
130
+
131
+ # @!method get_depreciated_period(opts = {})
132
+ define :get_depreciated_period
133
+
134
+ # @!method upgrade(opts = {})
135
+ define :upgrade
136
+
137
+ # @!method partcontrol_grant(opts = {})
138
+ define :partcontrol_grant
139
+
140
+ # @!method partcontrol_revoke(opts = {})
141
+ define :partcontrol_revoke
142
+
143
+ # @!method resend_mail(opts = {})
144
+ define :resend_mail
145
+
25
146
  extend self
26
147
  end
27
148
  end
@@ -0,0 +1,63 @@
1
+ module RegApi2
2
+ # Hash with indifferent access to its elements.
3
+ # Also have no difference between {String} ans {Symbol} keys.
4
+ # @see ResultContract
5
+ class SymHash < Hash
6
+ # Forms data with indifferent access from specified source.
7
+ # @return [Object] Data with indifferent access
8
+ def self.from(source)
9
+ case source
10
+ when Hash
11
+ res = SymHash.new
12
+ source.each_pair do |key, value|
13
+ res[key] = self.from(value)
14
+ end
15
+ res
16
+ when Array
17
+ source.map do |el|
18
+ self.from(el)
19
+ end
20
+ else
21
+ source
22
+ end
23
+ end
24
+
25
+ # Returns true if the given key is present in hsh.
26
+ def has_key?(key)
27
+ key.kind_of?(Symbol) ? self.has_key?(key.to_s) : super(key)
28
+ end
29
+
30
+ # Returns true if the given key is present in hsh.
31
+ def include?(key)
32
+ has_key?(key)
33
+ end
34
+
35
+ # Element Reference — Retrieves the value object corresponding to the key object. If not found, returns the default value (see {Hash::new} for details).
36
+ def [](key)
37
+ key.kind_of?(Symbol) ? self[key.to_s] : super(key)
38
+ end
39
+
40
+ # Element Assignment — Associates the value given by value with the key given by key. key should not have its value changed while it is in use as a key (a String passed as a key will be duplicated and frozen).
41
+ def []=(key, new_value)
42
+ key.kind_of?(Symbol) ? self[key.to_s]=new_value : super(key, new_value)
43
+ end
44
+
45
+ # Always true
46
+ def respond_to?(key)
47
+ true
48
+ end
49
+
50
+ # Sets or gets field in the hash.
51
+ def method_missing(key, *args, &block)
52
+ if key.to_s =~ /\A(.+)=\z/
53
+ self[$1] = args.first
54
+ return args.first
55
+ end
56
+ if key.to_s =~ /\A(.+)\?\z/
57
+ return !!self[$1]
58
+ end
59
+ return self[key] if has_key?(key)
60
+ nil
61
+ end
62
+ end
63
+ end
data/lib/reg_api2/user.rb CHANGED
@@ -25,7 +25,7 @@ module RegApi2
25
25
  # @return [String] user_id of new user.
26
26
  # @note Accessability: partners
27
27
  # New user registration.
28
- define :create, required: %w[ user_login user_password user_email user_country_code ], field: 'user_id', result: :single_field
28
+ define :create, required: %w[ user_login user_password user_email user_country_code ], field: :user_id
29
29
 
30
30
  # @!method get_statistics(opts = {})
31
31
  # @param opts Opts
@@ -46,6 +46,20 @@ module RegApi2
46
46
  # @return [Hash(currency, prepay, blocked, credit)] User balance
47
47
  define :get_balance, required: %w[ currency ]
48
48
 
49
+ # @!method refill_balance(opts = {})
50
+ # Add funds to the account. Allows sending invoices to WebMoney and Yandex.Dengi.
51
+ # @param opts Opts
52
+ # @option opts [String] :pay_type Payment method. Available options: WM — WebMoney, requires specifying of WMID; ymbill - Yandex.Dengi (the invoice receipt function should be enabled).
53
+ # @option opts [String] :wmid WebMoney ID
54
+ # @option opts [String] :currency Currency of the transfer. RUR only for Yandex.Dengi; USD, EUR and UAH for WebMoney.
55
+ # @option opts [String] :amount Amount of the invoice.
56
+ # @note Accessability: clients
57
+ # @note Support of service lists: no
58
+ # @return [Hash(total_payment, ...)] Result of operation.
59
+ # @example Add funds though WebMoney
60
+ # RegApi2.user.refill_balance(pay_type: 'WM', wmid: 123456789012, currency: 'RUR', amount: 1000)
61
+ define :refill_balance, required: %w[ pay_type wmid currency amount ]
62
+
49
63
  extend self
50
64
  end
51
65
  end
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module RegApi2
3
3
  # Gem version.
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2".freeze
5
5
  end