Class: Saddle::BaseEndpoint
- Inherits:
-
Object
- Object
- Saddle::BaseEndpoint
- 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
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#relative_path ⇒ Object
readonly
Returns the value of attribute relative_path.
-
#requester ⇒ Object
readonly
Returns the value of attribute requester.
Instance Method Summary collapse
-
#build_and_attach_node(endpoint_class, method_name) ⇒ Object
Create an endpoint instance and foist it upon this node.
-
#create_resource_endpoint(endpoint_class, resource_id) ⇒ Object
This will create a resource endpoint, based upon the parameters of this current node endpoint.
-
#delete(action, params = {}, options = {}) ⇒ Object
Provide DELETE functionality for the implementer class.
-
#endpoint_chain ⇒ Object
Get the parent chain that led to this endpoint.
-
#get(action, params = {}, options = {}) ⇒ Object
Provide GET functionality for the implementer class.
-
#initialize(requester, relative_path = nil, parent = nil) ⇒ BaseEndpoint
constructor
Each endpoint needs to have a requester in order to …
-
#path(action = nil) ⇒ Object
Get the url path for this endpoint/action combo.
- #path_array ⇒ Object
-
#post(action, params = {}, options = {}) ⇒ Object
Provide POST functionality for the implementer class.
-
#put(action, params = {}, options = {}) ⇒ Object
Provide PUT functionality for the implementer class.
- #request(method, action, params = {}, options = {}) ⇒ Object
Constructor Details
#initialize(requester, relative_path = 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=nil, parent=nil) @requester = requester @relative_path = relative_path @parent = parent.is_a?(BaseEndpoint) ? parent : nil end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
14 15 16 |
# File 'lib/saddle/endpoint.rb', line 14 def parent @parent end |
#relative_path ⇒ Object (readonly)
Returns the value of attribute relative_path.
14 15 16 |
# File 'lib/saddle/endpoint.rb', line 14 def relative_path @relative_path end |
#requester ⇒ Object (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) ⇒ Object
Create an endpoint instance and foist it upon this node
78 79 80 81 82 83 |
# File 'lib/saddle/endpoint.rb', line 78 def build_and_attach_node(endpoint_class, method_name) endpoint_instance = endpoint_class.new(@requester, method_name, self) self.instance_variable_set("@#{method_name}", endpoint_instance) self.class.class_eval { define_method(method_name) { 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
87 88 89 90 91 92 93 |
# File 'lib/saddle/endpoint.rb', line 87 def create_resource_endpoint(endpoint_class, resource_id) endpoint_class.new( @requester, (path_array + [resource_id]).join('/') # no parent so that it can free up memory ) end |
#delete(action, params = {}, options = {}) ⇒ Object
Provide DELETE functionality for the implementer class
40 41 42 |
# File 'lib/saddle/endpoint.rb', line 40 def delete(action, params={}, ={}) request(:delete, action, params, ) end |
#endpoint_chain ⇒ Object
Get the parent chain that led to this endpoint
66 67 68 69 70 71 72 73 74 |
# File 'lib/saddle/endpoint.rb', line 66 def endpoint_chain chain = [] node = self until node.nil? chain << node node = node.parent end chain.reverse end |
#get(action, params = {}, options = {}) ⇒ Object
Provide GET functionality for the implementer class
25 26 27 |
# File 'lib/saddle/endpoint.rb', line 25 def get(action, params={}, ={}) request(:get, action, params, ) end |
#path(action = nil) ⇒ Object
Get the url path for this endpoint/action combo
55 56 57 58 59 |
# File 'lib/saddle/endpoint.rb', line 55 def path(action=nil) paths = path_array paths << action unless action.nil? '/' + paths.join('/') end |
#path_array ⇒ Object
61 62 63 |
# File 'lib/saddle/endpoint.rb', line 61 def path_array endpoint_chain.map(&:relative_path).reject{|p| p.nil?} end |
#post(action, params = {}, options = {}) ⇒ Object
Provide POST functionality for the implementer class
30 31 32 |
# File 'lib/saddle/endpoint.rb', line 30 def post(action, params={}, ={}) request(:post, action, params, ) end |
#put(action, params = {}, options = {}) ⇒ Object
Provide PUT functionality for the implementer class
35 36 37 |
# File 'lib/saddle/endpoint.rb', line 35 def put(action, params={}, ={}) request(:put, action, params, ) end |
#request(method, action, params = {}, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/saddle/endpoint.rb', line 44 def request(method, action, params={}, ={}) # Augment in interesting options [:saddle] = { :call_chain => path_array, :action => action, } @requester.send(method, path(action), params, ) end |