From acb65c47d65faa5c37c60187134f9cd8b4c2c834 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Wed, 2 Jul 2014 17:30:50 +0300 Subject: [PATCH] Refactor --- app/controllers/concerns/epp/common.rb | 14 +----- app/controllers/epp/commands_controller.rb | 6 ++- app/controllers/epp/sessions_controller.rb | 4 +- app/helpers/epp/contacts_helper.rb | 10 +--- app/helpers/epp/domains_helper.rb | 17 +++---- app/models/domain.rb | 2 + spec/epp/requests/contacts/create.xml | 56 +++++++++------------- 7 files changed, 39 insertions(+), 70 deletions(-) diff --git a/app/controllers/concerns/epp/common.rb b/app/controllers/concerns/epp/common.rb index 0ab5a5dcb..9a16b65fa 100644 --- a/app/controllers/concerns/epp/common.rb +++ b/app/controllers/concerns/epp/common.rb @@ -15,19 +15,7 @@ module Epp::Common end def get_params_hash(path) - node_set = parsed_frame.css(path).children.select{ |x| x.element? && x.element_children.empty? } - - node_set.inject({}) do |hash, obj| - #convert to array if 1 or more attributes with same name - if hash[obj.name.to_sym] && !hash[obj.name.to_sym].is_a?(Array) - hash[obj.name.to_sym] = [hash[obj.name.to_sym]] - hash[obj.name.to_sym] << obj.text.strip - else - hash[obj.name.to_sym] = obj.text.strip - end - - hash - end + Hash.from_xml(parsed_frame.css(path).to_xml).with_indifferent_access end def epp_session diff --git a/app/controllers/epp/commands_controller.rb b/app/controllers/epp/commands_controller.rb index 398a5aa65..38b88e98d 100644 --- a/app/controllers/epp/commands_controller.rb +++ b/app/controllers/epp/commands_controller.rb @@ -10,12 +10,14 @@ class Epp::CommandsController < ApplicationController private def create - type = OBJECT_TYPES[parsed_frame.css('create create').attr('schemaLocation').value] + ph = get_params_hash('create create')[:create] + type = OBJECT_TYPES[ph[:schemaLocation]] send("create_#{type}") end def check - type = OBJECT_TYPES[parsed_frame.css('check check').attr('schemaLocation').value] + ph = get_params_hash('check check')[:check] + type = OBJECT_TYPES[ph[:schemaLocation]] send("check_#{type}") end end diff --git a/app/controllers/epp/sessions_controller.rb b/app/controllers/epp/sessions_controller.rb index 5650b46af..6ebcbbbbe 100644 --- a/app/controllers/epp/sessions_controller.rb +++ b/app/controllers/epp/sessions_controller.rb @@ -27,7 +27,7 @@ class Epp::SessionsController < ApplicationController ### HELPER METHODS ### def login_params - login_params = parsed_frame.css('epp command login') - { username: login_params.css('clID').text, password: login_params.css('pw').text } + ph = get_params_hash('epp command login')[:login] + { username: ph[:clID], password: ph[:pw] } end end diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index ba2880531..5ebfdc037 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -1,13 +1,5 @@ module Epp::ContactsHelper def create_contact - ccp = contact_create_params - end - - ### HELPER METHODS ### - - def contact_create_params - { - addr: get_params_hash('epp command create create postalInfo addr') - } + cp = Hash.from_xml(parsed_frame.css("epp command create create").to_xml).with_indifferent_access end end diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index 350ef6cce..9f1820b60 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -5,28 +5,23 @@ module Epp::DomainsHelper end def check_domain - @domains = Domain.check_availability(domain_check_params[:names]) + ph = get_params_hash('epp command check check')[:check] + @domains = Domain.check_availability(ph[:name]) render '/epp/domains/check' end ### HELPER METHODS ### def domain_create_params - node_set = parsed_frame.css("epp command create create").children.select(&:element?) - command_params = node_set.inject({}) {|hash, obj| hash[obj.name.to_sym] = obj.text;hash } + ph = get_params_hash('epp command create create')[:create] { - name: command_params[:name], + name: ph[:name], registrar_id: current_epp_user.registrar.try(:id), registered_at: Time.now, valid_from: Date.today, - valid_to: Date.today + command_params[:period].to_i.years, - auth_info: command_params[:authInfo] + valid_to: Date.today + ph[:period].to_i.years, + auth_info: ph[:authInfo] } end - - def domain_check_params - node_set = parsed_frame.css('epp command check check name') - node_set.inject({names: []}){ |hash, obj| hash[:names] << obj.text; hash } - end end diff --git a/app/models/domain.rb b/app/models/domain.rb index b8d598ad8..187359e8a 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -20,6 +20,8 @@ class Domain < ActiveRecord::Base class << self def check_availability(domains) + domains = [domains] if domains.is_a?(String) + res = [] domains.each do |x| if !DomainNameValidator.validate(x) diff --git a/spec/epp/requests/contacts/create.xml b/spec/epp/requests/contacts/create.xml index 8dd05d831..054183e89 100644 --- a/spec/epp/requests/contacts/create.xml +++ b/spec/epp/requests/contacts/create.xml @@ -1,34 +1,24 @@ - - - - - - sh8013 - - John Doe - Example Inc. - - 123 Example Dr. - Suite 100 - Dulles - VA - 20166-6503 - US - - - +1.7035555555 - +1.7035555556 - jdoe@example.com - - 2fooBAR - - - - - - - - ABC-12345 - + +CID:TEST:10 + +Test +Test Street 11-2 +Test City +123456 +EE + + ++372.5555555 +test@test.com +37812124567 + + +neka005#10-02-08at13:51:37 +