Module: Sophos::Client::Helper
- Included in:
- Sophos::Client
- Defined in:
- lib/sophos/client/helper.rb
Class Method Summary collapse
-
.common_url(method) ⇒ String
Generates a URL path for the common API.
-
.def_api_call(method, url, singular_method = nil, paged = true) ⇒ Object
Dynamically defines API methods for singular and plural endpoints.
-
.define_paged_method(method, url) ⇒ Object
Defines a paginated method for list-based API endpoints.
-
.define_plain_method(method, url) ⇒ Object
Defines a simple, non-paginated API method.
-
.define_singular_and_plural_methods(method, url, singular_method) ⇒ Object
Defines both singular and plural methods for API endpoints.
-
.endpoint_url(method) ⇒ String
Generates a URL path for the endpoint API.
-
.partner_url(method) ⇒ String
Generates a URL path for the partner API.
-
.sanitize(method_name) ⇒ String
Converts method names to a URL-safe format by replacing underscores with hyphens.
-
.url(api, method) ⇒ String
Constructs the full API URL path.
Class Method Details
.common_url(method) ⇒ String
Generates a URL path for the common API.
Example:
common_url(:alerts) # => "/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.
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.
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.
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.
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"
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"
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"
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"
72 73 74 |
# File 'lib/sophos/client/helper.rb', line 72 def self.url(api, method) "/#{api}/v1/#{sanitize(method)}" end |