Class: Saddle::BaseEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/saddle/endpoint.rb

Overview

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.

Direct Known Subclasses

ResourceEndpoint, TraversalEndpoint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(requester, relative_path_override = nil, parent = nil) ⇒ BaseEndpoint

Each endpoint needs to have a requester in order to … make … uh … requests.



22
23
24
25
26
# File 'lib/saddle/endpoint.rb', line 22

def initialize(requester, relative_path_override=nil, parent=nil)
  @requester = requester
  @parent = parent
  @relative_path = relative_path_override || _relative_path
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



19
20
21
# File 'lib/saddle/endpoint.rb', line 19

def parent
  @parent
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



19
20
21
# File 'lib/saddle/endpoint.rb', line 19

def relative_path
  @relative_path
end

#requesterObject (readonly)

Returns the value of attribute requester.



19
20
21
# File 'lib/saddle/endpoint.rb', line 19

def requester
  @requester
end

Instance Method Details

#_build_and_attach_node(endpoint_class, method_name = nil) ⇒ Object

Create an endpoint instance and foist it upon this node Not private, but not part of the public interface for an endpoint



67
68
69
70
71
72
73
74
75
# File 'lib/saddle/endpoint.rb', line 67

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

#create_resource_endpoint(endpoint_class, resource_id) ⇒ Object

This will create a resource endpoint, based upon the parameters of this current node endpoint



60
61
62
# File 'lib/saddle/endpoint.rb', line 60

def create_resource_endpoint(endpoint_class, resource_id)
  endpoint_class.new(@requester, resource_id, self)
end

#define_singleton_method(name, &block) ⇒ Object



78
79
80
# File 'lib/saddle/endpoint.rb', line 78

def define_singleton_method(name, &block)
  (class << self; self end).send(:define_method, name, &block)
end

#delete(action, params = {}, options = {}) ⇒ Object

Provide DELETE functionality for the implementer class



53
54
55
# File 'lib/saddle/endpoint.rb', line 53

def delete(action, params={}, options={})
  request(:delete, action, params, options)
end

#get(action, params = {}, options = {}) ⇒ Object

Provide GET functionality for the implementer class



38
39
40
# File 'lib/saddle/endpoint.rb', line 38

def get(action, params={}, options={})
  request(:get, action, params, options)
end

#post(action, params = {}, options = {}) ⇒ Object

Provide POST functionality for the implementer class



43
44
45
# File 'lib/saddle/endpoint.rb', line 43

def post(action, params={}, options={})
  request(:post, action, params, options)
end

#put(action, params = {}, options = {}) ⇒ Object

Provide PUT functionality for the implementer class



48
49
50
# File 'lib/saddle/endpoint.rb', line 48

def put(action, params={}, options={})
  request(:put, action, params, options)
end

#request(method, action, params = {}, options = {}) ⇒ Object

Generic request wrapper



30
31
32
33
34
35
# File 'lib/saddle/endpoint.rb', line 30

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