# File lib/contextio/api/resource.rb, line 73 def primary_key self.class.primary_key end
module ContextIO::API::Resource
When ‘include`d into a class, this module provides some helper methods for various things a singular resource will need or find useful.
Attributes
(see ContextIO#api
)
@!attribute [r] with_constraints
A Hash of the constraints limiting this resource.
Public Class Methods
@private
For internal use only. Users of this gem shouldn’t be calling this directly.
@param [API] api A handle on the Context.IO API
. @param [Hash{String, Symbol => String, Numeric, Boolean}] options A Hash
of attributes describing the resource.
# File lib/contextio/api/resource.rb, line 23 def initialize(api, options = {}) @options ||= options validate_options @with_constraints = @options[:with] || {} @api = api @options.each do |key, value| key = key.to_s.gsub('-', '_') if self.class.associations.include?(key.to_sym) && value.is_a?(Array) association_class = ContextIO::API::AssociationHelpers.class_for_association_name(key.to_sym) value = association_class.new(api, self.class.association_name => self, attribute_hashes: value) end instance_variable_set("@#{key}", value) unless self.respond_to?(key) define_singleton_method(key) do instance_variable_get("@#{key}") end end end end
Private Class Methods
Make sure a Resource
has the declarative syntax handy.
# File lib/contextio/api/resource.rb, line 86 def self.included(other_mod) other_mod.extend(DeclarativeClassSyntax) end
Public Instance Methods
@!attribute [r] api_attributes
@return [{String => Numeric, String, Hash, Array, Boolean}] The
attributes returned from the API as a Hash. If it hasn't been populated, it will ask the API and populate it.
# File lib/contextio/api/resource.rb, line 66 def api_attributes @api_attributes ||= fetch_attributes end
Deletes the resource.
@return [Boolean] Whether the deletion worked or not.
# File lib/contextio/api/resource.rb, line 58 def delete api.request(:delete, resource_url)['success'] end
@!attribute [r] primary_key
@return [String, Symbol] The name of the key used to build the resource
URL.
@!attribute [r] resource_url
@return [String] The URL that will fetch attributes from the API
.
# File lib/contextio/api/resource.rb, line 51 def resource_url @resource_url ||= api.url_for(self) end
# File lib/contextio/api/resource.rb, line 78 def with(constraints) constraints.each{|i,c| constraints[i] = (c ? 1 : 0) if c == !!c } self.class.new(api, @options.merge(with: with_constraints.merge(constraints))) end
Private Instance Methods
Fetches attributes from the API
for the resource. Relies on having a handle on a ‘ContextIO::API` via an `api` method and on a `resource_url` method that returns the path for the resource.
Defines getter methods for any attributes that come back and don’t already have them. This way, if the API
expands, the gem will still let users get attributes we didn’t explicitly declare as lazy.
@return [{String => Numeric, String, Hash, Array, Boolean}] The
attributes returned from the API as a Hash. If it hasn't been populated, it will ask the API and populate it.
# File lib/contextio/api/resource.rb, line 117 def fetch_attributes api.request(:get, resource_url, with_constraints).inject({}) do |memo, (key, value)| key = key.to_s.gsub('-', '_') unless respond_to?(key) self.define_singleton_method(key) do value end end memo[key] = value memo end end
Raises ArgumentError unless the primary key or the resource URL is supplied. Use this to ensure that the initializer has or can build the right URL to fetch its self.
# File lib/contextio/api/resource.rb, line 93 def validate_options required_keys = ['resource_url', :resource_url] unless self.primary_key.nil? required_keys << primary_key.to_s required_keys << primary_key.to_sym end if (@options.keys & required_keys).empty? raise ArgumentError, "Required option missing. Make sure you have either resource_url or #{primary_key}." end end