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



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

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
39
# 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)

  # TODO: Configure Faraday for HTTPS requests
  conn = Faraday.new(:url => url) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end

  response = conn.post do |req|
    req.body = params
  end

  parse_response(response.body)
end