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

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

Class Method Summary collapse

Class Method Details

.parse_response(raw) ⇒ Object

Converts the API response to a Hash



41
42
43
44
45
46
47
48
49
# File 'lib/whmcs/base.rb', line 41

def self.parse_response(raw)
  return {} if raw.to_s.blank?

  if raw.match(/xml version/)
    Crack::XML.parse(raw)
  else
    Hash[raw.split(';').map { |line| line.split('=') }]
  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
# File 'lib/whmcs/base.rb', line 15

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

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

  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