glennr_opensrs 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +33 -0
- data/LICENSE +20 -0
- data/README.rdoc +97 -0
- data/Rakefile +40 -0
- data/lib/opensrs.rb +8 -0
- data/lib/opensrs/response.rb +29 -0
- data/lib/opensrs/server.rb +77 -0
- data/lib/opensrs/version.rb +5 -0
- data/lib/opensrs/xml_processor.rb +77 -0
- data/lib/opensrs/xml_processor/libxml.rb +60 -0
- data/lib/opensrs/xml_processor/nokogiri.rb +59 -0
- data/opensrs.gemspec +76 -0
- data/spec/opensrs/server_spec.rb +109 -0
- data/spec/opensrs/version_spec.rb +9 -0
- data/spec/opensrs/xml_processor/libxml_spec.rb +254 -0
- data/spec/opensrs/xml_processor/nokogiri_spec.rb +228 -0
- data/spec/spec_helper.rb +6 -0
- metadata +182 -0
data/.document
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
libxml-ruby (2.1.2)
|
11
|
+
nokogiri (1.4.7)
|
12
|
+
rake (0.9.2)
|
13
|
+
rspec (2.6.0)
|
14
|
+
rspec-core (~> 2.6.0)
|
15
|
+
rspec-expectations (~> 2.6.0)
|
16
|
+
rspec-mocks (~> 2.6.0)
|
17
|
+
rspec-core (2.6.4)
|
18
|
+
rspec-expectations (2.6.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.6.0)
|
21
|
+
shoulda (2.11.3)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
git
|
28
|
+
jeweler
|
29
|
+
libxml-ruby (~> 2.1.2)
|
30
|
+
nokogiri (~> 1.4.6)
|
31
|
+
rake
|
32
|
+
rspec (~> 2.0)
|
33
|
+
shoulda
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010-2011 Josh Delsman
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
= OpenSRS http://travis-ci.org/voxxit/opensrs.png
|
2
|
+
|
3
|
+
This (unofficial) OpenSRS gem provides basic support to connect to, and utilize the OpenSRS API. This library has been well-tested in high-performance production
|
4
|
+
environments. More information on the API can be located here:
|
5
|
+
|
6
|
+
http://www.opensrs.com/site/resources/documentation/api
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
You can install this gem by doing the following:
|
11
|
+
|
12
|
+
$ gem install opensrs
|
13
|
+
|
14
|
+
You can then include it in a Ruby project, like so:
|
15
|
+
|
16
|
+
require 'opensrs'
|
17
|
+
|
18
|
+
Alternatively, you can include it in a Rails 2.x project in the <tt>environment.rb</tt> file, like so:
|
19
|
+
|
20
|
+
config.gem "opensrs"
|
21
|
+
|
22
|
+
For Rails 3.x, use the <tt>Gemfile</tt>:
|
23
|
+
|
24
|
+
gem "opensrs"
|
25
|
+
|
26
|
+
== Usage
|
27
|
+
|
28
|
+
This library provides basic functionality for interacting with the OpenSRS XML API.
|
29
|
+
|
30
|
+
* Connection handling
|
31
|
+
* Error reporting
|
32
|
+
* XML encoding
|
33
|
+
* XML decoding
|
34
|
+
|
35
|
+
Currently, the library supports LibXML and Nokogiri as XML parsers. By default, it uses LibXML to parse and generate XML. If you'd like to use Nokogiri (1.4.7 and below) for parsing XML, then in one of your app initializers add the following line:
|
36
|
+
|
37
|
+
OpenSRS::Server.xml_processor = :nokogiri
|
38
|
+
|
39
|
+
To connect, instantiate a new <tt>OpenSRS::Server</tt> object:
|
40
|
+
|
41
|
+
server = OpenSRS::Server.new(
|
42
|
+
:server => "https://rr-n1-tor.opensrs.net:55443/",
|
43
|
+
:username => "testing",
|
44
|
+
:password => "53cr3t",
|
45
|
+
:key => "c633be3170c7fb3fb29e2f99b84be2410..."
|
46
|
+
)
|
47
|
+
|
48
|
+
NOTE: Connecting to OpenSRS requires that you add the IP(s) you're connecting from to their whitelist. Login to the testing or production servers, and add your IP(s) under Profile Management > Add IPs for Script/API Access. IP changes take about one hour to take effect.
|
49
|
+
|
50
|
+
Once you have a server connection class, you can build from this to create the methods that you need. For instance, let's say we want to grab our account balance. The OpenSRS XML API takes a couple of attributes for all commands. You can include those here:
|
51
|
+
|
52
|
+
def get_balance
|
53
|
+
server.call(
|
54
|
+
:action => "GET_BALANCE",
|
55
|
+
:object => "BALANCE"
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
Sometimes you might need to include attributes for the command, such as a cookie, or the data attributes themselves. You can do this, too:
|
60
|
+
|
61
|
+
def create_nameserver(nameserver)
|
62
|
+
server.call(
|
63
|
+
:action => "CREATE",
|
64
|
+
:object => "NAMESERVER",
|
65
|
+
:cookie => "366828736:3210384",
|
66
|
+
:attributes => {
|
67
|
+
:name => nameserver.hostname,
|
68
|
+
:ipaddress => "212.112.123.11"
|
69
|
+
}
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
Responses from OpenSRS are returned in an OpenSRS::Response object, which gives you access to a multitude of things.
|
74
|
+
|
75
|
+
* <tt>response.response</tt> - This gives you the response in a Hash form, which is highly accessible to most other actions in your application.
|
76
|
+
* <tt>response.errors</tt> - If there are errors which come back from OpenSRS, they are returned here. If not, it returns nil.
|
77
|
+
* <tt>response.success?</tt> - Returns true if the response was labeled as successful. If not, it returns false.
|
78
|
+
* <tt>response.request_xml</tt> - Returns raw request XML.
|
79
|
+
* <tt>response.response_xml</tt> - Returns raw response XML.
|
80
|
+
|
81
|
+
== Bugs/Feature Requests
|
82
|
+
|
83
|
+
If you have any bugs or feature requests for this gem, feel free to add them in the Issues portion of the GitHub repository here:
|
84
|
+
|
85
|
+
http://github.com/voxxit/opensrs/issues
|
86
|
+
|
87
|
+
== Note on Patches/Pull Requests
|
88
|
+
|
89
|
+
* Fork the project.
|
90
|
+
* Make your feature addition or bug fix.
|
91
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
92
|
+
* Commit, do not mess with rakefile, version, or history. If you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull.
|
93
|
+
* Send me a pull request. Bonus points for topic branches.
|
94
|
+
|
95
|
+
== Copyright
|
96
|
+
|
97
|
+
Copyright (c) 2010-2012 Josh Delsman. Distributed under the MIT license. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift("lib")
|
13
|
+
|
14
|
+
require 'rake'
|
15
|
+
require 'opensrs'
|
16
|
+
|
17
|
+
begin
|
18
|
+
require 'jeweler'
|
19
|
+
|
20
|
+
Jeweler::Tasks.new do |gem|
|
21
|
+
gem.name = "glennr_opensrs"
|
22
|
+
gem.version = OpenSRS::Version::VERSION
|
23
|
+
gem.summary = "Provides support to utilize the OpenSRS API with Ruby/Rails."
|
24
|
+
gem.description = "Provides support to utilize the OpenSRS API with Ruby/Rails."
|
25
|
+
gem.email = "glenn@siyelo.com"
|
26
|
+
gem.homepage = "http://github.com/glennr/opensrs"
|
27
|
+
gem.license = "MIT"
|
28
|
+
gem.authors = ["Josh Delsman", "Glenn Roberts"]
|
29
|
+
|
30
|
+
# Requirements are in Gemfile
|
31
|
+
end
|
32
|
+
rescue LoadError
|
33
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'rspec/core/rake_task'
|
37
|
+
|
38
|
+
RSpec::Core::RakeTask.new(:spec)
|
39
|
+
|
40
|
+
task :default => :spec
|
data/lib/opensrs.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/opensrs/xml_processor'
|
2
|
+
require File.dirname(__FILE__) + '/opensrs/xml_processor/libxml.rb'
|
3
|
+
require File.dirname(__FILE__) + '/opensrs/server.rb'
|
4
|
+
require File.dirname(__FILE__) + '/opensrs/response.rb'
|
5
|
+
require File.dirname(__FILE__) + '/opensrs/version.rb'
|
6
|
+
|
7
|
+
module OpenSRS
|
8
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module OpenSRS
|
2
|
+
class Response
|
3
|
+
attr_reader :request_xml, :response_xml
|
4
|
+
attr_accessor :response, :success
|
5
|
+
|
6
|
+
def initialize(parsed_response, request_xml, response_xml)
|
7
|
+
@response = parsed_response
|
8
|
+
@request_xml = request_xml
|
9
|
+
@response_xml = response_xml
|
10
|
+
@success = success?
|
11
|
+
end
|
12
|
+
|
13
|
+
# We need to return the error message unless the
|
14
|
+
# response is successful.
|
15
|
+
def errors
|
16
|
+
if !success?
|
17
|
+
if response["response_text"] and response["response_code"]
|
18
|
+
"#{response["response_text"]} (Code #{response["response_code"]})"
|
19
|
+
else
|
20
|
+
"Unknown error"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def success?
|
26
|
+
response["is_success"] ? response["is_success"].to_s == "1" : true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "net/https"
|
3
|
+
require "digest/md5"
|
4
|
+
require "openssl"
|
5
|
+
|
6
|
+
module OpenSRS
|
7
|
+
class BadResponse < StandardError; end
|
8
|
+
class TimeoutError < StandardError; end
|
9
|
+
|
10
|
+
class Server
|
11
|
+
attr_accessor :server, :username, :password, :key, :timeout, :open_timeout
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
@server = URI.parse(options[:server] || "https://rr-n1-tor.opensrs.net:55443/")
|
15
|
+
@username = options[:username]
|
16
|
+
@password = options[:password]
|
17
|
+
@key = options[:key]
|
18
|
+
@timeout = options[:timeout]
|
19
|
+
@open_timeout = options[:open_timeout]
|
20
|
+
end
|
21
|
+
|
22
|
+
def call(data = {})
|
23
|
+
xml = xml_processor.build({ :protocol => "XCP" }.merge!(data))
|
24
|
+
|
25
|
+
begin
|
26
|
+
response = http.post(server_path, xml, headers(xml))
|
27
|
+
rescue Net::HTTPBadResponse
|
28
|
+
raise OpenSRS::BadResponse, "Received a bad response from OpenSRS. Please check that your IP address is added to the whitelist, and try again."
|
29
|
+
end
|
30
|
+
|
31
|
+
parsed_response = xml_processor.parse(response.body)
|
32
|
+
return OpenSRS::Response.new(parsed_response, xml, response.body)
|
33
|
+
rescue Timeout::Error => err
|
34
|
+
raise OpenSRS::TimeoutError, err
|
35
|
+
end
|
36
|
+
|
37
|
+
def xml_processor
|
38
|
+
@@xml_processor
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.xml_processor=(name)
|
42
|
+
require File.dirname(__FILE__) + "/xml_processor/#{name.to_s.downcase}"
|
43
|
+
@@xml_processor = OpenSRS::XmlProcessor.const_get("#{name.to_s.capitalize}")
|
44
|
+
end
|
45
|
+
|
46
|
+
OpenSRS::Server.xml_processor = :libxml
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def headers(request)
|
51
|
+
{ "Content-Length" => request.length.to_s,
|
52
|
+
"Content-Type" => "text/xml",
|
53
|
+
"X-Username" => username,
|
54
|
+
"X-Signature" => signature(request)
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def signature(request)
|
59
|
+
signature = Digest::MD5.hexdigest(request + key)
|
60
|
+
signature = Digest::MD5.hexdigest(signature + key)
|
61
|
+
signature
|
62
|
+
end
|
63
|
+
|
64
|
+
def http
|
65
|
+
http = Net::HTTP.new(server.host, server.port)
|
66
|
+
http.use_ssl = (server.scheme == "https")
|
67
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
68
|
+
http.read_timeout = http.open_timeout = @timeout if @timeout
|
69
|
+
http.open_timeout = @open_timeout if @open_timeout
|
70
|
+
http
|
71
|
+
end
|
72
|
+
|
73
|
+
def server_path
|
74
|
+
server.path.empty? ? '/' : server.path
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module OpenSRS
|
2
|
+
|
3
|
+
class XmlProcessor
|
4
|
+
|
5
|
+
# Parses the main data block from OpenSRS and discards
|
6
|
+
# the rest of the response.
|
7
|
+
def self.parse(response)
|
8
|
+
data_block = data_block_element(response)
|
9
|
+
|
10
|
+
raise ArgumentError.new("No data found in document") if !data_block
|
11
|
+
|
12
|
+
return decode_data(data_block)
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
# Encodes individual elements, and their child elements, for the root XML document.
|
18
|
+
def self.encode_data(data, container = nil)
|
19
|
+
case data.class.to_s
|
20
|
+
when "Array" then return encode_dt_array(data, container)
|
21
|
+
when "Hash" then return encode_dt_assoc(data, container)
|
22
|
+
when "String", "Numeric", "Date", "Time", "Symbol", "NilClass"
|
23
|
+
return data.to_s
|
24
|
+
else
|
25
|
+
return data.inspect
|
26
|
+
end
|
27
|
+
|
28
|
+
return nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.encode_dt_array(data, container)
|
32
|
+
dt_array = new_element(:dt_array, container)
|
33
|
+
|
34
|
+
data.each_with_index do |item, index|
|
35
|
+
item_node = new_element(:item, container)
|
36
|
+
item_node["key"] = index.to_s
|
37
|
+
item_node << encode_data(item, item_node)
|
38
|
+
|
39
|
+
dt_array << item_node
|
40
|
+
end
|
41
|
+
|
42
|
+
return dt_array
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.encode_dt_assoc(data, container)
|
46
|
+
dt_assoc = new_element(:dt_assoc, container)
|
47
|
+
|
48
|
+
data.each do |key, value|
|
49
|
+
item_node = new_element(:item, container)
|
50
|
+
item_node["key"] = key.to_s
|
51
|
+
item_node << encode_data(value, item_node)
|
52
|
+
|
53
|
+
dt_assoc << item_node
|
54
|
+
end
|
55
|
+
|
56
|
+
return dt_assoc
|
57
|
+
end
|
58
|
+
|
59
|
+
# Recursively decodes individual data elements from OpenSRS
|
60
|
+
# server response.
|
61
|
+
def self.decode_data(data)
|
62
|
+
data.each do |element|
|
63
|
+
case element.name
|
64
|
+
when "dt_array"
|
65
|
+
return decode_dt_array_data(element)
|
66
|
+
when "dt_assoc"
|
67
|
+
return decode_dt_assoc_data(element)
|
68
|
+
when "text", "item", "dt_scalar"
|
69
|
+
next if element.content.strip.empty?
|
70
|
+
return element.content.strip
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require "libxml"
|
2
|
+
|
3
|
+
module OpenSRS
|
4
|
+
class XmlProcessor::Libxml < OpenSRS::XmlProcessor
|
5
|
+
include ::LibXML::XML
|
6
|
+
|
7
|
+
# First, builds REXML elements for the inputted data. Then, it will
|
8
|
+
# go ahead and build the entire XML document to send to OpenSRS.
|
9
|
+
def self.build(data)
|
10
|
+
xml = Document.new
|
11
|
+
xml.root = envelope = Node.new("OPS_envelope")
|
12
|
+
|
13
|
+
envelope << header = Node.new("header")
|
14
|
+
envelope << body = Node.new("body")
|
15
|
+
header << Node.new("version", "0.9")
|
16
|
+
body << data_block = Node.new("data_block")
|
17
|
+
|
18
|
+
data_block << encode_data(data, data_block)
|
19
|
+
|
20
|
+
return xml.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def self.data_block_element(response)
|
26
|
+
doc = Parser.string(response).parse
|
27
|
+
return doc.find("//OPS_envelope/body/data_block/*")
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.decode_dt_array_data(element)
|
31
|
+
dt_array = []
|
32
|
+
|
33
|
+
element.children.each do |item|
|
34
|
+
next if item.empty?
|
35
|
+
dt_array[item.attributes["key"].to_i] = decode_data(item)
|
36
|
+
end
|
37
|
+
|
38
|
+
return dt_array
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.decode_dt_assoc_data(element)
|
42
|
+
dt_assoc = {}
|
43
|
+
|
44
|
+
element.children.each do |item|
|
45
|
+
next if item.content.strip.empty?
|
46
|
+
dt_assoc[item.attributes["key"]] = decode_data(item)
|
47
|
+
end
|
48
|
+
|
49
|
+
return dt_assoc
|
50
|
+
end
|
51
|
+
|
52
|
+
# Accepts two parameters but uses only one; to keep the interface same as other xml parser classes
|
53
|
+
# Is that a side effect of Template pattern?
|
54
|
+
#
|
55
|
+
def self.new_element(element_name, container)
|
56
|
+
return Node.new(element_name.to_s)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|