lib/telnyx/api_resource.rb in telnyx-2.3.0 vs lib/telnyx/api_resource.rb in telnyx-2.4.0
- old
+ new
@@ -12,19 +12,28 @@
def self.class_name
name.split("::")[-1]
end
- def self.resource_url
+ 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.
- const_defined?("RESOURCE_PATH") ? "/v2/#{self::RESOURCE_PATH}" : "/v2/#{self::OBJECT_NAME.downcase.tr('.', '/')}s"
+ 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
+ def self.identified_resource_url(id)
+ return "/v2/#{resource_path(id)}" if respond_to?("resource_path")
+
+ "#{resource_url}/#{CGI.escape(id)}"
+ end
+
# 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
@@ -49,20 +58,22 @@
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
def refresh
resp, opts = request(:get, resource_url, @retrieve_params, @opts)
initialize_from(resp.data[:data], opts)
end
def self.retrieve(id, opts = {})
opts = Util.normalize_opts(opts)
- instance = new(id, opts)
+ instance = new(id, **opts)
instance.refresh
instance
end
end
end