class Telnyx::APIResource

Attributes

descendants[R]
save_with_parent[RW]

A flag that can be set a behavior that will cause this resource to be encoded and sent up along with an update of its parent resource. This is usually not desirable because resources are updated individually on their own endpoints, but there are certain cases where this is allowed.

Public Class Methods

class_name() click to toggle source
# File lib/telnyx/api_resource.rb, line 23
def self.class_name
  name.split("::")[-1]
end
identified_resource_url(id) click to toggle source
# File lib/telnyx/api_resource.rb, line 39
def self.identified_resource_url(id)
  return "/v2/#{resource_path(id)}" if respond_to?("resource_path")

  "#{resource_url}/#{CGI.escape(id)}"
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/telnyx/api_resource.rb, line 14
def inherited(subclass)
  super
  @descendants ||= []
  @descendants << subclass
end
resource_url(inner_id = nil) click to toggle source
# File lib/telnyx/api_resource.rb, line 27
def self.resource_url(inner_id = nil)
  if self == APIResource
    raise NotImplementedError, "APIResource is an abstract class. You should perform actions on its subclasses"
  end
  # Namespaces are separated in object names with periods (.) and in URLs
  # with forward slashes (/), so replace the former with the latter.
  return "/v2/#{resource_path(inner_id)}" if respond_to?("resource_path")
  return "/v2/#{self::RESOURCE_PATH}" if const_defined?("RESOURCE_PATH")

  "/v2/#{self::OBJECT_NAME.downcase.tr('.', '/')}s"
end
retrieve(id, opts = {}) click to toggle source
# File lib/telnyx/api_resource.rb, line 83
def self.retrieve(id, opts = {})
  opts = Util.normalize_opts(opts)
  instance = new(id, **opts)
  instance.refresh
  instance
end
save_nested_resource(name) click to toggle source

A metaprogramming call that specifies that a field of a resource can be its own type of API resource (say a nested card under an account for example), and if that resource is set, it should be transmitted to the API on a create or update. Doing so is not the default behavior because API resources should normally be persisted on their own RESTful endpoints.

Calls superclass method
# File lib/telnyx/api_resource.rb, line 51
def self.save_nested_resource(name)
  define_method(:"#{name}=") do |value|
    super(value)

    # The parent setter will perform certain useful operations like
    # converting to an APIResource if appropriate. Refresh our argument
    # value to whatever it mutated to.
    value = send(name)

    # Note that the value may be subresource, but could also be a scalar
    # (like a tokenized card's ID for example), so we check the type before
    # setting #save_with_parent here.
    value.save_with_parent = true if value.is_a?(APIResource)

    value
  end
end

Public Instance Methods

refresh() click to toggle source
# File lib/telnyx/api_resource.rb, line 78
def refresh
  resp, opts = request(:get, resource_url, @retrieve_params, @opts)
  initialize_from(resp.data[:data], opts)
end
resource_url() click to toggle source
# File lib/telnyx/api_resource.rb, line 69
def resource_url
  unless (id = self["id"])
    raise InvalidRequestError, "Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}"
  end
  return self.class.resource_url(id).to_s if self.class.respond_to?("resource_path") # Use resource_path defined paths

  "#{self.class.resource_url}/#{CGI.escape(id)}"
end