Class: Transip

Inherits:
Object
  • Object
show all
Defined in:
lib/transip.rb

Overview

Savon Gem - See: savonrb.com/. Wouldn’t be so simple without it!

Constant Summary collapse

WSDL =
'https://api.transip.nl/wsdl/?service=DomainService'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login, ip, mode = :readonly) ⇒ Transip

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


31
32
33
34
35
# File 'lib/transip.rb', line 31

def initialize(, ip, mode = :readonly)
  @login = 
  @ip = ip
  @mode = mode
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



25
26
27
# File 'lib/transip.rb', line 25

def hash
  @hash
end

#ipObject

Returns the value of attribute ip.



25
26
27
# File 'lib/transip.rb', line 25

def ip
  @ip
end

#loginObject

Returns the value of attribute login.



25
26
27
# File 'lib/transip.rb', line 25

def 
  @login
end

#modeObject

Returns the value of attribute mode.



25
26
27
# File 'lib/transip.rb', line 25

def mode
  @mode
end

#responseObject (readonly)

Returns the value of attribute response.



26
27
28
# File 'lib/transip.rb', line 26

def response
  @response
end

Instance Method Details

#actionsObject

Returns Array with all possible SOAP WSDL actions.



72
73
74
# File 'lib/transip.rb', line 72

def actions
  client.wsdl.soap_actions
end

#clientObject

Returns a Savon::Client object to be used in the connection. This object is re-used and cached as @client.



67
68
69
# File 'lib/transip.rb', line 67

def client
  @client ||= client!
end

#client!Object

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.



57
58
59
60
61
62
63
# File 'lib/transip.rb', line 57

def client!
  @client = Savon::Client.new do
    wsdl.document = WSDL
  end
  @client.http.headers["Cookie"] = cookie
  return @client
end

Used as authentication

Raises:

  • (StandardError)


49
50
51
52
# File 'lib/transip.rb', line 49

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=#{}; hash=#{hash}; mode=#{mode}; "
end

#generate_hash(password) ⇒ Object

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.



42
43
44
45
46
# File 'lib/transip.rb', line 42

def generate_hash(password)
  digest_string = "#{}:#{password}@#{ip}"
  digest = Digest::MD5.hexdigest(digest_string)
  @hash = digest
end

#request(action, options = nil) ⇒ Object

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.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/transip.rb', line 82

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