Class: NinjaOne::Client

Inherits:
API
  • Object
show all
Includes:
Backup, Devices, Organizations, Queries, System, Users
Defined in:
lib/ninjaone/client.rb,
lib/ninjaone/client/users.rb,
lib/ninjaone/client/backup.rb,
lib/ninjaone/client/system.rb,
lib/ninjaone/client/devices.rb,
lib/ninjaone/client/queries.rb,
lib/ninjaone/client/organizations.rb

Overview

Note:

All methods are grouped in separate modules for better organization and follow the structure provided in the official API documentation.

The NinjaOne::Client class acts as a wrapper for the NinjaOne REST API. This class inherits from the NinjaOne::API class and includes modules that group the API methods according to the structure in the NinjaOne API documentation.

Defined Under Namespace

Modules: Backup, Devices, Organizations, Queries, System, Users

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Organizations

#organization_backup_usage_by_location, #organization_location_custom_fields

Methods included from System

#search_devices

Methods inherited from API

#config, #initialize

Methods included from Authentication

#auth_token

Constructor Details

This class inherits a constructor from NinjaOne::API

Class Method Details

.define_endpoint(scope, resource = nil, paged = false) ⇒ Object

Dynamically defines methods for interacting with NinjaOne API resources.

Depending on the arguments, this will define methods to:

  • Fetch all records for the given scope
  • Fetch a resource specific record by ID

Paging is not supported.

Examples:

Defining endpoints

api_endpoint :companies, :company
# Defines:
# - `companies(params = {})` to fetch all companies.
# - `company(id, params = {})` to fetch a single company by ID.

Parameters:

  • scope (Symbol)

    The method name for fetching all records.

  • resource (String) (defaults to: nil)

    If given, fetches all records for scope by id. If empty string, it will load single scope record



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ninjaone/client.rb', line 37

def self.define_endpoint(scope, resource = nil, paged = false)
  if resource
    name = self.resource_to_name(scope)
    name = "#{name}_#{self.resource_to_name(resource)}" if resource && !resource.empty?
    # change to nil if resource is empty so we ar eable to generate /scope/id/resource
    # but also /scope/id without training / 
    resource = resource&.empty? ? nil : resource
    send(:define_method, name) do |id, params = {}|
      url = api_url([scope, id, resource].compact.join('/'))
      paged ? get_paged(url, params) : get(url, params)
    end
  else
    name = self.resource_to_name(scope)
    send(:define_method, name) do |params = {}|
      url = api_url(scope)
      paged ? get_paged(url, params) : get(url, params)
    end
  end
end

.resource_to_name(path) ⇒ String

Constructs url resource name from url path

Parameters:

  • path (String)

    Resource name

Returns:

  • (String)

    translated from /- to _



17
18
19
# File 'lib/ninjaone/client.rb', line 17

def self.resource_to_name(path)
  path.to_s.gsub(/[-\/]/,'_')
end

Instance Method Details

#api_url(path) ⇒ String

Constructs the full API URL for a given path.

Parameters:

  • path (String)

    The API path.

Returns:

  • (String)

    The full API URL.



76
77
78
# File 'lib/ninjaone/client.rb', line 76

def api_url(path)
  "/v2/#{path}"
end