Skip to content

Commit

Permalink
feature: initial import of slightly tweaked code, this is 3 years old…
Browse files Browse the repository at this point in the history
… so could be better
  • Loading branch information
kernow committed Jan 29, 2010
1 parent 4f81150 commit c7e9cb0
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/cpanel_api.rb
@@ -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'
10 changes: 10 additions & 0 deletions lib/cpanel_api/cpanel/commands.rb
@@ -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
26 changes: 26 additions & 0 deletions lib/cpanel_api/model.rb
@@ -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
43 changes: 43 additions & 0 deletions lib/cpanel_api/remote.rb
@@ -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
17 changes: 17 additions & 0 deletions lib/cpanel_api/response.rb
@@ -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

0 comments on commit c7e9cb0

Please sign in to comment.