Class: Plesk::Client
- Inherits:
-
Object
- Object
- Plesk::Client
- Defined in:
- lib/plesk/client.rb
Defined Under Namespace
Classes: ActionError, Error, RequestError
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(host, username, password, options = {}) ⇒ Client
constructor
A new instance of Client.
- #request(controller, action, &block) ⇒ Object
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, = {}) @host = host @options = @username = username @password = password end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/plesk/client.rb', line 13 def @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 = xml.xpath('/packet/system/errtext').first&.content raise RequestError, "#{error_code || "-1"}: #{}" elsif xml.xpath("/packet/#{controller}/#{action}/result/status").first&.content == "error" error_code = xml.xpath("/packet/#{controller}/#{action}/result/errcode").first&.content = xml.xpath("/packet/#{controller}/#{action}/result/errtext").first&.content raise ActionError, "#{error_code || "-1"}: #{}" else xml.xpath("//result")[0] end end |