Created
April 18, 2012 23:22
-
-
Save luckyruby/2417321 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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