From 8decfc31f6b12af7988ab28363ad69000cb12d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lipowicz?= Date: Wed, 4 Mar 2009 03:00:39 +0100 Subject: [PATCH] First release. Works great on the Gandi testing platform --- README | 31 +++++++++++++++++++++++++++++++ lib/gandi.rb | 24 ++++++++++++++++++++++++ lib/gandi/errors.rb | 4 ++++ lib/gandi/session.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 lib/gandi.rb create mode 100644 lib/gandi/errors.rb create mode 100644 lib/gandi/session.rb diff --git a/README b/README index e69de29..2d837b6 100644 --- a/README +++ b/README @@ -0,0 +1,31 @@ +USAGE +===== + +require 'gandi' +session = Gandi::Session.connect(:login => "FO1234-GANDI", :password => "foobar") + +session.password("new_password") +session.domain_list +session.account_balance + +domain = "example.org" +period = 1 +owner_handle = "AA1234-GANDI" +admin_handle = "BB2345-GANDI" +tech_handle = "CC3456-GANDI" +billing_handle = "DD4567-GANDI" +ns_list = ["ns1.example.net", "ns2.example.net", "ns1.example.com"] + +operation = session.domain_create( domain, period, + owner_handle, admin_handle, + tech_handle, billing_handle, + ns_list) + +session.operation_list :state => "PENDING" + +TODO +==== +Tests +Gem release +Argument checking for all Gandi methods +Documentation diff --git a/lib/gandi.rb b/lib/gandi.rb new file mode 100644 index 0000000..d11be05 --- /dev/null +++ b/lib/gandi.rb @@ -0,0 +1,24 @@ +# Add the directory containing this file to the start of the load path if it +# isn't there already. +$:.unshift(File.dirname(__FILE__)) unless + $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) + +require 'xmlrpc/client' +require 'gandi/session' +require 'gandi/errors' + +# Gandi/Ruby Library +# +# Author:: Jérôme Lipowicz (mailto:jerome@yayel.com) +# License:: MIT License + +class Array + # from ActiveSupport::CoreExtensions::Array::ExtractOptions + def extract_options! + last.is_a?(::Hash) ? pop : {} + end +end + +module Gandi + VERSION = '1.0.0' +end diff --git a/lib/gandi/errors.rb b/lib/gandi/errors.rb new file mode 100644 index 0000000..63cc192 --- /dev/null +++ b/lib/gandi/errors.rb @@ -0,0 +1,4 @@ +module Gandi + class DataError < ArgumentError ; end + class ServerError < RuntimeError ; end +end \ No newline at end of file diff --git a/lib/gandi/session.rb b/lib/gandi/session.rb new file mode 100644 index 0000000..a256ddb --- /dev/null +++ b/lib/gandi/session.rb @@ -0,0 +1,44 @@ +module Gandi + class Session + HOST = "https://api.gandi.net/xmlrpc/" + + attr_reader :handler, :session_id + + def initialize(login, password, host) + @login = login + @password = password + @host = host + end + + def connect + @handler = XMLRPC::Client.new2(@host) + @session_id = call("login", @login, @password) + end + alias login connect + + def call(*args) + raise LoadError, "no connexion handler is set." unless @handler.is_a?(XMLRPC::Client) + @handler.call(*args) + rescue XMLRPC::FaultException => exception + raise ( exception.faultCode < 500000 ? Gandi::ServerError : Gandi::DataError ), exception.faultString + end + + def method_missing(method, *args) + if %w(password account_currency account_balance domain_list domain_available domain_lock domain_unlock domain_info domain_renew domain_create domain_restore domain_del domain_transfer_in_available domain_transfer_in domain_transfer_out domain_trade domain_change_owner domain_change_contact domain_ns_list domain_ns_add domain_ns_del domain_ns_set host_list host_info host_create host_delete domain_gandimail_activate domain_gandimail_deactivate domain_forward_list domain_forward_set domain_forward_delete domain_mailbox_list domain_mailbox_info domain_mailbox_add domain_mailbox_update domain_mailbox_delete domain_mailbox_alias_list domain_mailbox_alias_set domain_mailbox_purge domain_web_redir_list domain_web_redir_add domain_web_redir_del contact_create contact_update contact_del contact_info operation_list operation_details operation_relaunch operation_cancel tld_list).include?(method.to_s) + call(method, @session_id, *args) + else + raise NoMethodError, "method #{method} is not available in the GANDI API." + end + end + + def self.connect(*args) + config = { :host => HOST }.merge(args.extract_options!) + unless config[:login] && config[:password] + raise ArgumentError, "login and password needed" + end + client = self.new(config[:login], config[:password], config[:host]) + client.connect + return client + end + end +end \ No newline at end of file