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.



17
18
19
20
21
# 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

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



14
15
16
# File 'lib/saddle/endpoint.rb', line 14

def parent
  @parent
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



14
15
16
# File 'lib/saddle/endpoint.rb', line 14

def relative_path
  @relative_path
end

#requesterObject (readonly)

Returns the value of attribute requester.



14
15
16
# File 'lib/saddle/endpoint.rb', line 14

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



62
63
64
65
66
67
68
69
70
# 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

#create_resource_endpoint(endpoint_class, resource_id) ⇒ Object

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



55
56
57
# File 'lib/saddle/endpoint.rb', line 55

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

#define_singleton_method(name, &block) ⇒ Object



73
74
75
# File 'lib/saddle/endpoint.rb', line 73

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



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

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

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

Provide GET functionality for the implementer class



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

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

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

Provide POST functionality for the implementer class



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

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

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

Provide PUT functionality for the implementer class



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

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

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

Generic request wrapper



25
26
27
28
29
30
# 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