lib/loopiator/client.rb in loopiator-0.2.0 vs lib/loopiator/client.rb in loopiator-0.3.0

- old
+ new

@@ -1,66 +1,39 @@ -require 'uri' -require 'cgi' -require 'rubygems' require 'xmlrpc/client' require 'simpleidn' module Loopiator class Client - attr_accessor :config, :client, :username, :password, :endpoint + attr_accessor :client include Loopiator::Logger - def initialize(options = {}) - @config = set_config - - options.symbolize_keys! - @config.merge!(options) - - @username = @config.fetch(:username, nil) - @password = @config.fetch(:password, nil) - @endpoint = @config.fetch(:endpoint, nil) - - timeout = options.fetch(:timeout, 500) - - set_client(timeout) + def initialize(connection_options = nil) + set_client(connection_options) end - - def set_config - rails_env = defined?(Rails) ? Rails.env : "development" - config ||= YAML.load_file(File.join(Rails.root, "config/loopia.yml"))[rails_env] if defined?(Rails) - config ||= YAML.load_file(File.join(File.dirname(__FILE__), "../generators/templates/loopia.yml"))[rails_env] rescue nil - config ||= YAML.load_file(File.join(File.dirname(__FILE__), "../generators/templates/loopia.template.yml"))[rails_env] rescue nil - config ||= {} - - config.symbolize_keys! - - return config - end - def set_client(timeout = 500) - @client = XMLRPC::Client.new_from_uri(@endpoint) - @client.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE) - @client.timeout = timeout - @client + def set_client(connection_options = nil) + self.client = XMLRPC::Client.new_from_hash(generate_connection_options(connection_options)) + self.client.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE) + self.client end def call(rpc_method, *args) - response = "" + response = nil begin - response = @client.call(rpc_method, @username, @password, *args) + response = self.client.call(rpc_method, Loopiator.configuration.username, Loopiator.configuration.password, *args) rescue EOFError => eof_error raise Loopiator::ConnectionError end return response end def parse_status_response(response) - response = response.downcase.to_sym + response = response.downcase.to_sym case response when :ok then return response when :domain_occupied then return response when :auth_error then raise Loopiator::AuthError @@ -72,7 +45,23 @@ end end include Loopiator::Domains include Loopiator::Credits + + private + def generate_connection_options(connection_options = nil) + connection_options ||= { + host: Loopiator.configuration.host, + path: Loopiator.configuration.path, + port: Loopiator.configuration.port, + use_ssl: Loopiator.configuration.use_ssl, + timeout: Loopiator.configuration.timeout, + proxy_host: Loopiator.configuration.proxy_host, + proxy_port: Loopiator.configuration.proxy_port, + user: Loopiator.configuration.proxy_user, + password: Loopiator.configuration.proxy_password, + } + end + end end \ No newline at end of file