Skip to content
Browse files

Initial commit. Working version.

  • Loading branch information...
0 parents commit d073555f95f7014cac6480c92d9f6eceb75ffc90 Joost Hietbrink committed
Showing with 187 additions and 0 deletions.
  1. +6 −0 Gemfile
  2. +27 −0 Gemfile.lock
  3. +20 −0 MIT-LICENSE
  4. +39 −0 README.rdoc
  5. +95 −0 transip.rb
6 Gemfile
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+source 'http://gemcutter.org'
+source 'http://gems.github.com'
+
+gem 'savon'
+gem 'curb' # For Savon, see https://github.com/rubiii/savon/issues/167
27 Gemfile.lock
@@ -0,0 +1,27 @@
+GEM
+ remote: http://rubygems.org/
+ remote: http://gemcutter.org/
+ remote: http://gems.github.com/
+ specs:
+ builder (3.0.0)
+ crack (0.1.8)
+ curb (0.7.15)
+ gyoku (0.4.2)
+ builder (>= 2.1.2)
+ httpi (0.9.2)
+ ntlm-http (>= 0.1.1)
+ rack
+ ntlm-http (0.1.1)
+ rack (1.2.2)
+ savon (0.9.1)
+ builder (>= 2.1.2)
+ crack (~> 0.1.8)
+ gyoku (>= 0.4.0)
+ httpi (>= 0.7.8)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ curb
+ savon
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Joost Hietbrink
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 README.rdoc
@@ -0,0 +1,39 @@
+= TransIP API
+
+Ruby lib to use the full TransIP API (v2).
+
+For more info see:
+
+* <b>The origin of this code:</b> https://github.com/joost/transip-api
+* <b>TransIP API Docs:</b> https://www.transip.nl/g/api/
+
+Credits:
+
+* <b>Savon Gem:</b> See: http://savonrb.com/. Wouldn't be so simple without it!
+
+== Install
+
+Have to turn into a gem..
+Download / clone the repository. Bundle install the needed gems and require the lib.
+
+ git clone git://github.com/joost/transip-api.git
+ bundle install
+ irb # and require './transip'
+
+== Usage
+
+For the most up-to-date documentation see the source files. Use as follows:
+
+ transip = Transip.new('username', '12.34.12.3')
+ transip.generate_hash('your_api_password')
+ transip.actions # => [:check_availability, .., :set_contacts]
+ transip.request(:get_domain_names)
+ transip.request(:get_info, :domain_name => 'domain.com')
+
+== TODO
+
+* Turn into gem
+
+Please feel free to contribute and send me a pull request via Github!
+
+Copyright (c) 2011 Joost Hietbrink, released under the MIT license
95 transip.rb
@@ -0,0 +1,95 @@
+require "rubygems"
+require "bundler/setup"
+
+require 'savon'
+require 'curb'
+require 'digest/md5'
+#
+# Implements the www.transip.nl API (v2). For more info see: https://www.transip.nl/g/api/
+#
+# Usage:
+# transip = Transip.new('username', '12.34.12.3') # will use readonly mode
+# transip = Transip.new('username', '12.34.12.3', :readwrite) # use this in production
+# transip.generate_hash('your_api_password') # Use this to generate a authentication hash
+# transip.hash = 'your_hash' # Or use this to directly set the hash (so you don't have to use your password in your code)
+# transip.actions # => [:check_availability, :get_whois, :get_domain_names, :get_info, :get_auth_code, :get_is_locked, :register, :cancel, :transfer_with_owner_change, :transfer_without_owner_change, :set_nameservers, :set_lock, :unset_lock, :set_dns_entries, :set_owner, :set_contacts]
+# transip.request(:get_domain_names)
+# transip.request(:get_info, :domain_name => 'yelloyello.be')
+#
+# Credits:
+# Savon Gem - See: http://savonrb.com/. Wouldn't be so simple without it!
+class Transip
+
+ WSDL = 'https://api.transip.nl/wsdl/?service=DomainService'
+
+ attr_accessor :login, :ip, :mode, :hash
+ attr_reader :response
+
+ # Example:
+ # transip = Transip.new('username', '12.34.12.3') # will use readonly mode
+ # transip = Transip.new('username', '12.34.12.3', 'readwrite') # use this in production
+ def initialize(login, ip, mode = :readonly)
+ @login = login
+ @ip = ip
+ @mode = mode
+ end
+
+ # Generates the needed authentication hash.
+ #
+ # NOTE: The password is NOT your general TransIP password
+ # but one specially for the API. Configure it in the Control
+ # Panel.
+ def generate_hash(password)
+ digest_string = "#{login}:#{password}@#{ip}"
+ digest = Digest::MD5.hexdigest(digest_string)
+ @hash = digest
+ end
+
+ # Used as authentication
+ def cookie
+ raise StandardError, "Don't have an authentication hash yet. Please set a hash using generate_hash('your_api_password') or hash= method." if hash.blank?
+ "login=#{login}; hash=#{hash}; mode=#{mode}; "
+ end
+
+ # Same as client method but initializes a brand new fresh client.
+ # You have to use this one when you want to re-set the mode (readwrite, readonly),
+ # or authentication details of your client.
+ def client!
+ @client = Savon::Client.new do
+ wsdl.document = WSDL
+ end
+ @client.http.headers["Cookie"] = cookie
+ return @client
+ end
+
+ # Returns a Savon::Client object to be used in the connection.
+ # This object is re-used and cached as @client.
+ def client
+ @client ||= client!
+ end
+
+ # Returns Array with all possible SOAP WSDL actions.
+ def actions
+ client.wsdl.soap_actions
+ end
+
+ # Returns the response.to_hash (raw Savon::SOAP::Response is also stored in @response).
+ # Examples:
+ # hash_response = transip.request(:get_domain_names)
+ # hash_response[:get_domain_names_response][:return][:item] # => ["your.domain", "names.list"]
+ # For more info see the Transip API docs.
+ # Be sure to rescue all the errors.. since it is hardcore error throwing.
+ def request(action, options = nil)
+ if options.nil?
+ @response = client.request(action)
+ elsif options.is_a?(Hash)
+ @response = client.request(action) do
+ soap.body = options
+ end
+ else
+ raise ArgumentError, "Expected options to be nil or a Hash!"
+ end
+ @response.to_hash
+ end
+
+end

0 comments on commit d073555

Please sign in to comment.
Something went wrong with that request. Please try again.