Class: WHMCS::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/whmcs/base.rb

Overview

WHMCS::Base is the main class used to subclass WHMCS API resources

Direct Known Subclasses

Announcement, Client, Domain, Invoice, Misc, Module, Order, Quote, Ticket

Class Method Summary collapse

Class Method Details

.parse_response(raw) ⇒ Object

Converts the API response to a Hash



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/whmcs/base.rb', line 45

def self.parse_response(raw)
  result = {}
  return result if raw.to_s.empty?
  
  if raw.match(/xml version/)
    Crack::XML.parse(raw)
  else
    # in case of password encrypt/decrypt - '=' should be properly parsed
    CGI::unescapeHTML(raw).split(';').map do |line|
      m = /^(\w+)\=(.*)$/.match(line)
      result[ m[1] ] = m[2]
    end
    result
  end
end

.send_request(params = {}) ⇒ Object

Sends an API request to the WHMCS API

Parameters:

  • :action - The API action to perform

All other paramters are passed along as HTTP POST variables



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/whmcs/base.rb', line 15

def self.send_request(params = {})
  if params[:action].empty?
    raise "No API action set"
  end

  params.merge!(
    :username => WHMCS.config.api_username,
    :password => WHMCS.config.api_password
  )

  # alternative API access
  if( !WHMCS.config.api_key.nil? )
    params.merge!( :accesskey => WHMCS.config.api_key )
  end

  url = URI.parse(WHMCS.config.api_url)

  http = Net::HTTP.new(url.host, url.port)

  if url.port == 443
    http.use_ssl = true
  end

  req = Net::HTTP::Post.new(url.path)
  req.set_form_data(params)
  res = http.start { |http| http.request(req) }
  parse_response(res.body)
end