Class: Cpanel::Server

Inherits:
Object
  • Object
show all
Includes:
ActiveResource
Defined in:
lib/cpanel/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



12
13
14
15
16
17
# File 'lib/cpanel/server.rb', line 12

def initialize(options = {})
  @url = URI.parse(options[:url])
  @key = format_key(options[:key])
  @timeout = options[:timeout] || 300
  @api = options[:api] || "json"
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



10
11
12
# File 'lib/cpanel/server.rb', line 10

def api
  @api
end

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/cpanel/server.rb', line 10

def key
  @key
end

#timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/cpanel/server.rb', line 10

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/cpanel/server.rb', line 10

def url
  @url
end

Instance Method Details

#request(script, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cpanel/server.rb', line 23

def request(script, options = {})
  request = Net::HTTP::Get.new("/#{api}-api/" + script)
  request.add_field "Authorization", "WHM root:#{key}"
  request.set_form_data(options) unless options.empty?
  
  result = http.request(request)
  response = handle_response(result)
  
  return Response.new(response)
rescue Timeout::Error, Errno::ETIMEDOUT => e
  raise TimeoutError.new(e.message)
rescue Errno::ECONNREFUSED => e
  raise CommandFailed.new("Connection to cPanel server was refused; may be down or unreachable")
end