Domain creating refactor

This commit is contained in:
Martin Lensment 2014-08-11 16:41:31 +03:00
parent a01f03a6d5
commit b26345dde4
8 changed files with 103 additions and 86 deletions

View file

@ -1,27 +1,12 @@
module Epp::DomainsHelper
def create_domain
ph = params_hash['epp']['command']['create']['create']
unless xml_attrs_present?(ph, [['name'], ['ns'], ['authInfo'], ['contact'], ['registrant']])
render '/epp/error' and return
end
@domain = Domain.new(domain_create_params(ph))
if owner_contact_id = Contact.find_by(code: ph[:registrant]).try(:id)
@domain.owner_contact_id = owner_contact_id
else
epp_errors << {code: '2303', msg: I18n.t('errors.messages.epp_registrant_not_found'), value: {obj: 'registrant', val: ph[:registrant]}}
render '/epp/error' and return
end
Domain.transaction do
if @domain.save && @domain.attach_contacts(domain_contacts) && @domain.attach_nameservers(domain_nameservers)
render '/epp/domains/create'
else
handle_errors(@domain)
raise ActiveRecord::Rollback
end
@domain = Domain.new(domain_create_params)
handle_errors(@domain) and return unless @domain.attach_objects(@ph, params[:frame])
handle_errors(@domain) and return unless @domain.save
render '/epp/domains/create'
end
end
@ -44,11 +29,31 @@ module Epp::DomainsHelper
### HELPER METHODS ###
private
## CREATE
def validate_domain_create_request
@ph = params_hash['epp']['command']['create']['create']
xml_attrs_present?(@ph, [['name'], ['ns'], ['authInfo'], ['contact'], ['registrant']])
end
def domain_create_params
{
name: @ph[:name],
registrar_id: current_epp_user.registrar.try(:id),
registered_at: Time.now,
period: @ph[:period].to_i,
valid_from: Date.today,
valid_to: Date.today + @ph[:period].to_i.years,
auth_info: @ph[:authInfo][:pw]
}
end
## RENEW
def validate_domain_renew_request
@ph = params_hash['epp']['command']['renew']['renew']
xml_attrs_present?(@ph, [['name'], ['curExpDate'], ['period']])
end
## SHARED
def find_domain
domain = Domain.find_by(name: @ph[:name])
unless domain
@ -56,38 +61,4 @@ module Epp::DomainsHelper
end
domain
end
def domain_create_params(ph)
{
name: ph[:name],
registrar_id: current_epp_user.registrar.try(:id),
registered_at: Time.now,
period: ph[:period].to_i,
valid_from: Date.today,
valid_to: Date.today + ph[:period].to_i.years,
auth_info: ph[:authInfo][:pw]
}
end
def domain_contacts
parsed_frame = Nokogiri::XML(params[:frame]).remove_namespaces!
res = {}
Contact::CONTACT_TYPES.each do |ct|
res[ct.to_sym] ||= []
parsed_frame.css("contact[type='#{ct}']").each do |x|
res[ct.to_sym] << Hash.from_xml(x.to_s).with_indifferent_access
end
end
res
end
def domain_nameservers
ph = params_hash['epp']['command']['create']['create']['ns']
return [] unless ph
return ph[:hostObj] if ph[:hostObj]
return ph[:hostAttr] if ph[:hostAttr]
[]
end
end