opensrs 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ begin
2
+ require 'nokogiri'
3
+ rescue LoadError => e
4
+ $stderr.puts "Cannot find Nokogiri gem. Please install Nokogiri before using it as the xml processor\n\n"
5
+ raise e
6
+ end
7
+
8
+ module OpenSRS
9
+ class XmlProcessor::Nokogiri < OpenSRS::XmlProcessor
10
+
11
+ def self.build(data)
12
+ builder = ::Nokogiri::XML::Builder.new
13
+
14
+ envelope = ::Nokogiri::XML::Node.new("OPS_envelope", builder.doc)
15
+ header = ::Nokogiri::XML::Node.new("header", builder.doc)
16
+ version = ::Nokogiri::XML::Node.new("version", builder.doc)
17
+ body = ::Nokogiri::XML::Node.new("body", builder.doc)
18
+ data_block = ::Nokogiri::XML::Node.new("data_block", builder.doc)
19
+ other_data = encode_data(data, builder.doc)
20
+ builder.doc << envelope << header << version << '0.9'
21
+ envelope << body << data_block << other_data
22
+ return builder.to_xml
23
+ end
24
+
25
+ protected
26
+
27
+ def self.data_block_element(response)
28
+ doc = ::Nokogiri::XML(response)
29
+ return doc.xpath('//OPS_envelope/body/data_block/*')
30
+ end
31
+
32
+ def self.decode_dt_array_data(element)
33
+ dt_array = []
34
+
35
+ element.children.each do |item|
36
+ next if item.content.strip.empty?
37
+ dt_array[item.attributes["key"].value.to_i] = decode_data(item.children)
38
+ end
39
+
40
+ return dt_array
41
+ end
42
+
43
+ def self.decode_dt_assoc_data(element)
44
+ dt_assoc = {}
45
+
46
+ element.children.each do |item|
47
+ next if item.content.strip.empty?
48
+ dt_assoc[item.attributes["key"].value] = decode_data(item.children)
49
+ end
50
+
51
+ return dt_assoc
52
+ end
53
+
54
+ def self.new_element(element_name, container)
55
+ return ::Nokogiri::XML::Node.new(element_name.to_s, container)
56
+ end
57
+
58
+ end
59
+ end
@@ -5,56 +5,77 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{opensrs}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Josh Delsman"]
12
- s.date = %q{2011-01-31}
13
- s.description = %q{Provides support to utilise the OpenSRS API with Ruby/Rails.}
14
- s.email = %q{josh@voxx.it}
12
+ s.date = %q{2011-05-09}
13
+ s.description = %q{Provides support to utilize the OpenSRS API with Ruby/Rails.}
14
+ s.email = %q{josh@voxxit.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
21
23
  "LICENSE",
22
24
  "README.rdoc",
23
25
  "Rakefile",
24
- "VERSION",
25
26
  "lib/opensrs.rb",
26
27
  "lib/opensrs/response.rb",
27
28
  "lib/opensrs/server.rb",
28
- "lib/opensrs/xml.rb",
29
+ "lib/opensrs/version.rb",
30
+ "lib/opensrs/xml_processor.rb",
31
+ "lib/opensrs/xml_processor/libxml.rb",
32
+ "lib/opensrs/xml_processor/nokogiri.rb",
29
33
  "opensrs.gemspec",
30
- "spec/opensrs/xml_spec.rb",
31
- "spec/spec_helper.rb",
32
- "test/opensrs_test.rb",
33
- "test/test_helper.rb"
34
+ "spec/opensrs/server_spec.rb",
35
+ "spec/opensrs/version_spec.rb",
36
+ "spec/opensrs/xml_processor/libxml_spec.rb",
37
+ "spec/opensrs/xml_processor/nokogiri_spec.rb",
38
+ "spec/spec_helper.rb"
34
39
  ]
35
40
  s.homepage = %q{http://github.com/voxxit/opensrs}
41
+ s.licenses = ["MIT"]
36
42
  s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.4.2}
38
- s.summary = %q{Provides support to utilise the OpenSRS API with Ruby/Rails.}
39
- s.test_files = [
40
- "spec/opensrs/xml_spec.rb",
41
- "spec/spec_helper.rb",
42
- "test/opensrs_test.rb",
43
- "test/test_helper.rb"
44
- ]
43
+ s.rubygems_version = %q{1.6.2}
44
+ s.summary = %q{Provides support to utilize the OpenSRS API with Ruby/Rails.}
45
45
 
