Class: Plesk::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, *credentials) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/plesk/client.rb', line 7

def initialize host, *credentials
  if credentials.size == 2
    @headers = {
      'HTTP_AUTH_LOGIN' => credentials[0],
      'HTTP_AUTH_PASSWD' => credentials[1],
      'Accept' => '*/*',
      'Content-Type' => 'text/xml',
    }
  elsif credentials.length == 1
    @headers = {
      'KEY' => credentials[0],
      'Accept' => '*/*',
      'Content-Type' => 'text/xml',
    }
  else
    raise ArgumentError
  end
  @response = 0
  @uri = URI.parse "https://example.com:8443/enterprise/control/agent.php"
  @uri.host = host
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  @http = http
end

Instance Attribute Details

#httpObject

Returns the value of attribute http.



6
7
8
# File 'lib/plesk/client.rb', line 6

def http
  @http
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/plesk/client.rb', line 6

def response
  @response
end

#uriObject

Returns the value of attribute uri.



6
7
8
# File 'lib/plesk/client.rb', line 6

def uri
  @uri
end

Instance Method Details

#get_domain_id_for(domain) ⇒ Object



32
33
34
35
36
37
# File 'lib/plesk/client.rb', line 32

def get_domain_id_for domain
  packet = Packet.new
  packet.domain_info_for_domain domain
  answer = start_request packet.to_xml
  answer.at('id').text
end

#get_mailgroup_info_for(mail) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/plesk/client.rb', line 38

def get_mailgroup_info_for mail
  name,domain = mail.split("@")
  domain_id = get_domain_id_for domain
  packet = Packet.new
  packet.mailgroup_info name,domain_id
  answer = start_request packet.to_xml

  answer.search('address').map(&:text)
end

#get_secret_for_ip(ip) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/plesk/client.rb', line 54

def get_secret_for_ip ip
  packet = Packet.new
  packet.secret_key_for_ip ip
  answer = start_request packet.to_xml
  if answer.at('status').text == "ok"
    answer.at('key').text
  else
    $stderr.puts answer.body
    raise PleskException
  end
end

#set_mailgroup_for(mail, mails) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/plesk/client.rb', line 47

def set_mailgroup_for mail,mails
  name,domain = mail.split("@")
  domain_id = get_domain_id_for domain
  packet = Packet.new
  packet.mailgroup_set name,domain_id,mails
  answer = start_request packet.to_xml
end

#start_request(body) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/plesk/client.rb', line 66

def start_request(body)
  response = http.request_post(@uri.path,body,@headers)
  if (400...600).include? response.code.to_i
    raise PleskException
  end
  @response = response
  Nokogiri::XML(response.body)
end