class Snuggie::NOC

Snuggie::NOC is the main class for communicating with the Softaculous NOC API.

Constants

API_URL

The Softaculous NOC API lives here

Public Class Methods

new(credentials = {}) click to toggle source

Creates a new Snuggie::NOC instance

Params

* credentials - (Optional) a hash with :username, :password to
                be used to authenticate with Softaculous. If not
                provided, the Snuggie will try using those
                configured via `Snuggie#configure`
# File lib/snuggie/noc.rb, line 22
def initialize(credentials = {})
  credentials = {
    :username => Snuggie.config.username,
    :password => Snuggie.config.password
  } if credentials.empty? && Snuggie.config.username && Snuggie.config.password

  @credentials = credentials
end

Public Instance Methods

buy_license(params = {}) click to toggle source

Buy a license

NOTE: You must specify either months_to_add or years_to_add

Params

* ip             - The IP of the license to be Purchased or Renewed
* months_to_add  - Months to buy/renew the license for
* years_to_add   - Years to buy/renew the license for
* server_type    - Type of server used by this license, should be
                   :dedicated or :vps
* auth_email     - When buying a new license, this is required to
                   verify the owner of the License. This address will
                   be used to send reminders when the license is
                   expiring. Not required for renewals
* auto_renew     - Renew this license automatically before
                   expiration. Set to true or false.
# File lib/snuggie/noc.rb, line 48
def buy_license(params = {})
  params[:ips]        = params.delete(:ip)          if params[:ip]
  params[:autorenew]  = params.delete(:auto_renew)  if params[:auto_renew]
  params[:authemail]  = params.delete(:auth_email)  if params[:auth_email]
  params[:servertype] = params.delete(:server_type) if params[:server_type]

  if params[:months_to_add]
    params[:toadd] = "#{params.delete(:months_to_add)}M"
  elsif params[:years_to_add]
    params[:toadd] = "#{params.delete(:years_to_add)}Y"
  end

  if [:dedicated, :vps].include?(params[:servertype])
    params[:servertype] = params[:servertype] == :dedicated ? 1 : 2
  end

  if params.has_key?(:autorenew)
    params[:autorenew] = params[:autorenew] === true ? 1 : 2
  end

  commit(params.merge!(:ca => :buy, :purchase => 1),
    :require  => [:purchase, :ips, :toadd, :servertype, :autorenew]
  )
end
cancel_license(params = {}) click to toggle source

Cancel license

NOTES:

  • Either ip or key needs to be specified

  • Cancellations are not allowed on licenses expiring more than 1 month in the future.

  • A refund is NOT applied when you cancel a license. You must claim the refund using Snuggie::NOC#refund

Params

* key - (Optional) The license key
* ip  - (Optional) The Primary IP of the license
# File lib/snuggie/noc.rb, line 127
def cancel_license(params = {})
  params[:lickey] = params.delete(:key) if params[:key]
  params[:licip]  = params.delete(:ip)  if params[:ip]
  commit(params.merge!(:ca => :cancel, :cancel_license => 1),
    :require_one => [:lickey, :licip]
  )
end
edit_ips(params = {}) click to toggle source

Edit IPs associated with a license

Params

* license_id - The license ID (**NOT** the license key)
* ips        - The list of IPs of the same VPS/Server. The first IP is
               the Primary IP. You may add up to 8 IPs
# File lib/snuggie/noc.rb, line 142
def edit_ips(params = {})
  params[:'ips[]'] = params.delete(:ips)
  params[:lid]     = params.delete(:license_id) if params[:license_id]
  commit(params.merge!(:ca => :showlicense, :editlicense => 1),
    :require => [:lid, :'ips[]']
  )
end
invoice_details(params = {}) click to toggle source

Get details for an invoice

NOTE: If invoid is 0 or not set, a list of all unbilled transactions for the current month will be returned

Params

* invoice_id - The invoice ID to getch details for.
# File lib/snuggie/noc.rb, line 158
def invoice_details(params = {})
  params[:invoid] = params.delete(:invoice_id) if params[:invoice_id]
  params[:ca]     = :invoicedetails
  commit(params)
end
license_logs(params = {}) click to toggle source

