Module: Panelbeater::Remote

Included in:
Whm::Commands
Defined in:
lib/panelbeater/remote.rb

Constant Summary collapse

@@default_port =
2087
@@default_user =
'root'

Instance Method Summary collapse

Instance Method Details

#connect(server, port, command, username, api_key, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/panelbeater/remote.rb', line 14

def connect(server, port, command, username, api_key, options={})
  http = Net::HTTP.new(server, port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Ignore invalid SSL certificates
  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}"
    http.request(req)
  end
end

#do_request(command, options = {}, mappings = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/panelbeater/remote.rb', line 25

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
  Panelbeater::Response.new(command, response)
end

#map_options_to_url(options = {}) ⇒ Object



32
33
34
# File 'lib/panelbeater/remote.rb', line 32

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

#set_server(options = {}) ⇒ Object



7
8
9
10
11
12
# File 'lib/panelbeater/remote.rb', line 7

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