module Evvnt::PathHelpers

Internal: Helper methods defining API paths for the defined resources

Constants

PARAM_REGEX

A regular expression to check for params in a URL String.

Public Instance Methods

resource_name() click to toggle source

The name for this class's corresponding resource on the API.

# File lib/evvnt/path_helpers.rb, line 16
def resource_name
  @resource_name || default_resource_name
end
singular_resource?() click to toggle source

Is this class defined as a singular resource?

Returns Boolean

# File lib/evvnt/path_helpers.rb, line 11
def singular_resource?
  @singular_resource == true
end

Protected Instance Methods

resource_name=(value) click to toggle source
# File lib/evvnt/path_helpers.rb, line 30
def resource_name=(value)
  @resource_name = value
end
singular_resource!() click to toggle source

Declare this class as a singular resource

# File lib/evvnt/path_helpers.rb, line 25
def singular_resource!
  @singular_resource = true
  self.resource_name = name.split("::").last.underscore.singularize
end

Private Instance Methods

default_resource_name() click to toggle source

The default name of this resource on the API (e.g. “users”)

Returns String

# File lib/evvnt/path_helpers.rb, line 39
def default_resource_name
  @default_resource_name || name.split("::").last.underscore.pluralize
end
default_resource_path() click to toggle source

The default path of this resource on the API (e.g. “/users”)

Returns String

# File lib/evvnt/path_helpers.rb, line 46
def default_resource_path
  resource_name
end
nest_path_within_parent(path, params) click to toggle source

Nest a given resource path within a parent resource.

path - A String representing an API resource path. params - A Hash of params to send to the API.

Examples

nest_path_within_parent("bags/1", {user_id: "123"}) # => /users/123/bags/1

Returns String

# File lib/evvnt/path_helpers.rb, line 60
def nest_path_within_parent(path, params)
  return path unless params_include_parent_resource_id?(params)
  params.symbolize_keys!
  parent_resource = parent_resource_name(params)
  parent_id       = params.delete(parent_resource_param(params)).try(:to_s)
  File.join(*[parent_resource, parent_id, path].compact).to_s
end
plural_resource_path() click to toggle source
# File lib/evvnt/path_helpers.rb, line 76
def plural_resource_path
  @plural_resource_path ||= default_resource_path
end
singular_path_for_record(record_id, _params) click to toggle source

Template method for fetching mine records from the API.

record_id - A String or Integer ID for the record we're fetching from the API. params - A Hash of parameters to send to the API.

Returns String

# File lib/evvnt/path_helpers.rb, line 86
def singular_path_for_record(record_id, _params)
  singular_resource_path.gsub(/\:id/, record_id.to_s)
end
singular_resource_path() click to toggle source

The API path for a single resource on the API

Returns String

# File lib/evvnt/path_helpers.rb, line 71
def singular_resource_path
  return resource_name if singular_resource?
  "#{resource_name}/:id"
end