Get Action Logs for a license

NOTE: The logs are returned in **DESCENDING ORDER**, meaning the latest logs will appear first.

Params

* key   - The license key
* limit - The number of action logs to return
# File lib/snuggie/noc.rb, line 173
def license_logs(params = {})
  params[:ca] = :licenselogs
  commit(params, :require => :key)
end
list_licenses(params = {}) click to toggle source

List licenses

NOTE: All parameters are optional. If called without parameters, a list of all of your licenses will be returned.

Params

* key    - (Optional) Search for a specific License by License Key
* ip     - (Optional) Search for a specific License by Primary IP
* expiry - (Optional) Fetch a list of Licenses that are expiring
           Set to 1 to list all expired licenses on your account
           Set to 2 to list licenses expiring in the next 7 days
           Set to 3 to list licenses expiring in the next 15 days
* start  - (Optional) The starting point to return from
           Eg: specify 99 if you have 500 licenses and want to
           return licenses 100-500.
* limit  - (Optional) The length to limit the result set to
           Eg: specify 100 if you have 500 licenses and want to
           limit the result set to 100 items
# File lib/snuggie/noc.rb, line 106
def list_licenses(params = {})
  params[:lickey] = params.delete(:key)   if params[:key]
  params[:ips]    = params.delete(:ip)    if params[:ip]
  params[:len]    = params.delete(:limit) if params[:limit]
  params[:ca]     = :licenses
  commit(params)
end
refund(params = {}) click to toggle source

Refund a transaction

NOTE: A refund can only be issued within 7 days of buying/renewing a license.

Params

* action_id - The Action ID to clain a refund for
# File lib/snuggie/noc.rb, line 81
def refund(params = {})
  params[:actid] = params.delete(:action_id) if params[:action_id]
  params[:ca]    = :refund
  commit(params, :require => [:actid])
end

Private Instance Methods

commit(params, options = {}) click to toggle source

Send a request upstream to Softaculous

Params

* params  - a hash of parameters for the request
* options - a hash of options, used to validate required params
# File lib/snuggie/noc.rb, line 186
def commit(params, options = {})
  if options[:require]
    unless require_params(params, options[:require])
      raise Errors::MissingArgument
    end
  end

  if options[:require_one]
    unless require_one_of(params, options[:require_one])
      raise Errors::MissingArgument
    end
  end

  params.merge!(@credentials) unless @credentials.nil? || @credentials.empty?

  uri = "#{API_URL}?#{query_string(params)}"
  if res = fetch(uri)
    @response = begin
                  PHP.unserialize(res.body)
                rescue
                  res.body
                end
  end
end
fetch(uri_str, limit = 10) click to toggle source

Performs a GET request on the given URI, redirects if needed

Params

* uri_str - the URL to fetch as a string
* limit   - number of redirects to allow
# File lib/snuggie/noc.rb, line 259
def fetch(uri_str, limit = 10)
  # You should choose better exception.
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0
  Faraday.new(:url => uri_str) do |c|
    c.response :raise_error
    c.response :follow_redirects, :limit => limit
    c.adapter :net_http
  end.get
end
query_string(params) click to toggle source

Formats params into a query string for a GET request

NOTE: For convenience, this converts the keys :username and

:password to :nocname and :nocpass respectively.

Params

* params - hash of query parameters
# File lib/snuggie/noc.rb, line 241
def query_string(params)
  params.map do |key, val|
    case key.to_sym
    when :username
      key = :nocname
    when :password
      key = :nocpass
    end
    "#{key}=#{CGI.escape(val.to_s)}"
  end.join('&')
end
require_one_of(params, keys) click to toggle source

Returns true if params has one of the specified keys

Params

* params - hash of query parameters
* keys   - keys to search for in params
# File lib/snuggie/noc.rb, line 228
def require_one_of(params, keys)
  Array(keys).each { |key| return true if params[key] }
  false
end
require_params(params, keys) click to toggle source

Returns true if params has all of the specified keys

Params

* params - hash of query parameters
* keys   - keys to search for in params
# File lib/snuggie/noc.rb, line 217
def require_params(params, keys)
  Array(keys).each { |key| return false unless params[key] }
  true
end