Class: Lumberg::Cpanel::Base

Inherits:
Whm::Base show all
Defined in:
lib/lumberg/cpanel/base.rb

Instance Attribute Summary collapse

Attributes inherited from Whm::Base

#server

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Whm::Base

#setup_server

Constructor Details

#initialize(options = {}) ⇒ Base

Public: Initialize a new cPanel API interface.

options - Hash options (default: {}):

:api_username - String username to set as the default 
                username for cPanel API calls (optional).


20
21
22
23
24
# File 'lib/lumberg/cpanel/base.rb', line 20

def initialize(options = {})
  @api_username = options.delete(:api_username)

  super options
end

Instance Attribute Details

#api_usernameObject

Returns the value of attribute api_username



4
5
6
# File 'lib/lumberg/cpanel/base.rb', line 4

def api_username
  @api_username
end

Class Method Details

.api_moduleObject

Public: Gets cPanel API module name based on class name. This method should be overriden in subclasses where the cPanel API module name and the class name don't match up.

Returns String cPanel API module name.



11
12
13
# File 'lib/lumberg/cpanel/base.rb', line 11

def self.api_module
  self.name.split("::").last
end

Instance Method Details

#perform_request(options = {}) ⇒ Object

Public: Perform a cPanel API method call.

options - Hash options for API call params (default: {}):

:api_function - String API function name to call.
:api_module   - String API module on which API function will be
                called (optional, default: self.class.api_module).
:response_key - String key used to select desired part of API
                response (optional, default: "cpanelresult").
:api_username - String  username for API call (optional,
                default: @api_username)
:api_version  - Integer cPanel API version to use (optional,
                default: 2)

Returns Hash API response.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lumberg/cpanel/base.rb', line 40

def perform_request(options = {})
  options[:api_username] ||= api_username

  api_module = options.delete(:api_module) || self.class.api_module
  params = {
    response_key:              options.delete(:response_key) || "cpanelresult",
    cpanel_jsonapi_user:       options.delete(:api_username),
    cpanel_jsonapi_module:     api_module,
    cpanel_jsonapi_func:       options.delete(:api_function),
    cpanel_jsonapi_apiversion: options.delete(:api_version) || 2,
  }.merge(options)

  server.perform_request("cpanel", params)
end