46
46
  if s.respond_to? :specification_version then
47
47
  s.specification_version = 3
48
48
 
49
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
- s.add_runtime_dependency(%q<libxml-ruby>, [">= 0"])
50
+ s.add_runtime_dependency(%q<libxml-ruby>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<nokogiri>, [">= 0"])
52
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
53
+ s.add_development_dependency(%q<git>, [">= 0"])
54
+ s.add_development_dependency(%q<rake>, [">= 0"])
55
+ s.add_development_dependency(%q<bundler>, [">= 0"])
56
+ s.add_development_dependency(%q<yard>, [">= 0"])
57
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
51
58
  s.add_development_dependency(%q<rspec>, ["~> 2.0"])
52
59
  else
53
- s.add_dependency(%q<libxml-ruby>, [">= 0"])
60
+ s.add_dependency(%q<libxml-ruby>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<nokogiri>, [">= 0"])
62
+ s.add_dependency(%q<jeweler>, [">= 0"])
63
+ s.add_dependency(%q<git>, [">= 0"])
64
+ s.add_dependency(%q<rake>, [">= 0"])
65
+ s.add_dependency(%q<bundler>, [">= 0"])
66
+ s.add_dependency(%q<yard>, [">= 0"])
67
+ s.add_dependency(%q<shoulda>, [">= 0"])
54
68
  s.add_dependency(%q<rspec>, ["~> 2.0"])
55
69
  end
56
70
  else
57
- s.add_dependency(%q<libxml-ruby>, [">= 0"])
71
+ s.add_dependency(%q<libxml-ruby>, ["~> 1.0.0"])
72
+ s.add_dependency(%q<nokogiri>, [">= 0"])
73
+ s.add_dependency(%q<jeweler>, [">= 0"])
74
+ s.add_dependency(%q<git>, [">= 0"])
75
+ s.add_dependency(%q<rake>, [">= 0"])
76
+ s.add_dependency(%q<bundler>, [">= 0"])
77
+ s.add_dependency(%q<yard>, [">= 0"])
78
+ s.add_dependency(%q<shoulda>, [">= 0"])
58
79
  s.add_dependency(%q<rspec>, ["~> 2.0"])
59
80
  end
60
81
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenSRS::Server do
4
+ before(:each) do
5
+ @server = OpenSRS::Server.new
6
+ end
7
+
8
+ describe "#test xml processor" do
9
+ context "on class initialization" do
10
+ it { @server.xml_processor.should eql(OpenSRS::XmlProcessor::Libxml) }
11
+ end
12
+
13
+ context "on changing xml processor" do
14
+ before(:each) do
15
+ OpenSRS::Server.xml_processor = :nokogiri
16
+ end
17
+
18
+ it { @server.xml_processor.should eql(OpenSRS::XmlProcessor::Nokogiri) }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenSRS::Version do
4
+ describe "VERSION" do
5
+ it "should return version string" do
6
+ OpenSRS::Version::VERSION.should eql("0.3.0")
7
+ end
8
+ end
9
+ end
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
  require 'date'
3
3
 
4
- describe OpenSRS::XML do
4
+ describe OpenSRS::XmlProcessor::Libxml do
5
5
  describe ".build" do
6
6
  it "should create XML for a nested hash" do
7
7
  attributes = {:foo => {:bar => 'baz'}}
8
- xml = OpenSRS::XML.build(attributes)
8
+ xml = OpenSRS::XmlProcessor::Libxml.build(attributes)
9
9
  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}
10
10
  end
11
11
  end
@@ -13,7 +13,7 @@ describe OpenSRS::XML do
13
13
  describe '.encode_data' do
14
14
  context "on a 3 element array" do
15
15
  before(:each) do
16
- @e = OpenSRS::XML.encode_data([1,2,3])
16
+ @e = OpenSRS::XmlProcessor::Libxml.encode_data([1,2,3])
17
17
  end
18
18
 
19
19
  it "is a REXML::Element" do
@@ -40,7 +40,7 @@ describe OpenSRS::XML do
40
40
 
41
41
  context "on a hash" do
42
42
  before(:each) do
43
- @e = OpenSRS::XML.encode_data({:name => "kitteh"})
43
+ @e = OpenSRS::XmlProcessor::Libxml.encode_data({:name => "kitteh"})
44
44
  end
45
45
 
46
46
  it "is a REXML::Element" do
@@ -60,7 +60,7 @@ describe OpenSRS::XML do
60
60
 
