Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Created April 18, 2012 23:22
Show Gist options
  • Save luckyruby/2417321 to your computer and use it in GitHub Desktop.
Save luckyruby/2417321 to your computer and use it in GitHub Desktop.
require 'socket'
require 'nokogiri'
require 'uri'
require 'oj'
require 'net/http/persistent'
class XMLParser
def initialize(msg)
@doc = Nokogiri::XML(msg)
@nodes = {}
@doc.xpath("//foo").each do |node|
@nodes.merge!(extract_children(node))
end
@nodes
end
def extract_children(node)
nodes = {}
node.children.each do |child|
nodes[child.name] = child.content if child.name != 'text'
end
nodes
end
def nodes
@nodes
end
end
class XMLBuilder
def initialize(request, response)
@request = request
@response = Oj.load(response)
end
def xml_response
builder = Nokogiri::XML::Builder.new do |xml|
xml.foo {
xml.bar 'foobar'
}
end
builder.to_xml
end
end
server = TCPServer.new 5000
puts 'started'
loop do
Thread.start(server.accept) do |client|
while msg = client.gets("</foo>") do
xml = XMLParser.new(msg)
request = xml.nodes
url = "http://10.10.123.46:8080/requests/test"
http = Net::HTTP::Persistent.new('foobar').request(URI url)
client.puts XMLBuilder.new(request, http.body).xml_response
client.close
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment