class Cubits::Resource
Public Class Methods
Loads collection of resources
@return [Array<Resource>]
# File lib/cubits/resource.rb, line 76 def self.all fail NoMethodError, "Resource #{name} does not expose .all" unless exposed_method?(:all) Cubits.connection.get(path_to)[collection_name].map { |r| new r } rescue NotFound nil end
# File lib/cubits/resource.rb, line 60 def self.belongs_to(association_name, params = {}) define_method(association_name) do association_id = send :"#{association_name}_id" unless association_id fail ArgumentError, "No #{association_name}_id attribute is defined for #{self}" end class_name = params[:class_name] || association_name.to_s.capitalize resource = Cubits.const_get(class_name) || fail("Failed to find class #{class_name}") resource.find(association_id) end end
By convention collection name for the resource is the last part of the path
# File lib/cubits/resource.rb, line 14 def self.collection_name @path.split('/').last end
Creates a new resource
# File lib/cubits/resource.rb, line 140 def self.create(params = {}) fail NoMethodError, "Resource #{name} does not expose .create" unless exposed_method?(:create) new Cubits.connection.post(path_to, params) end
Sets exposed methods for the resource
# File lib/cubits/resource.rb, line 20 def self.expose_methods(*args) @exposed_methods = args end
@return true if the method is exposed by this resource
# File lib/cubits/resource.rb, line 26 def self.exposed_method?(method_name) (@exposed_methods || []).include?(method_name) end
Loads resource
@param id [String]
@return nil if resource is not found
# File lib/cubits/resource.rb, line 89 def self.find(id) fail NoMethodError, "Resource #{name} does not expose .find" unless exposed_method?(:find) new Cubits.connection.get(path_to(id)) rescue NotFound nil end
Processes callback request parsed into separate params and instantiates a resource object on success.
@param params [Hash] @param params [String] Value of the CUBITS_CALLBACK_ID header @param params [String] Value of the CUBITS_KEY header @param params [String] Value of the CUBITS_SIGNATURE header @param params [String] Request body @param params [Boolean] (optional) Allow insecure, unsigned callbacks (default: false)
@return [Resource,Hash]
@raise [InvalidSignature] @raise [InsecureCallback]
# File lib/cubits/resource.rb, line 111 def self.from_callback(params) unless exposed_method?(:from_callback) fail NoMethodError, "Resource #{name} does not expose .from_callback" end Cubits::Callback.from_params(params.merge(resource_class: self)) end
Associations
# File lib/cubits/resource.rb, line 44 def self.has_many(association_name, params = {}) define_method(association_name) do association = instance_variable_get("@#{association_name}") return association if association class_name = params[:class_name] || association_name.to_s.capitalize.sub(/s$/, '') resource = Cubits.const_get(class_name) || fail("Failed to find class #{class_name}") association = ResourceCollection.new( path: self.class.path_to(id) + '/' + association_name.to_s, resource: resource, expose_methods: params[:expose_methods] || [:find, :all] ) instance_variable_set("@#{association_name}", association) association end end
Sets path for the resource
# File lib/cubits/resource.rb, line 8 def self.path(p) @path = p end
Returns API path to resource
# File lib/cubits/resource.rb, line 32 def self.path_to(resource_or_id = nil) fail ArgumentError, "Resource path is not set for #{name}" unless @path if resource_or_id.is_a?(Resource) "#{@path}/#{resource_or_id.id}" elsif resource_or_id "#{@path}/#{resource_or_id}" else @path end end
Public Instance Methods
Reloads resource
# File lib/cubits/resource.rb, line 120 def reload unless self.class.exposed_method?(:reload) fail NoMethodError, "Resource #{self.class.name} does not expose #reload" end fail "Resource #{self.class.name} does not have an id" unless self.respond_to?(:id) replace(self.class.find(id)) end
Updates the resource
# File lib/cubits/resource.rb, line 130 def update(params = {}) unless self.class.exposed_method?(:update) fail NoMethodError, "Resource #{self.class.name} does not expose #update" end fail "Resource #{self.class.name} does not have an id" unless self.respond_to?(:id) replace(self.class.new Cubits.connection.post(self.class.path_to(id), params)) end