refactoring

This commit is contained in:
Oleg Hasjanov 2021-06-15 10:39:46 +03:00
parent b7c0a6c4c9
commit 6a07aeb4c7
2 changed files with 103 additions and 71 deletions

View file

@ -6,13 +6,13 @@ module Depp
attr_accessor :name, :current_user, :epp_xml
STATUSES = %w(
STATUSES = %w[
clientDeleteProhibited
clientHold
clientRenewProhibited
clientTransferProhibited
clientUpdateProhibited
)
].freeze
PERIODS = [
['3 months', '3m'],
@ -28,11 +28,15 @@ module Depp
['8 years', '8y'],
['9 years', '9y'],
['10 years', '10y'],
]
].freeze
def initialize(args = {})
self.current_user = args[:current_user]
self.epp_xml = EppXml::Domain.new(cl_trid_prefix: current_user.tag, schema_prefix: 'domain-ee', schema_version: '1.1')
self.epp_xml = EppXml::Domain.new(
cl_trid_prefix: current_user.tag,
schema_prefix: 'domain-ee',
schema_version: '1.1'
)
end
def info(domain_name)
@ -49,6 +53,11 @@ module Depp
current_user.request(xml)
end
def hostname_present
domain_params[:nameservers_attributes]
.select { |_key, value| value['hostname'].present? }.any?
end
def create(domain_params)
dns_hash = {}
keys = Domain.create_dnskeys_hash(domain_params)
@ -57,8 +66,8 @@ module Depp
period = domain_params[:period].to_i.to_s
period_unit = domain_params[:period][-1].to_s
if domain_params[:nameservers_attributes].select { |key, value| value['hostname'].present? }.any?
xml = epp_xml.create({
xml = if hostname_present
epp_xml.create({
name: { value: domain_params[:name] },
period: { value: period, attrs: { unit: period_unit } },
ns: Domain.create_nameservers_hash(domain_params),
@ -66,7 +75,7 @@ module Depp
_anonymus: Domain.create_contacts_hash(domain_params)
}, dns_hash, Domain.construct_custom_params_hash(domain_params))
else
xml = epp_xml.create({
epp_xml.create({
name: { value: domain_params[:name] },
period: { value: period, attrs: { unit: period_unit } },
registrant: { value: domain_params[:registrant] },
@ -92,7 +101,8 @@ module Depp
def delete(domain_params)
xml = epp_xml.delete({
name: { value: domain_params[:name] }},
name: { value: domain_params[:name] },
},
Depp::Domain.construct_custom_params_hash(domain_params),
(domain_params[:verified].present? && 'yes'))
@ -189,6 +199,7 @@ module Depp
data.css('status').each_with_index do |x, i|
next unless STATUSES.include?(x['s'])
ret[:statuses_attributes][i] = {
code: x['s'],
description: x.text
@ -231,9 +242,12 @@ module Depp
rem_arr << { ns: rem_ns } if rem_ns.any?
rem_arr << { _anonymus: rem_anon } if rem_anon.any?
if domain_params[:registrant] != old_domain_params[:registrant]
chg = [{ registrant: { value: domain_params[:registrant] } }] if !domain_params[:verified].present?
chg = [{ registrant: { value: domain_params[:registrant], attrs: { verified: 'yes' } } }] if domain_params[:verified]
return if domain_params[:registrant] == old_domain_params[:registrant]
chg = [{ registrant: { value: domain_params[:registrant] } }]
if domain_params[:verified].blank? && (domain_params[:verified])
chg = [{ registrant: { value: domain_params[:registrant],
attrs: { verified: 'yes' } } }]
end
add_arr = nil if add_arr.none?
@ -263,13 +277,17 @@ module Depp
host_attr = []
host_attr << { hostName: { value: v['hostname'] } }
v['ipv4'].to_s.split(",").each do |ip|
if v['ipv4'].present?
v['ipv4'].to_s.split(',').each do |ip|
host_attr << { hostAddr: { value: ip, attrs: { ip: 'v4' } } }
end if v['ipv4'].present?
end
end
v['ipv6'].to_s.split(",").each do |ip|
if v['ipv6'].present?
v['ipv6'].to_s.split(',').each do |ip|
host_attr << { hostAddr: { value: ip, attrs: { ip: 'v6' } } }
end if v['ipv6'].present?
end
end
ret << { hostAttr: host_attr }
end
@ -281,6 +299,7 @@ module Depp
ret = []
domain_params[:contacts_attributes].each do |_k, v|
next if v['code'].blank?
ret << {
contact: { value: v['code'], attrs: { type: v['type'] } }
}
@ -294,9 +313,11 @@ module Depp
domain_params[:dnskeys_attributes].each do |_k, v|
if v['ds_key_tag'].blank?
kd = create_key_data_hash(v)
if kd
ret << {
keyData: kd
} if kd
}
end
else
ret << {
dsData: [
@ -315,6 +336,7 @@ module Depp
def create_key_data_hash(key_data_params)
return nil if key_data_params['public_key'].blank?
{
flags: { value: key_data_params['flags'] },
protocol: { value: key_data_params['protocol'] },

View file

@ -2,9 +2,9 @@ class EppConstraint
OBJECT_TYPES = {
domain: [
{ domain: Xsd::Schema.filename(for_prefix: 'domain-ee') },
{ domain: Xsd::Schema.filename(for_prefix: 'domain-eis') }
{ domain: Xsd::Schema.filename(for_prefix: 'domain-eis') },
],
contact: { contact: Xsd::Schema.filename(for_prefix: 'contact-ee') }
contact: { contact: Xsd::Schema.filename(for_prefix: 'contact-ee') },
}.freeze
def initialize(type)
@ -16,23 +16,14 @@ class EppConstraint
# TODO: Maybe move this to controller to keep params clean
return redirect_to_error_controller(request) if request.params[:action] == 'wrong_schema'
if request.params[:raw_frame]
request.params[:raw_frame] = request.params[:raw_frame].gsub!(/(?<=>)(.*?)(?=<)/) do |s|
s.strip
end
end
request.params[:nokogiri_frame] ||= Nokogiri::XML(request.params[:raw_frame] || request.params[:frame])
request.params[:parsed_frame] ||= request.params[:nokogiri_frame].dup.remove_namespaces!
request = parse_raw_frame(request) if request.params[:raw_frame]
request = parse_params(request)
unless %i[poll session].include?(@type)
element = "//#{@type}:#{request.params[:action]}"
if @type == :domain
OBJECT_TYPES[@type].each do |obj|
return true unless request.params[:nokogiri_frame].xpath(element.to_s, obj).none?
end
return false
end
return enumerate_domain_object(request, element) if @type == :domain
return false if request.params[:nokogiri_frame].xpath(element.to_s, OBJECT_TYPES[@type]).none?
end
@ -41,6 +32,25 @@ class EppConstraint
true
end
def parse_raw_frame(request)
request.params[:raw_frame] = request.params[:raw_frame].gsub!(/(?<=>)(.*?)(?=<)/, &:strip)
request
end
def enumerate_domain_object(request, element)
OBJECT_TYPES[@type].each do |obj|
return true unless request.params[:nokogiri_frame].xpath(element.to_s, obj).none?
end
false
end
def parse_params(request)
request.params[:nokogiri_frame] ||= Nokogiri::XML(request.params[:raw_frame] || request.params[:frame])
request.params[:parsed_frame] ||= request.params[:nokogiri_frame].dup.remove_namespaces!
request
end
def redirect_to_error_controller(request)
request.params[:epp_object_type] = @error
true