Module: Sophos::Client::Helper

Included in:
Sophos::Client
Defined in:
lib/sophos/client/helper.rb

Class Method Summary collapse

Class Method Details

.common_url(method) ⇒ String

Generates a URL path for the common API.

Example:

common_url(:alerts) # => "/common/v1/alerts"

Parameters:

  • method (Symbol, String)

    The endpoint method name.

Returns:

  • (String)

    Full path for the common API (e.g., /common/v1/alerts).



38
39
40
# File 'lib/sophos/client/helper.rb', line 38

def self.common_url(method)
  url('common', method)
end

.def_api_call(method, url, singular_method = nil, paged = true) ⇒ Object

Dynamically defines API methods for singular and plural endpoints.

If singular_method is provided, it generates:

  • A plural method for fetching all resources (e.g., alerts).
  • A singular method for fetching a specific resource by ID (e.g., alert(id)).

Example:

def_api_call(:alerts, "/common/v1/alerts", :alert)

# Generates:
# - alerts(params = {}) – Fetches paginated alerts.
# - alert(id, params = {}) – Fetches a specific alert by ID.

Parameters:

  • method (Symbol)

    The plural method name (e.g., :alerts).

  • url (String)

    The API endpoint URL path.

  • singular_method (Symbol, nil) (defaults to: nil)

    The singular method name (optional).

  • paged (Boolean) (defaults to: true)

    Whether the method should support pagination (default: true).

See Also:

  • define_paged_method, define_plain_method


95
96
97
98
99
100
101
# File 'lib/sophos/client/helper.rb', line 95

def self.def_api_call(method, url, singular_method = nil, paged = true)
  if singular_method
    define_singular_and_plural_methods(method, url, singular_method)
  else
    paged ? define_paged_method(method, url) : define_plain_method(method, url)
  end
end

.define_paged_method(method, url) ⇒ Object

Defines a paginated method for list-based API endpoints.

Example:

define_paged_method(:alerts, "/common/v1/alerts")

# Generates:
# - alerts(params = {}) – Fetches paginated alerts.

Parameters:

  • method (Symbol)

    The method name (e.g., :alerts).

  • url (String)

    The API endpoint URL path.



141
142
143
144
145
# File 'lib/sophos/client/helper.rb', line 141

def self.define_paged_method(method, url)
  define_method(method) do |params = {}|
    get_paged(url, params)
  end
end

.define_plain_method(method, url) ⇒ Object

Defines a simple, non-paginated API method.

Example:

define_plain_method(:roles, "/partner/v1/roles")

# Generates:
# - roles(params = {}) – Fetches roles without pagination.

Parameters:

  • method (Symbol)

    The method name (e.g., :roles).

  • url (String)

    The API endpoint URL path.



157
158
159
160
161
# File 'lib/sophos/client/helper.rb', line 157

def self.define_plain_method(method, url)
  define_method(method) do |params = {}|
    get(url, params)
  end
end

.define_singular_and_plural_methods(method, url, singular_method) ⇒ Object

Defines both singular and plural methods for API endpoints.

  • The plural method fetches either paginated resources or a single resource if an ID is passed.
  • The singular method explicitly fetches a single resource by ID.

Example:

define_singular_and_plural_methods(:alerts, "/common/v1/alerts", :alert)

# Generates:
# - alerts(params = {}) – Fetches paginated alerts.
# - alerts(id, params = {}) – Fetches a single alert by ID.
# - alert(id, params = {}) – Fetches a single alert explicitly by ID.

Parameters:

  • method (Symbol)

    The plural method name (e.g., :alerts).

  • url (String)

    The API endpoint URL path.

  • singular_method (Symbol)

    The singular method name (e.g., :alert).



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sophos/client/helper.rb', line 119

def self.define_singular_and_plural_methods(method, url, singular_method)
  # Define plural method: paginated call if no ID, otherwise fetch singular resource.
  define_method(method) do |id = nil, params = {}|
    id ? get("#{url}/#{id}", params) : get_paged(url, params)
  end

  # Define singular method explicitly for single resource retrieval.
  define_method(singular_method) do |id, params = {}|
    get("#{url}/#{id}", params)
  end
end

.endpoint_url(method) ⇒ String

Generates a URL path for the endpoint API.

Example:

endpoint_url(:downloads) # => "/endpoint/v1/downloads"

Parameters:

  • method (Symbol, String)

    The endpoint method name.

Returns:

  • (String)

    Full path for the endpoint API.



49
50
51
# File 'lib/sophos/client/helper.rb', line 49

def self.endpoint_url(method)
  url('endpoint', method)
end

.partner_url(method) ⇒ String

Generates a URL path for the partner API.

Example:

partner_url(:tenants) # => "/partner/v1/tenants"

Parameters:

  • method (Symbol, String)

    The endpoint method name.

Returns:

  • (String)

    Full path for the partner API.



60
61
62
# File 'lib/sophos/client/helper.rb', line 60

def self.partner_url(method)
  url('partner', method)
end

.sanitize(method_name) ⇒ String

Converts method names to a URL-safe format by replacing underscores with hyphens.

Example:

sanitize(:user_groups) # => "user-groups"

Parameters:

  • method_name (Symbol, String)

    The method name to sanitize.

Returns:

  • (String)

    The sanitized method name (e.g., 'user_groups' => 'user-groups').



27
28
29
# File 'lib/sophos/client/helper.rb', line 27

def self.sanitize(method_name)
  method_name.to_s.tr('_', '-')
end

.url(api, method) ⇒ String

Constructs the full API URL path.

Example:

url('common', :alerts) # => "/common/v1/alerts"

Parameters:

  • api (String)

    API type (e.g., 'common', 'endpoint', 'partner').

  • method (Symbol, String)

    The sanitized endpoint method name.

Returns:

  • (String)

    Full API URL path.



72
73
74
# File 'lib/sophos/client/helper.rb', line 72

def self.url(api, method)
  "/#{api}/v1/#{sanitize(method)}"
end