opensrs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 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,18 @@
1
+ = OpenSRS
2
+
3
+ Provides basic support to connect to, and utilise the OpenSRS API. More information on the API can be located here:
4
+
5
+ http://opensrs.com/resources/documentation/subreseller_xmlapi.pdf
6
+
7
+ == Note on Patches/Pull Requests
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a
12
+ future version unintentionally.
13
+ * 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.
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2010 Josh Delsman. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "opensrs"
8
+ gem.summary = %Q{Provides support to utilise the OpenSRS API with Ruby/Rails.}
9
+ gem.description = %Q{Provides support to utilise the OpenSRS API with Ruby/Rails.}
10
+ gem.email = "jdelsman@voxxit.com"
11
+ gem.homepage = "http://github.com/voxxit/opensrs"
12
+ gem.authors = ["Josh Delsman"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+ task :test => :check_dependencies
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION')
46
+ version = File.read('VERSION')
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "opensrs #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/opensrs.rb ADDED
@@ -0,0 +1,6 @@
1
+ require File.dirname(__FILE__) + '/opensrs/server.rb'
2
+ require File.dirname(__FILE__) + '/opensrs/response.rb'
3
+ require File.dirname(__FILE__) + '/opensrs/xml.rb'
4
+
5
+ module OpenSRS
6
+ end
@@ -0,0 +1,20 @@
1
+ module OpenSRS
2
+ class Response
3
+ attr_accessor :response, :success
4
+
5
+ def initialize(response)
6
+ @response = response
7
+ @success = success?
8
+ end
9
+
10
+ # We need to return the error message unless the
11
+ # response is successful.
12
+ def errors
13
+ "#{response["response_text"]} (Code #{response["response_code"]})" unless success?
14
+ end
15
+
16
+ def success?
17
+ response["is_success"].to_s == "1"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,57 @@
1
+ require 'uri'
2
+ require 'net/https'
3
+ require 'digest/md5'
4
+ require 'openssl'
5
+
6
+ module OpenSRS
7
+ class Server
8
+ attr_accessor :server, :username, :password, :key
9
+
10
+ def initialize(options = {})
11
+ @server = URI.parse(options[:server] || "https://rr-n1-tor.opensrs.net:55443/")
12
+ @username = options[:username]
13
+ @password = options[:password]
14
+ @key = options[:key]
15
+ end
16
+
17
+ def call(options = {})
18
+ xml = OpenSRS::XML.build({
19
+ :protocol => "XCP",
20
+ :action => options[:action],
21
+ :object => options[:object],
22
+ :attributes => options[:attributes]
23
+ })
24
+
25
+ response = http.post(server.path, xml, headers(xml))
26
+ parsed_response = OpenSRS::XML.parse(response.body)
27
+
28
+ return OpenSRS::Response.new(parsed_response)
29
+ end
30
+
31
+ private
32
+
33
+ def headers(request)
34
+ headers = {
35
+ "Content-Length" => request.length.to_s,
36
+ "Content-Type" => "text/xml",
37
+ "X-Username" => username,
38
+ "X-Signature" => signature(request)
39
+ }
40
+
41
+ return headers
42
+ end
43
+
44
+ def signature(request)
45
+ signature = Digest::MD5.hexdigest(request + key)
46
+ signature = Digest::MD5.hexdigest(signature + key)
47
+ signature
48
+ end
49
+
50
+ def http
51
+ http = Net::HTTP.new(server.host, server.port)
52
+ http.use_ssl = (server.scheme == 'https')
53
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
54
+ http
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,138 @@
1
+ require 'rexml/document'
2
+
3
+ module OpenSRS
4
+ class XML
5
+ include REXML
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
+ data = encode(data) unless data.kind_of?(REXML::Element)
11
+
12
+ doc = Document.new
13
+ doc << XMLDecl.new("1.0", "UTF-8", "no")
14
+ doc << DocType.new("OPS_envelope", "SYSTEM 'ops.dtd'")
15
+
16
+ root = Element.new("OPS_envelope", doc)
17
+
18
+ Element.new("version", Element.new("header", root)).add_text("0.9")
19
+ Element.new("data_block", Element.new("body", root)).add(data)
20
+
21
+ return doc.to_s
22
+ end
23
+
24
+ # Encodes individual elements, and their child elements, for the root
25
+ # XML document.
26
+ def self.encode(data)
27
+ case data
28
+ when Array
29
+ element = Element.new("dt_array")
30
+
31
+ data.each_with_index do |v, j|
32
+ item = Element.new("item")
33
+ item.add_attribute("key", j.to_s)
34
+ item.add_element(encode(v))
35
+
36
+ element.add_element(item)
37
+ end
38
+
39
+ return element
40
+ when Hash
41
+ element = Element.new("dt_assoc")
42
+
43
+ data.each do |k, v|
44
+ item = Element.new("item")
45
+ item.add_attribute("key", k.to_s)
46
+ item.add_element(encode(v))
47
+
48
+ element.add_element(item)
49
+ end
50
+
51
+ return element
52
+ when String, Numeric, Date, Time, Symbol, NilClass
53
+ element = Element.new("dt_scalar")
54
+ element.text = data.to_s
55
+
56
+ return element
57
+ else
58
+ # Give up, probably wrong!
59
+ return encode(data.inspect)
60
+ end
61
+ end
62
+
63
+ # Parses the main data block from OpenSRS and discards
64
+ # the rest of the response.
65
+ def self.parse(response)
66
+ response.gsub!(/<!DOCTYPE OPS_envelope SYSTEM "ops.dtd">/, "")
67
+ response = Document.new(response) unless response.is_a?(REXML::Document)
68
+
69
+ data_block_children = XPath.first(response, "//OPS_envelope/body/data_block/*")
70
+
71
+ raise ArgumentError.new("No OPS_envelope found in document") unless data_block_children
72
+
73
+ return decode(data_block_children)
74
+ end
75
+
76
+ # Decodes individual data elements from OpenSRS response.
77
+ def self.decode(data)
78
+ if data.is_a?(String)
79
+ doc = Document.new(data)
80
+
81
+ if doc.children.length == 1
82
+ return decode(doc.children[0])
83
+ else
84
+ doc.children.map { |i| return decode(i) }
85
+ end
86
+ end
87
+
88
+ # The OpenSRS server's response (and the client docs) don't always
89
+ # encapsulate scalars in <dt_scalar> as you'd expect - so we have to
90
+ # infer that unexpected raw text is always a scalar.
91
+ return guess_scalar(data.value) if data.is_a?(REXML::Text)
92
+
93
+ case data.name
94
+ when "dt_array"
95
+ array = []
96
+
97
+ data.elements.each("item") do |item|
98
+ array[item.attributes["key"].to_i] = trim_and_decode(item.children)
99
+ end
100
+
101
+ return array
102
+ when "dt_assoc"
103
+ hash = {}
104
+
105
+ data.elements.each("item") do |item|
106
+ hash[item.attributes["key"]] = trim_and_decode(item.children)
107
+ end
108
+
109
+ return hash
110
+ when /^dt_scalar(ref)?/ then return guess_scalar(data.value)
111
+ end
112
+ end
113
+
114
+ protected
115
+
116
+ # Tries to correctly parse individual text into Date, Time, or Integers.
117
+ # If it fails, just displays the returned text.
118
+ def self.guess_scalar(text)
119
+ case text
120
+ when /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/ then Time.parse(text)
121
+ when /^\d\d\d\d-\d\d-\d\d$/ then Date.parse(text)
122
+ else
123
+ text
124
+ end
125
+ end
126
+
127
+ # Decodes lists, such as contact lists.
128
+ def self.trim_and_decode(list)
129
+ return "" if list.length.zero?
130
+ return decode(list[0]) if list.length == 1
131
+
132
+ first_non_text = list.select { |e| !e.kind_of?(REXML::Text) }[0]
133
+
134
+ return decode(first_non_text) unless first_non_text.nil?
135
+ return decode(list[0])
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class OpenSRSTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'opensrs'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opensrs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Delsman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-01 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Provides support to utilise the OpenSRS API with Ruby/Rails.
26
+ email: jdelsman@voxxit.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/opensrs.rb
42
+ - lib/opensrs/response.rb
43
+ - lib/opensrs/server.rb
44
+ - lib/opensrs/xml.rb
45
+ - test/opensrs_test.rb
46
+ - test/test_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/voxxit/opensrs
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Provides support to utilise the OpenSRS API with Ruby/Rails.
75
+ test_files:
76
+ - test/opensrs_test.rb
77
+ - test/test_helper.rb