Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: initial import of slightly tweaked code, this is 3 years old…
… so could be better
- Loading branch information
Showing
13 changed files
with
550 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'net/https' | ||
require 'json' | ||
require 'uri' | ||
require 'logger' | ||
|
||
require 'cpanel_api/remote' | ||
require 'cpanel_api/response' | ||
require 'cpanel_api/model' | ||
require 'cpanel_api/cpanel/commands' | ||
require 'cpanel_api/whm/commands' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module CpanelApi | ||
module Cpanel | ||
module Commands | ||
|
||
def addsubdomain(user, subdomain, rootdomain, directory) | ||
cpanel(user, {:xmlin => "<cpanelaction><module>SubDomain</module><func>addsubdomain</func><args><domain>#{subdomain}</domain><rootdomain>#{rootdomain}</rootdomain><dir>#{directory}</dir></args></cpanelaction>"}) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module CpanelApi | ||
module Model | ||
|
||
def key_mappings(map, hash) | ||
map.each_key do |k| | ||
if hash.has_key? k | ||
hash[map[k]] = hash[k] | ||
hash.delete(k) | ||
end | ||
end | ||
hash | ||
end | ||
|
||
def filter_options(hash) | ||
hash.each do |k,v| | ||
if v == true | ||
hash[k] = 1 | ||
elsif v == false | ||
hash[k] = 0 | ||
end | ||
end | ||
hash.delete_if {|k,v| v.nil? } | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module CpanelApi | ||
module Remote | ||
|
||
@@default_port = 2087 | ||
@@default_user = 'root' | ||
|
||
def set_server(options={}) | ||
self.base_url = options[:url] | ||
self.user = options[:user] ||= @@default_user | ||
self.api_key = options[:api_key].gsub(/\n|\r/, '') | ||
self.port = options[:port] ||= @@default_port | ||
end | ||
|
||
def connect(server, port, command, username, api_key, options={}) | ||
http = Net::HTTP.new(server, port) | ||
http.use_ssl = true | ||
http.start do |http| | ||
req = Net::HTTP::Get.new "/json-api/#{command}#{map_options_to_url(options)}" | ||
req.add_field 'Authorization', "WHM #{username}:#{api_key}" | ||
# req.each_header {|k,v| @log.info "#{k}: #{v}"} | ||
http.request(req) | ||
# puts response.to_yaml | ||
# response.value | ||
# response.body | ||
end | ||
end | ||
|
||
def do_request(command, options={}, mappings=nil) | ||
options = key_mappings(mappings, options) unless mappings.nil? | ||
options = filter_options(options) | ||
response = connect self.base_url, self.port, command, self.user, self.api_key, options | ||
# if response.code == '200' | ||
# response.json = JSON.parse(response.body) | ||
# end | ||
CpanelApi::Response.new(response) | ||
end | ||
|
||
def map_options_to_url(options={}) | ||
'?' + options.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join('&') unless options.nil? | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module CpanelApi | ||
class Response | ||
|
||
def initialize(response) | ||
@response = response | ||
end | ||
|
||
def json | ||
JSON.parse @response.body | ||
end | ||
|
||
def method_missing(method, *args) | ||
@response.send(method) if @response.respond_to?(method) | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.