61
61
  context "on a nested hash" do
62
62
  before(:each) do
63
- @e = OpenSRS::XML.encode_data({:suggestion => {:maximum => "10"}})
63
+ @e = OpenSRS::XmlProcessor::Libxml.encode_data({:suggestion => {:maximum => "10"}})
64
64
  end
65
65
 
66
66
  it "is a REXML::Element" do
@@ -90,29 +90,29 @@ describe OpenSRS::XML do
90
90
 
91
91
  context "produces a scalar" do
92
92
  it "from a string" do
93
- OpenSRS::XML.encode_data("cheezburger").to_s.should == "cheezburger"
93
+ OpenSRS::XmlProcessor::Libxml.encode_data("cheezburger").to_s.should == "cheezburger"
94
94
  end
95
95
 
96
96
  it "from a string with XML characters" do
97
- OpenSRS::XML.encode_data("<smile>").to_s.should == "<smile>"
97
+ OpenSRS::XmlProcessor::Libxml.encode_data("<smile>").to_s.should == "<smile>"
98
98
  end
99
99
 
100
100
  it "from an integer" do
101
- OpenSRS::XML.encode_data(12345).to_s.should == "12345"
101
+ OpenSRS::XmlProcessor::Libxml.encode_data(12345).to_s.should == "12345"
102
102
  end
103
103
 
104
104
  it "from a date" do
105
105
  date = Date.parse("2010/02/12")
106
- OpenSRS::XML.encode_data(date).to_s.should == "2010-02-12"
106
+ OpenSRS::XmlProcessor::Libxml.encode_data(date).to_s.should == "2010-02-12"
107
107
  end
108
108
 
109
109
  it "from a symbol" do
110
- OpenSRS::XML.encode_data(:name).to_s.should == "name"
110
+ OpenSRS::XmlProcessor::Libxml.encode_data(:name).to_s.should == "name"
111
111
  end
112
112
 
113
113
  it "from true or false" do
114
- OpenSRS::XML.encode_data(true).to_s.should == "true"
115
- OpenSRS::XML.encode_data(false).to_s.should == "false"
114
+ OpenSRS::XmlProcessor::Libxml.encode_data(true).to_s.should == "true"
115
+ OpenSRS::XmlProcessor::Libxml.encode_data(false).to_s.should == "false"
116
116
  end
117
117
  end
118
118
  end
@@ -132,7 +132,7 @@ describe OpenSRS::XML do
132
132
  </body>
133
133
  </OPS_envelope>}
134
134
 
135
- resp = OpenSRS::XML.parse(xml)
135
+ resp = OpenSRS::XmlProcessor::Libxml.parse(xml)
136
136
  resp.should == "Tom Jones"
137
137
  end
138
138
 
@@ -158,7 +158,7 @@ describe OpenSRS::XML do
158
158
  </body>
159
159
  </OPS_envelope>}
160
160
 
161
- resp = OpenSRS::XML.parse(xml)
161
+ resp = OpenSRS::XmlProcessor::Libxml.parse(xml)
162
162
  resp["domain_list"].class.should == Array
163
163
  resp["domain_list"][0].should == "ns1.example.com"
164
164
  resp["domain_list"][1].should == "ns2.example.com"
@@ -196,7 +196,7 @@ describe OpenSRS::XML do
196
196
  </body>
197
197
  </OPS_envelope>}
198
198
 
199
- resp = OpenSRS::XML.parse(xml)
199
+ resp = OpenSRS::XmlProcessor::Libxml.parse(xml)
200
200
 
201
201
  resp["contact_set"]["owner"]["first_name"].should == "Tom"
202
202
  resp["contact_set"]["owner"]["last_name"].should == "Jones"
@@ -232,7 +232,7 @@ describe OpenSRS::XML do
232
232
  </body>
233
233
  </OPS_envelope>}
234
234
 
235
- @resp = OpenSRS::XML.parse(xml)
235
+ @resp = OpenSRS::XmlProcessor::Libxml.parse(xml)
236
236
  end
237
237
 
238
238
  it "produces a hash" do
@@ -0,0 +1,228 @@
1
+ require 'spec_helper'
2
+ require 'date'
3
+
4
+ describe OpenSRS::XmlProcessor::Nokogiri do
5
+ describe ".build" do
6
+ it "should create XML for a nested hash" do
7
+ attributes = {:foo => {:bar => 'baz'}}
8
+ xml = OpenSRS::XmlProcessor::Nokogiri.build(attributes)
9
+ 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}
10
+ end
11
+ end
12
+
13
+ describe '.encode_data' do
14
+
15
+ before(:each) do
16
+ @builder = ::Nokogiri::XML::Builder.new
17
+ @doc = @builder.doc
18
+ end
19
+
20
+ context "on a 3 element array" do
21
+ before(:each) do
22
+ @e = OpenSRS::XmlProcessor::Nokogiri.encode_data([1,2,3], @doc)
23
+ end
24
+
25
+ it { @e.should be_an_instance_of(::Nokogiri::XML::Element) }
26
+ it { @e.name.should eql('dt_array') }
27
+
28
+ it { @e.should have(3).children }
29
+ it { @e.children[0].name.should eql("item") }
30
+ it { @e.children[1].name.should eql("item") }
31
+ it { @e.children[2].name.should eql("item") }
32
+
33
+ it { @e.children[0].attributes["key"].value.should eql("0") }
34
+ it { @e.children[1].attributes["key"].value.should eql("1") }
35
+ it { @e.children[2].attributes["key"].value.should eql("2") }
36
+ end
37
+
38
+ context "on a hash" do
39
+ before(:each) do
40
+ @e = OpenSRS::XmlProcessor::Nokogiri.encode_data({:name => "kitteh"}, @doc)
41
+ end
42
+
43
+ it { @e.should be_an_instance_of(::Nokogiri::XML::Element) }
44
+ it { @e.name.should eql('dt_assoc') }
45
+
46
+ it { @e.should have(1).children }
47
+ it { @e.children[0].name.should eql('item') }
48
+ it { @e.children[0].attributes["key"].value.should eql('name') }
49
+ end
50
+
51
+ context "on a nested hash" do
52
+ before(:each) do
53
+ @e = OpenSRS::XmlProcessor::Nokogiri.encode_data({:suggestion => {:maximum => "10"}}, @doc)
54
+ @suggestion = @e.children[0]
55
+ @dt_assoc = @suggestion.children[0]
56
+ end
57
+
58
+ it { @e.should be_an_instance_of(::Nokogiri::XML::Element) }
59
+ it { @e.name.should == 'dt_assoc' }
60
+
61
+ context "<item> child" do
62
+ it { @e.should have(1).children }
63
+ it { @suggestion.name.should eql('item') }
64
+ it { @suggestion.attributes["key"].value.should eql('suggestion') }
65
+ end
66
+
67
+ context "suggesion children" do
68
+ it { @suggestion.should have(1).children }
69
+ it { @dt_assoc.name.should eql('dt_assoc') }
70
+ end
71
+
72
+ context "dt_assoc children" do
73
+ before(:each) do
74
+ @maximum = @dt_assoc.children[0]
75
+ end
76
+ it { @dt_assoc.should have(1).children }
77
+ it { @maximum.name.should eql('item') }
78
+ it { @maximum.attributes["key"].value.should eql('maximum') }
79
+ end
80
+ end
81
+
82
+ context "produces a scalar" do
83
+ it { OpenSRS::XmlProcessor::Nokogiri.encode_data("cheezburger").to_s.should eql("cheezburger") }
84
+ it { OpenSRS::XmlProcessor::Nokogiri.encode_data("<smile>").to_s.should eql("<smile>") }
85
+
86
+ it { OpenSRS::XmlProcessor::Nokogiri.encode_data(12345).to_s.should eql("12345") }
87
+ it { OpenSRS::XmlProcessor::Nokogiri.encode_data(Date.parse("2010/02/12")).to_s.should eql("2010-02-12") }
88
+ it { OpenSRS::XmlProcessor::Nokogiri.encode_data(:name).to_s.should eql("name") }
89
+ it { OpenSRS::XmlProcessor::Nokogiri.encode_data(true).to_s.should eql("true") }
90
+ it { OpenSRS::XmlProcessor::Nokogiri.encode_data(false).to_s.should eql("false") }
91
+ end
92
+ end
93
+
94
+ describe '.parse' do
95
+
96
+ context "when scaler values" do
97
+ before(:each) do
98
+ xml = %{<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
99
+ <!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
100
+ <OPS_envelope>
101
+ <header>
102
+ <version>1.0</version>
103
+ </header>
104
+ <body>
105
+ <data_block>
106
+ <dt_scalar>Tom Jones</dt_scalar>
107
+ </data_block>
108
+ </body>
109
+ </OPS_envelope>}
110
+ @response = OpenSRS::XmlProcessor::Nokogiri.parse(xml)
111
+ end
112
+
113
+ it { @response.should eql("Tom Jones") }
114
+ end
115
+
116
+ context "when associative arrays with arrays of values" do
117
+ before(:each) do
118
+ xml = %{<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
119
+ <!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
120
+ <OPS_envelope>
121
+ <header>
122
+ <version>1.0</version>
123
+ </header>
124
+ <body>
125
+ <data_block>
126
+ <dt_assoc>
127
+ <item key='domain_list'>
128
+ <dt_array>
129
+ <item key='0'>ns1.example.com</item>
130
+ <item key='1'>ns2.example.com</item>
131
+ <item key='2'>ns3.example.com</item>
132
+ </dt_array>
133
+ </item>
134
+ </dt_assoc>
135
+ </data_block>
136
+ </body>
137
+ </OPS_envelope>}
138
+
139
+ @response = OpenSRS::XmlProcessor::Nokogiri.parse(xml)
140
+ end
141
+
142
+ it { @response["domain_list"].class.should eql(Array) }
143
+ it { @response["domain_list"][0].should eql("ns1.example.com") }
144
+ it { @response["domain_list"][1].should eql("ns2.example.com") }
145
+ it { @response["domain_list"][2].should eql("ns3.example.com") }
146
+ end
147
+
148
+ context "when associative arrays containing other associative arrays" do
149
+ before(:each) do
150
+ xml = %{<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
151
+ <!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
152
+ <OPS_envelope>
153
+ <header>
154
+ <version>1.0</version>
155
+ </header>
156
+ <body>
157
+ <data_block>
158
+ <dt_assoc>
159
+ <item key="contact_set">
160
+ <dt_assoc>
161
+ <item key='owner'>
162
+ <dt_assoc>
163
+ <item key='first_name'>Tom</item>
164
+ <item key='last_name'>Jones</item>
165
+ </dt_assoc>
166
+ </item>
167
+ <item key='tech'>
168
+ <dt_assoc>
169
+ <item key='first_name'>Anne</item>
170
+ <item key='last_name'>Smith</item>
171
+ </dt_assoc>
172
+ </item>
173
+ </dt_assoc>
174
+ </item>
175
+ </dt_assoc>
176
+ </data_block>
177
+ </body>
178
+ </OPS_envelope>}
179
+
180
+ @response = OpenSRS::XmlProcessor::Nokogiri.parse(xml)
181
+ end
182
+ it { @response["contact_set"]["owner"]["first_name"].should eql("Tom") }
183
+ it { @response["contact_set"]["owner"]["last_name"].should eql("Jones") }
184
+ it { @response["contact_set"]["tech"]["first_name"].should eql("Anne") }
185
+ it { @response["contact_set"]["tech"]["last_name"].should eql("Smith") }
186
+ end
187
+
188
+ context "with a balance enquiry example response" do
189
+ before(:each) do
190
+ xml = %{<?xml version='1.0' encoding='UTF-8' standalone='no' ?>
191
+ <!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
192
+ <OPS_envelope>
193
+ <header>
194
+ <version>0.9</version>
195
+ </header>
196
+ <body>
197
+ <data_block>
198
+ <dt_assoc>
199
+ <item key="protocol">XCP</item>
200
+ <item key="action">REPLY</item>
201
+ <item key="object">BALANCE</item>
202
+ <item key="is_success">1</item>
203
+ <item key="response_code">200</item>
204
+ <item key="response_text">Command successful</item>
205
+ <item key="attributes">
206
+ <dt_assoc>
207
+ <item key="balance">8549.18</item>
208
+ <item key="hold_balance">1676.05</item>
209
+ </dt_assoc>
210
+ </item>
211
+ </dt_assoc>
212
+ </data_block>
213
+ </body>
214
+ </OPS_envelope>}
215
+
216
+ @response = OpenSRS::XmlProcessor::Nokogiri.parse(xml)
217
+ end
218
+
219
+ it { @response.should be_an_instance_of(Hash) }
220
+ it { @response["protocol"].should eql("XCP") }
221
+ it { @response["action"].should eql("REPLY") }
222
+ it { @response["object"].should eql("BALANCE") }
223
+ it { @response["attributes"]["balance"].should eql("8549.18") }
224
+ it { @response["attributes"]["hold_balance"].should eql("1676.05") }
225
+
226
+ end
227
+ end
228
+ end