Skip to content

Instantly share code, notes, and snippets.

@cilim
Created May 16, 2019 09:35
Show Gist options
  • Save cilim/a6ee3e0b7287ff067da0d3a42097e277 to your computer and use it in GitHub Desktop.
Save cilim/a6ee3e0b7287ff067da0d3a42097e277 to your computer and use it in GitHub Desktop.
XML parser
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns2="http://www.opentravel.org/OTA/2003/05">
<SOAP-ENV:Header>
<ns1:Security>true</ns1:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:OTA_HotelAvailNotifRS TimeStamp="2017-07-20T10:47:34+00:00">
<ns2:Errors>
<ns2:Error Code="1" ShortText="Invalid Room Code : ROOM2410" Type="3"/>
</ns2:Errors>
</ns2:OTA_HotelAvailNotifRS>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
module Import
module Origin
module Xml
module Parse
class Ceki < Base
param :success, element: 'ns2:Success'
has_many :errors, element: 'ns2:Errors//ns2:Error', parser: CekiError
def success?
success.nil? ? false : true
end
end
end
end
end
end
module Import
module Origin
module Xml
module Parse
class CekiError < Base
param :code, element: 'Code', attribute: true
param :description, element: 'ShortText', attribute: true
end
end
end
end
end
describe Import::Origin::Xml::Parse::Ceki do
let(:parser) { described_class.new(node) }
context 'success' do
let(:node) { xml_element(:good_ceki, name: 'ns2:OTA_HotelAvailNotifRS') }
let(:params) { { success: '' } }
it 'responds to params and errors' do
expect(parser.params).to eq params
expect(parser.errors.size).to eq 0
end
it 'success?' do
expect(parser.success?).to eq true
end
end
context 'errors' do
let(:node) { xml_element(:bad_ceki, name: 'ns2:OTA_HotelAvailNotifRS') }
let(:params) { { success: nil } }
it 'responds to params and errors' do
expect(parser.params).to eq params
expect(parser.errors.size).to eq 1
end
it 'success?' do
expect(parser.success?).to eq false
end
describe 'errors' do
it 'contains correct values' do
error = parser.errors.first
expect(error.code).to eq '1'
expect(error.description).to eq 'Invalid Room Code : ROOM2410'
end
end
end
end
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns2="http://www.opentravel.org/OTA/2003/05">
<SOAP-ENV:Header>
<ns1:Security>true</ns1:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:OTA_HotelAvailNotifRS TimeStamp="2017-07-20T10:47:34+00:00">
<ns2:Success/>
</ns2:OTA_HotelAvailNotifRS>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
@cilim
Copy link
Author

cilim commented May 16, 2019

One step missing, the parser needs to receive a XML element (node), which you can get like this:

Nokogiri::XML(PhobsLog.find(1).response_data) { |config| config.strict.noblanks }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment