mirror of
https://github.com/internetee/registry.git
synced 2025-06-07 05:05:45 +02:00
Create contact XML deserializer
This commit is contained in:
parent
a1b39740e7
commit
cba445ee5b
2 changed files with 112 additions and 0 deletions
39
lib/deserializers/xml/contact.rb
Normal file
39
lib/deserializers/xml/contact.rb
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
module Deserializers
|
||||||
|
module Xml
|
||||||
|
class Contact
|
||||||
|
attr_reader :frame
|
||||||
|
|
||||||
|
def initialize(frame)
|
||||||
|
@frame = frame
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
attributes = {
|
||||||
|
name: if_present('postalInfo name'),
|
||||||
|
org: if_present('postalInfo org'),
|
||||||
|
email: if_present('email'),
|
||||||
|
fax: if_present('fax'),
|
||||||
|
phone: if_present('voice'),
|
||||||
|
|
||||||
|
# Address fields
|
||||||
|
city: if_present('postalInfo addr city'),
|
||||||
|
zip: if_present('postalInfo addr pc'),
|
||||||
|
street: if_present('postalInfo addr street'),
|
||||||
|
state: if_present('postalInfo addr sp'),
|
||||||
|
country_code: if_present('postalInfo addr cc'),
|
||||||
|
|
||||||
|
# Auth info
|
||||||
|
auth_info: if_present('authInfo pw'),
|
||||||
|
}
|
||||||
|
|
||||||
|
attributes.compact
|
||||||
|
end
|
||||||
|
|
||||||
|
def if_present(css_path)
|
||||||
|
return unless frame.css(css_path).present?
|
||||||
|
|
||||||
|
frame.css(css_path).text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
73
test/lib/deserializers/xml/contact_test.rb
Normal file
73
test/lib/deserializers/xml/contact_test.rb
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
require 'test_helper'
|
||||||
|
require 'deserializers/xml/contact'
|
||||||
|
|
||||||
|
class DeserializersXmlContactTest < ActiveSupport::TestCase
|
||||||
|
def test_trims_empty_values
|
||||||
|
xml_string = <<-XML
|
||||||
|
XML
|
||||||
|
|
||||||
|
nokogiri_frame = Nokogiri::XML(xml_string).remove_namespaces!
|
||||||
|
instance = ::Deserializers::Xml::Contact.new(nokogiri_frame)
|
||||||
|
assert_equal instance.call, {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_handles_update
|
||||||
|
xml_string = <<-XML
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<epp xmlns="https://epp.tld.ee/schema/epp-ee-1.0.xsd">
|
||||||
|
<command>
|
||||||
|
<update>
|
||||||
|
<contact:update xmlns:contact="https://epp.tld.ee/schema/contact-ee-1.1.xsd">
|
||||||
|
<contact:id>john-001</contact:id>
|
||||||
|
<contact:chg>
|
||||||
|
<contact:postalInfo>
|
||||||
|
<contact:name>new name</contact:name>
|
||||||
|
</contact:postalInfo>
|
||||||
|
<contact:voice>+123.4</contact:voice>
|
||||||
|
<contact:email>new-email@inbox.test</contact:email>
|
||||||
|
</contact:chg>
|
||||||
|
</contact:update>
|
||||||
|
</update>
|
||||||
|
</command>
|
||||||
|
</epp>
|
||||||
|
XML
|
||||||
|
|
||||||
|
nokogiri_frame = Nokogiri::XML(xml_string).remove_namespaces!
|
||||||
|
instance = ::Deserializers::Xml::Contact.new(nokogiri_frame)
|
||||||
|
assert_equal instance.call, { name: 'new name',
|
||||||
|
email: 'new-email@inbox.test',
|
||||||
|
phone: '+123.4' }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_handles_create
|
||||||
|
name = 'new'
|
||||||
|
email = 'new@registrar.test'
|
||||||
|
phone = '+1.2'
|
||||||
|
|
||||||
|
xml_string = <<-XML
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<epp xmlns="https://epp.tld.ee/schema/epp-ee-1.0.xsd">
|
||||||
|
<command>
|
||||||
|
<create>
|
||||||
|
<contact:create xmlns:contact="https://epp.tld.ee/schema/contact-ee-1.1.xsd">
|
||||||
|
<contact:postalInfo>
|
||||||
|
<contact:name>#{name}</contact:name>
|
||||||
|
</contact:postalInfo>
|
||||||
|
<contact:voice>#{phone}</contact:voice>
|
||||||
|
<contact:email>#{email}</contact:email>
|
||||||
|
</contact:create>
|
||||||
|
</create>
|
||||||
|
<extension>
|
||||||
|
<eis:extdata xmlns:eis="https://epp.tld.ee/schema/eis-1.0.xsd">
|
||||||
|
<eis:ident type="priv" cc="US">any</eis:ident>
|
||||||
|
</eis:extdata>
|
||||||
|
</extension>
|
||||||
|
</command>
|
||||||
|
</epp>
|
||||||
|
XML
|
||||||
|
|
||||||
|
nokogiri_frame = Nokogiri::XML(xml_string).remove_namespaces!
|
||||||
|
instance = ::Deserializers::Xml::Contact.new(nokogiri_frame)
|
||||||
|
assert_equal instance.call, { name: 'new', email: 'new@registrar.test', phone: '+1.2' }
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue