class Zaius::APIResource
Public Class Methods
class_name()
click to toggle source
# File lib/zaius/api_resource.rb, line 7 def self.class_name name.split("::")[-1] end
resource_url()
click to toggle source
# File lib/zaius/api_resource.rb, line 11 def self.resource_url if self == APIResource raise NotImplementedError, "APIResource is an abstract class. You should perform actions on its subclasses (Charge, Customer, etc.)" end # Namespaces are separated in object names with periods (.) and in URLs # with forward slashes (/), so replace the former with the latter. "/#{self::OBJECT_NAME.downcase.tr('.', '/')}s" end
retrieve(id, opts = {})
click to toggle source
# File lib/zaius/api_resource.rb, line 56 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/zaius/api_resource.rb, line 26 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/zaius/api_resource.rb, line 51 def refresh resp, opts = request(:get, resource_url, @retrieve_params) initialize_from(resp.data, opts) end
resource_url()
click to toggle source
# File lib/zaius/api_resource.rb, line 44 def resource_url unless (id = self["id"]) raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", "id") end "#{self.class.resource_url}/#{CGI.escape(id)}" end