class Saddle::BaseEndpoint
This base endpoint is what all implementation endpoints should inherit from. It automatically provides tree construction and traversal functionality. It also abstracts away url construction and requests to the underlying requester instance.
Attributes
Public Class Methods
Each endpoint needs to have a requester in order to … make … uh … requests.
# File lib/saddle/endpoint.rb, line 17 def initialize(requester, relative_path_override=nil, parent=nil) @requester = requester @parent = parent @relative_path = relative_path_override || _relative_path end
Public Instance Methods
Create an endpoint instance and foist it upon this node Not private, but not part of the public interface for an endpoint
# File lib/saddle/endpoint.rb, line 62 def _build_and_attach_node(endpoint_class, method_name=nil) # Create the new endpoint endpoint_instance = endpoint_class.new(@requester, method_name, self) # Attach the endpoint as an instance variable and method method_name ||= endpoint_class.name.demodulize.underscore self.instance_variable_set("@#{method_name}", endpoint_instance) self.define_singleton_method(method_name.to_s) { endpoint_instance } endpoint_instance end
This will create a resource endpoint, based upon the parameters of this current node endpoint
# File lib/saddle/endpoint.rb, line 55 def create_resource_endpoint(endpoint_class, resource_id) endpoint_class.new(@requester, resource_id, self) end
# File lib/saddle/endpoint.rb, line 73 def define_singleton_method(name, &block) (class << self; self end).send(:define_method, name, &block) end
Provide DELETE functionality for the implementer class
# File lib/saddle/endpoint.rb, line 48 def delete(action, params={}, options={}) request(:delete, action, params, options) end
Provide GET functionality for the implementer class
# File lib/saddle/endpoint.rb, line 33 def get(action, params={}, options={}) request(:get, action, params, options) end
Provide POST functionality for the implementer class
# File lib/saddle/endpoint.rb, line 38 def post(action, params={}, options={}) request(:post, action, params, options) end
Provide PUT functionality for the implementer class
# File lib/saddle/endpoint.rb, line 43 def put(action, params={}, options={}) request(:put, action, params, options) end
Generic request wrapper
# File lib/saddle/endpoint.rb, line 25 def request(method, action, params={}, options={}) # Augment in interesting options options[:call_chain] = _path_array options[:action] = action @requester.send(method, _path(action), params, options) end
Protected Instance Methods
Get the parent chain that led to this endpoint
# File lib/saddle/endpoint.rb, line 101 def _endpoint_chain chain = [] node = self while node.is_a?(BaseEndpoint) chain << node node = node.parent end chain.reverse end
If the parent is not an endpoint, it is a root node
# File lib/saddle/endpoint.rb, line 112 def _is_root? !@parent.is_a?(BaseEndpoint) end
Get the url path for this endpoint/action combo
# File lib/saddle/endpoint.rb, line 81 def _path(action=nil) # Use the absolute path if present, otherwise get the relative path pre_action_paths = if defined?(self.class::ABSOLUTE_PATH) [self.class::ABSOLUTE_PATH] else _path_array end # Join it with the action paths = pre_action_paths + [action] # Strip out empty elements paths = paths.map(&:to_s).reject(&:empty?) "/#{paths.join('/')}" end
# File lib/saddle/endpoint.rb, line 96 def _path_array _endpoint_chain.map(&:relative_path).compact end
Build the default relative path for this endpoint node
If this is a root node, use nil. Otherwise use the underscored version of the class name
Override this if needed for specific endpoints
# File lib/saddle/endpoint.rb, line 121 def _relative_path if _is_root? nil else self.class.name.demodulize.underscore end end