Skip to content

Instantly share code, notes, and snippets.

@airspeed
Last active January 25, 2017 11:32
Show Gist options
  • Save airspeed/66522b6ccd33e11be93be4cef391f38d to your computer and use it in GitHub Desktop.
Save airspeed/66522b6ccd33e11be93be4cef391f38d to your computer and use it in GitHub Desktop.
require "nokogiri"
def nokogiri_edit( handle, opts )
doc = Nokogiri::XML( handle )
# address update
if opts[ :address ] # start address
updated_address = opts[ :address ]
address = doc.xpath(
"//xmlns:PARTY[./bmecat:PARTY_ID[@type='systemspecific'][. = 'Address #{ opts[ :address ][ :id ] }'] and ./xmlns:PARTY_ROLE[. = 'delivery']]/xmlns:ADDRESS",
'xmlns' => 'http://www.opentrans.org/XMLSchema/2.1',
'bmecat' => 'http://www.bmecat.org/bmecat/2005'
).last
address.xpath( "./bmecat:NAME" ).last.content = "#{ updated_address[ :name ] } #{ updated_address[ :surname ] }"
address.xpath( "./bmecat:NAME3" ).last.content = updated_address[ :company ]
address.xpath( "./bmecat:STREET" ).last.content = updated_address[ :street ]
address.xpath( "./bmecat:ZIP" ).last.content = updated_address[ :zip_code ]
address.xpath( "./bmecat:CITY" ).last.content = updated_address[ :city ]
address.xpath( "./bmecat:STATE" ).last.content = updated_address[ :district ]
address.xpath( "./bmecat:COUNTRY_CODED" ).last.content = updated_address[ :country_code ]
puts "Updated address:"
puts address
end # end address
# storno
if opts[ :storno ] # start storno
order_info = doc.xpath(
"//xmlns:ORDER_INFO",
'xmlns' => 'http://www.opentrans.org/XMLSchema/2.1',
'bmecat' => 'http://www.bmecat.org/bmecat/2005'
).last
order_info.xpath( "./xmlns:UDX.INTOMEDIA.CANCELLATION" ).unlink # remove previous storno operations
order_info.prepend_child( "<UDX.INTOMEDIA.CANCELLATION>true</UDX.INTOMEDIA.CANCELLATION>" )
puts "Storno written:"
puts order_info
end # end storno
doc.to_xml
end
def start( input, output, opts )
ads = opts[ :addresses ]
# 1. Address update
ads.each do | a | # start addresses
xmlstring = nokogiri_edit( File.open( input ), {
:address => a,
:storno => false,
:email => opts[ :email ],
:order_id => opts[ :order_id ]
})
# write modified file
File.open( output, "w" ) do | edit |
edit.puts xmlstring
end
# overwrite original file
FileUtils.mv output, input
end # end addresses
# 2. Storno
if ( opts[ :storno ] ) # start storno
xmlstring = nokogiri_edit( File.open( input ), {
:address => nil,
:storno => true,
:email => opts[ :email ],
:order_id => opts[ :order_id ]
})
# write modified file
File.open( output, "w" ) do | edit |
edit.puts xmlstring
end
# overwrite original file
FileUtils.mv output, input
end # end storno
# upload to ftp server
# ...
input
end
start( '/Users/fpugl/Downloads/PX36103.xml', '/Users/fpugl/Downloads/PX36103.xml.edit', {
:addresses => [ {
:id => 'PX44255',
:name => 'Martin',
:surname => 'Mystere',
:company => 'Mysteres mysteries of the past',
:street => 'Washington Mews, 3',
:zip_code => '51346',
:city => 'New York',
:district => 'Columbia',
:country_code => 'US'
} ],
:storno => true,
:email => '[email protected]',
:order_id => 36103
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment