Skip to content

Commit

Permalink
Organize xml processor classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nas committed Apr 27, 2011
1 parent 614db50 commit 5028fc1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 39 deletions.
7 changes: 7 additions & 0 deletions lib/opensrs/xml_processor.rb
@@ -0,0 +1,7 @@
module OpenSRS

class XmlProcessor

end

end
4 changes: 2 additions & 2 deletions lib/opensrs/xml.rb → lib/opensrs/xml_processor/libxml.rb
@@ -1,8 +1,8 @@
require "libxml"

module OpenSRS
class XML
include LibXML::XML
class XmlProcessor::Libxml < OpenSRS::XmlProcessor
include ::LibXML::XML

# First, builds REXML elements for the inputted data. Then, it will
# go ahead and build the entire XML document to send to OpenSRS.
Expand Down
@@ -1,7 +1,12 @@
require "nokogiri"
begin
require 'nokogiri'
rescue LoadError => e
$stderr.puts "You don't have nokogiri installed in your application. Please install the Nokogiri gem before using it as the xml processor"
raise e
end

module OpenSRS
class Nokogiri
class XmlProcessor::Nokogiri < OpenSRS::XmlProcessor

def self.build(data)
builder = ::Nokogiri::XML::Builder.new
Expand Down
@@ -1,19 +1,19 @@
require 'spec_helper'
require 'date'

describe OpenSRS::XML do
describe OpenSRS::XmlProcessor::Libxml do
describe ".build" do
it "should create XML for a nested hash" do
attributes = {:foo => {:bar => 'baz'}}
xml = OpenSRS::XML.build(attributes)
xml = OpenSRS::XmlProcessor::Libxml.build(attributes)
xml.should eq %{<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<OPS_envelope>\n <header>\n <version>0.9</version>\n </header>\n <body>\n <data_block>\n <dt_assoc>\n <item key=\"foo\">\n <dt_assoc>\n <item key=\"bar\">baz</item>\n </dt_assoc>\n </item>\n </dt_assoc>\n </data_block>\n </body>\n</OPS_envelope>\n}
end
end

describe '.encode_data' do
context "on a 3 element array" do
before(:each) do
@e = OpenSRS::XML.encode_data([1,2,3])
@e = OpenSRS::XmlProcessor::Libxml.encode_data([1,2,3])
end

it "is a REXML::Element" do
Expand All @@ -40,7 +40,7 @@

context "on a hash" do
before(:each) do
@e = OpenSRS::XML.encode_data({:name => "kitteh"})
@e = OpenSRS::XmlProcessor::Libxml.encode_data({:name => "kitteh"})
end

it "is a REXML::Element" do
Expand All @@ -60,7 +60,7 @@

context "on a nested hash" do
before(:each) do
@e = OpenSRS::XML.encode_data({:suggestion => {:maximum => "10"}})
@e = OpenSRS::XmlProcessor::Libxml.encode_data({:suggestion => {:maximum => "10"}})
end

it "is a REXML::Element" do
Expand Down Expand Up @@ -90,29 +90,29 @@

context "produces a scalar" do
it "from a string" do
OpenSRS::XML.encode_data("cheezburger").to_s.should == "cheezburger"
OpenSRS::XmlProcessor::Libxml.encode_data("cheezburger").to_s.should == "cheezburger"
end

it "from a string with XML characters" do
OpenSRS::XML.encode_data("<smile>").to_s.should == "<smile>"
OpenSRS::XmlProcessor::Libxml.encode_data("<smile>").to_s.should == "<smile>"
end

it "from an integer" do
OpenSRS::XML.encode_data(12345).to_s.should == "12345"
OpenSRS::XmlProcessor::Libxml.encode_data(12345).to_s.should == "12345"
end

it "from a date" do
date = Date.parse("2010/02/12")
OpenSRS::XML.encode_data(date).to_s.should == "2010-02-12"
OpenSRS::XmlProcessor::Libxml.encode_data(date).to_s.should == "2010-02-12"
end

it "from a symbol" do
OpenSRS::XML.encode_data(:name).to_s.should == "name"
OpenSRS::XmlProcessor::Libxml.encode_data(:name).to_s.should == "name"
end

it "from true or false" do
OpenSRS::XML.encode_data(true).to_s.should == "true"
OpenSRS::XML.encode_data(false).to_s.should == "false"
OpenSRS::XmlProcessor::Libxml.encode_data(true).to_s.should == "true"
OpenSRS::XmlProcessor::Libxml.encode_data(false).to_s.should == "false"
end
end
end
Expand All @@ -132,7 +132,7 @@
</body>
</OPS_envelope>}

resp = OpenSRS::XML.parse(xml)
resp = OpenSRS::XmlProcessor::Libxml.parse(xml)
resp.should == "Tom Jones"
end

Expand All @@ -158,7 +158,7 @@
</body>
</OPS_envelope>}

resp = OpenSRS::XML.parse(xml)
resp = OpenSRS::XmlProcessor::Libxml.parse(xml)
resp["domain_list"].class.should == Array
resp["domain_list"][0].should == "ns1.example.com"
resp["domain_list"][1].should == "ns2.example.com"
Expand Down Expand Up @@ -196,7 +196,7 @@
</body>
</OPS_envelope>}

resp = OpenSRS::XML.parse(xml)
resp = OpenSRS::XmlProcessor::Libxml.parse(xml)

resp["contact_set"]["owner"]["first_name"].should == "Tom"
resp["contact_set"]["owner"]["last_name"].should == "Jones"
Expand Down Expand Up @@ -232,7 +232,7 @@
</body>
</OPS_envelope>}

@resp = OpenSRS::XML.parse(xml)
@resp = OpenSRS::XmlProcessor::Libxml.parse(xml)
end

it "produces a hash" do
Expand Down
@@ -1,11 +1,11 @@
require 'spec_helper'
require 'date'

describe OpenSRS::Nokogiri do
describe OpenSRS::XmlProcessor::Nokogiri do
describe ".build" do
it "should create XML for a nested hash" do
attributes = {:foo => {:bar => 'baz'}}
xml = OpenSRS::Nokogiri.build(attributes)
xml = OpenSRS::XmlProcessor::Nokogiri.build(attributes)
xml.should eq %{<?xml version=\"1.0\"?>\n<OPS_envelope>\n <header>\n <version>0.9</version>\n </header>\n <body>\n <data_block>\n <dt_assoc>\n <item key=\"foo\">\n <dt_assoc>\n <item key=\"bar\">baz</item>\n </dt_assoc>\n </item>\n </dt_assoc>\n </data_block>\n </body>\n</OPS_envelope>\n}
end
end
Expand All @@ -19,7 +19,7 @@

context "on a 3 element array" do
before(:each) do
@e = OpenSRS::Nokogiri.encode_data([1,2,3], @doc)
@e = OpenSRS::XmlProcessor::Nokogiri.encode_data([1,2,3], @doc)
end

it { @e.should be_an_instance_of(::Nokogiri::XML::Element) }
Expand All @@ -37,7 +37,7 @@

context "on a hash" do
before(:each) do
@e = OpenSRS::Nokogiri.encode_data({:name => "kitteh"}, @doc)
@e = OpenSRS::XmlProcessor::Nokogiri.encode_data({:name => "kitteh"}, @doc)
end

it{ @e.should be_an_instance_of(::Nokogiri::XML::Element) }
Expand All @@ -50,7 +50,7 @@

context "on a nested hash" do
before(:each) do
@e = OpenSRS::Nokogiri.encode_data({:suggestion => {:maximum => "10"}}, @doc)
@e = OpenSRS::XmlProcessor::Nokogiri.encode_data({:suggestion => {:maximum => "10"}}, @doc)
@suggestion = @e.children[0]
@dt_assoc = @suggestion.children[0]
end
Expand Down Expand Up @@ -80,14 +80,14 @@
end

context "produces a scalar" do
it { OpenSRS::Nokogiri.encode_data("cheezburger").to_s.should eql("cheezburger") }
it { OpenSRS::Nokogiri.encode_data("<smile>").to_s.should eql("<smile>") }

it { OpenSRS::Nokogiri.encode_data(12345).to_s.should eql("12345") }
it { OpenSRS::Nokogiri.encode_data(Date.parse("2010/02/12")).to_s.should eql("2010-02-12") }
it { OpenSRS::Nokogiri.encode_data(:name).to_s.should eql("name") }
it { OpenSRS::Nokogiri.encode_data(true).to_s.should eql("true") }
it { OpenSRS::Nokogiri.encode_data(false).to_s.should eql("false") }
it { OpenSRS::XmlProcessor::Nokogiri.encode_data("cheezburger").to_s.should eql("cheezburger") }
it { OpenSRS::XmlProcessor::Nokogiri.encode_data("<smile>").to_s.should eql("<smile>") }

it { OpenSRS::XmlProcessor::Nokogiri.encode_data(12345).to_s.should eql("12345") }
it { OpenSRS::XmlProcessor::Nokogiri.encode_data(Date.parse("2010/02/12")).to_s.should eql("2010-02-12") }
it { OpenSRS::XmlProcessor::Nokogiri.encode_data(:name).to_s.should eql("name") }
it { OpenSRS::XmlProcessor::Nokogiri.encode_data(true).to_s.should eql("true") }
it { OpenSRS::XmlProcessor::Nokogiri.encode_data(false).to_s.should eql("false") }
end
end

Expand All @@ -107,7 +107,7 @@
</data_block>
</body>
</OPS_envelope>}
@response = OpenSRS::Nokogiri.parse(xml)
@response = OpenSRS::XmlProcessor::Nokogiri.parse(xml)
end

it { @response.should eql("Tom Jones") }
Expand Down Expand Up @@ -136,7 +136,7 @@
</body>
</OPS_envelope>}

@response = OpenSRS::Nokogiri.parse(xml)
@response = OpenSRS::XmlProcessor::Nokogiri.parse(xml)
end

it { @response["domain_list"].class.should eql(Array) }
Expand Down Expand Up @@ -177,7 +177,7 @@
</body>
</OPS_envelope>}

@response = OpenSRS::Nokogiri.parse(xml)
@response = OpenSRS::XmlProcessor::Nokogiri.parse(xml)
end
it { @response["contact_set"]["owner"]["first_name"].should eql("Tom") }
it { @response["contact_set"]["owner"]["last_name"].should eql("Jones") }
Expand Down Expand Up @@ -213,7 +213,7 @@
</body>
</OPS_envelope>}

@response = OpenSRS::Nokogiri.parse(xml)
@response = OpenSRS::XmlProcessor::Nokogiri.parse(xml)
end

it { @response.should be_an_instance_of(Hash) }
Expand Down
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
@@ -1,2 +1,3 @@
require 'opensrs/xml.rb'
require 'opensrs/nokogiri.rb'
require 'opensrs/xml_processor.rb'
require 'opensrs/xml_processor/libxml.rb'
require 'opensrs/xml_processor/nokogiri.rb'

0 comments on commit 5028fc1

Please sign in to comment.