Class: Domainshare::Client
- Inherits:
-
Object
- Object
- Domainshare::Client
- Defined in:
- lib/domainshare/client.rb
Constant Summary collapse
- @@WARN =
false
Instance Method Summary collapse
- #call_function(name, type, params = {}) ⇒ Object
- #error_msg(type, str) ⇒ Object
- #init_functions ⇒ Object
-
#initialize(email, password) ⇒ Client
constructor
A new instance of Client.
- #list_functions(detail = true) ⇒ Object
- #method_missing(*arg) ⇒ Object
Constructor Details
#initialize(email, password) ⇒ Client
Returns a new instance of Client.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/domainshare/client.rb', line 4 def initialize(email, password) if (email.empty? or password.empty?) and @@WARN puts "Either email or password is empty. Most of the functions will not work, unless those are added to each funtion calls" end @email = email @password = password @function_list = { "ping" => {}, "availability_check" => {:args => ["email", "password", "domainname"] }, "register" => {:args => ["email", "password", "domainname", "enduseremail"], :opt_args => [ "monthsofregistration", "nameserver", "forwardurl" ] }, "renew" => { :args => ["email", "password", "domainname"], :opt_args => ["monthsofregistration"] }, "host_registration" => { :args => ["email", "password", "hostname", "ipaddress"] }, "host_removal" => { :args => ["email", "password", "hostname"] }, "host_list" => { :args => ["email", "password", "domainname"] }, "modify" => { :args => ["email", "password", "domainname"], :opt_args => ["nameserver", "forwardurl"] }, "resend_email" => { :args => ["email", "password", "domainname"], :opt_args => ["enduseremail"] }, "domain_deactivate" => { :args => ["email", "password", "domainname", "reason"] }, "domain_reactivate" => { :args => ["email", "password", "domainname"] }, "update_parking" => { :args => ["email", "password", "domainname"], :opt_args => ["category", "keyword"]} } # Extend function_list to add new API functions. # :args -> Mandatory arguments, # :opt_args -> Optional arguments. init_functions() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*arg) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/domainshare/client.rb', line 89 def method_missing(*arg) # Parses the API function calls (ping, ping_xml, ping_json etc.) # into name and type (ping, xml), and calls call_function. name = arg[0].id2name type = "xml" if name =~ /_xml$/ name.slice!(/\_xml$/) elsif name =~ /_json$/ name.slice!(/_json$/) type = "json" end call_function(name, type, *arg[1,arg.size]) end |
Instance Method Details
#call_function(name, type, params = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/domainshare/client.rb', line 50 def call_function(name, type, params = {}) # Searches in function lists for function 'name' and calls the # API function with params. params = {} if not params.is_a? Hash params["email"] = @email if not params.has_key?("email") params["password"] = @password if not params.has_key?("password") return error_msg(type, ["Function not defined ", name] ) if not @functions.has_key?(name) function = @functions[name] function.args.each do |value| if not params.has_key?(value) return error_msg(type, [value, " not defined in input; Mandatory parameters are ", function.args.join(",")] ) end end function.call(params, type=type) end |
#error_msg(type, str) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/domainshare/client.rb', line 70 def error_msg(type, str) # Generates and returns the client side error messages in # user-specified format if type == 'json' return '{"status":"NOT OK","reason":"'.concat(str.join()).concat('"}') else xml = <<EOS <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <dottk> <status>NOT OK</status> <reason></reason> </dottk> EOS doc = REXML::Document.new xml doc.elements["dottk"].elements["reason"].text = str.join() return doc.to_s end end |
#init_functions ⇒ Object
29 30 31 32 33 34 |
# File 'lib/domainshare/client.rb', line 29 def init_functions @functions = {} @function_list.each do |name, value| @functions[name] = APIFUNC.new( name, args = value[:args] ? value[:args] : {}, opt_args = value[:opt_args] ? value[:opt_args] : {} ) end end |
#list_functions(detail = true) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/domainshare/client.rb', line 36 def list_functions(detail = true) # Lists all the supported functions along with mandatory and optional # arguments, where relevant. i=0 puts "Available API functions are" @functions.each do |key, value| i += 1 print i, ". ",key, "\n" print "\tArguments:- ", value.args, "\n" if not value.args.empty? print "\tOptional Arguments:- ", value.opt_args, "\n" if not value.opt_args.empty? and detail puts end end |