Refactored contact attribute assignment

This commit is contained in:
Andres Keskküla 2014-08-15 11:52:43 +03:00
parent 5b91acb343
commit 6bcf508fac
7 changed files with 94 additions and 33 deletions

View file

@ -13,7 +13,7 @@ module Epp::ContactsHelper
#TODO add support for rem and add
code = params_hash['epp']['command']['update']['update'][:id]
@contact = Contact.where(code: code).first
if stamp(@contact) && @contact.update_attributes(contact_and_address_attributes.delete_if { |k, v| v.nil? })
if stamp(@contact) && @contact.update_attributes(contact_and_address_attributes(:update))
render 'epp/contacts/update'
else
epp_errors << { code: '2303', msg: t('errors.messages.epp_obj_does_not_exist'), value: { obj: 'id', val: code } } if @contact == []
@ -101,32 +101,17 @@ module Epp::ContactsHelper
end
def contact_and_address_attributes
ph = params_hash['epp']['command'][params[:command]][params[:command]]
ph = ph[:chg] if params[:command] == 'update'
contact_hash = {
code: ph[:id],
phone: ph[:voice],
ident: ph[:ident],
ident_type: ident_type,
email: ph[:email],
}
contact_hash = contact_hash.merge({
name: ph[:postalInfo][:name],
org_name: ph[:postalInfo][:org]
}) if ph[:postalInfo].is_a? Hash
contact_hash = contact_hash.merge({
address_attributes: {
country_id: Country.find_by(iso: ph[:postalInfo][:addr][:cc]),
street: ph[:postalInfo][:addr][:street][0],
street2: ph[:postalInfo][:addr][:street][1],
street3: ph[:postalInfo][:addr][:street][2],
zip: ph[:postalInfo][:addr][:pc]
}
}) if ph[:postalInfo].is_a?(Hash) && ph[:postalInfo][:addr].is_a?(Hash)
def contact_and_address_attributes( type=:create )
case type
when :update
contact_hash = Contact.extract_attributes(@ph[:chg], type)
contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo).try(:[], :addr) || [] ), type)
else
contact_hash = Contact.extract_attributes(@ph, type)
contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :postalInfo).try(:[], :addr) || [] ), type)
end
contact_hash
end

View file

@ -1,4 +1,20 @@
class Address < ActiveRecord::Base
belongs_to :contact
belongs_to :country
class << self
def extract_attributes ah, type=:create
address_hash = {}
address_hash = ({
country_id: Country.find_by(iso: ah[:cc]).try(:id),
city: ah[:city],
street: ah[:street][0],
street2: ah[:street][1],
street3: ah[:street][2],
zip: ah[:pc]
}) if ah.is_a?(Hash)
address_hash.delete_if { |k, v| v.nil? }
end
end
end

View file

@ -67,6 +67,32 @@ class Contact < ActiveRecord::Base
end
class << self
def extract_attributes ph, type=:create
contact_hash = {
#code: ph[:id],
phone: ph[:voice],
ident: ph[:ident],
#ident_type: ident_type,
email: ph[:email]
}
contact_hash = contact_hash.merge({
name: ph[:postalInfo][:name],
org_name: ph[:postalInfo][:org]
}) if ph[:postalInfo].is_a? Hash
contact_hash[:code] = ph[:id] if type == :create
contact_hash.delete_if { |k, v| v.nil? }
end
def ident_type code
end
def check_availability(codes)
codes = [codes] if codes.is_a?(String)

View file

@ -68,8 +68,8 @@ describe 'EPP Contact', epp: true do
context 'update command' do
it "fails if request is invalid" do
#response = epp_request('contacts/update_missing_attr.xml')
response = epp_request(contact_update_xml( {id: false} ), :xml)
response = epp_request('contacts/update_missing_attr.xml')
#response = epp_request(contact_update_xml( {id: false} ), :xml)
expect(response[:results][0][:result_code]).to eq('2003')
expect(response[:results][0][:msg]).to eq('Required parameter missing: id')
@ -88,16 +88,18 @@ describe 'EPP Contact', epp: true do
it 'is succesful' do
Fabricate(:contact, created_by_id: 1, email: 'not_updated@test.test', code: 'sh8013')
response = epp_request(contact_update_xml( { chg: { email: 'fred@bloggers.ee', postalInfo: { name: 'Fred Bloggers' } } } ), :xml)
#response = epp_request(contact_update_xml( { chg: { email: 'fred@bloggers.ee', postalInfo: { name: 'Fred Bloggers' } } } ), :xml)
response = epp_request('contacts/update.xml')
expect(response[:msg]).to eq('Command completed successfully')
expect(Contact.first.name).to eq('Fred Bloggers')
expect(Contact.first.email).to eq('fred@bloggers.ee')
expect(Contact.first.name).to eq('John Doe')
expect(Contact.first.email).to eq('jdoe@example.com')
end
it 'returns phone and email error' do
Fabricate(:contact, created_by_id: 1, email: 'not_updated@test.test', code: 'sh8013')
response = epp_request(contact_update_xml( { chg: { email: "qwe", phone: "123qweasd" } }), :xml)
#response = epp_request(contact_update_xml( { chg: { email: "qwe", phone: "123qweasd" } }), :xml)
response = epp_request('contacts/update_with_errors.xml')
expect(response[:results][0][:result_code]).to eq('2005')
expect(response[:results][0][:msg]).to eq('Phone nr is invalid')

View file

@ -0,0 +1,4 @@
Fabricator(:country) do
iso Faker::Address.state_abbr
name Faker::Address.country
end

View file

@ -4,3 +4,19 @@ describe Address do
it { should belong_to(:contact) }
it { should belong_to(:country) }
end
describe Address, '.extract_params' do
it 'returns params hash'do
Fabricate(:country, iso:'EE')
ph = { postalInfo: { name: "fred", addr: { cc: 'EE', city: 'Village', street: [ 'street1', 'street2' ] } } }
expect(Address.extract_attributes(ph[:postalInfo][:addr])).to eq( {
city: 'Village',
country_id: 1,
street: 'street1',
street2: 'street2'
} )
end
end

View file

@ -65,6 +65,18 @@ describe Contact, '#upID' do
end
describe Contact, '.extract_params' do
it 'returns params hash'do
ph = { id: '123123', email: 'jdoe@example.com', postalInfo: { name: "fred", addr: { cc: 'EE' } } }
expect(Contact.extract_attributes(ph)).to eq( {
code: '123123',
email: 'jdoe@example.com',
name: 'fred'
} )
end
end
describe Contact, '.check_availability' do
before(:each) {