Class: Plesk::Client

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

Defined Under Namespace

Classes: ActionError, Error, RequestError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, username, password, options = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
# File 'lib/plesk/client.rb', line 15

def initialize(host, username, password, options = {})
  @host = host
  @options = options
  @username = username
  @password = password
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/plesk/client.rb', line 13

def options
  @options
end

Instance Method Details

#request(controller, action, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plesk/client.rb', line 22

def request(controller, action, &block)
  request = Net::HTTP::Post.new("/enterprise/control/agent.php")
  params = ParamBuilder.build(&block)
  request.body = "<packet><#{controller}><#{action}>#{params}</#{action}></#{controller}></packet>"
  request.add_field 'User-Agent', "PleskRuby/#{Plesk::VERSION}"
  request.add_field 'HTTP_AUTH_LOGIN', @username
  request.add_field 'HTTP_AUTH_PASSWD', @password
  request.add_field 'Content-Type', 'text/xml'
  connection = Net::HTTP.new(@host, @options[:port] || 8443)
  connection.use_ssl = true
  connection.verify_mode = @options[:verify_ssl] == false ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
  result = connection.request(request)
  xml = Nokogiri::XML.parse(result.body)
  if xml.xpath('/packet/system/status').first&.content == "error"
    error_code = xml.xpath('/packet/system/errcode').first&.content
    error_message = xml.xpath('/packet/system/errtext').first&.content
    raise RequestError, "#{error_code || "-1"}: #{error_message}"
  elsif xml.xpath("/packet/#{controller}/#{action}/result/status").first&.content == "error"
    error_code = xml.xpath("/packet/#{controller}/#{action}/result/errcode").first&.content
    error_message = xml.xpath("/packet/#{controller}/#{action}/result/errtext").first&.content
    raise ActionError, "#{error_code || "-1"}: #{error_message}"
  else
    xml.xpath("//result")[0]
  end
end