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 = nil) ⇒ Object
Create an endpoint instance and foist it upon this node Not private, but not part of the public interface for an endpoint.
-
#create_resource_endpoint(endpoint_class, resource_id) ⇒ Object
This will create a resource endpoint, based upon the parameters of this current node endpoint.
- #define_singleton_method(name, &block) ⇒ Object
-
#delete(action, params = {}, options = {}) ⇒ Object
Provide DELETE functionality for the implementer class.
-
#get(action, params = {}, options = {}) ⇒ Object
Provide GET functionality for the implementer class.
-
#initialize(requester, relative_path_override = nil, parent = nil) ⇒ BaseEndpoint
constructor
Each endpoint needs to have a requester in order to …
-
#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
Generic request wrapper.
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
#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 = nil) ⇒ Object
Create an endpoint instance and foist it upon this node Not private, but not part of the public interface for an endpoint
65 66 67 68 69 70 71 72 73 |
# File 'lib/saddle/endpoint.rb', line 65 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
58 59 60 |
# File 'lib/saddle/endpoint.rb', line 58 def create_resource_endpoint(endpoint_class, resource_id) endpoint_class.new(@requester, resource_id, self) end |
#define_singleton_method(name, &block) ⇒ Object
76 77 78 |
# File 'lib/saddle/endpoint.rb', line 76 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
51 52 53 |
# File 'lib/saddle/endpoint.rb', line 51 def delete(action, params={}, ={}) request(:delete, action, params, ) end |
#get(action, params = {}, options = {}) ⇒ Object
Provide GET functionality for the implementer class
36 37 38 |
# File 'lib/saddle/endpoint.rb', line 36 def get(action, params={}, ={}) request(:get, action, params, ) end |
#post(action, params = {}, options = {}) ⇒ Object
Provide POST functionality for the implementer class
41 42 43 |
# File 'lib/saddle/endpoint.rb', line 41 def post(action, params={}, ={}) request(:post, action, params, ) end |
#put(action, params = {}, options = {}) ⇒ Object
Provide PUT functionality for the implementer class
46 47 48 |
# File 'lib/saddle/endpoint.rb', line 46 def put(action, params={}, ={}) request(:put, action, params, ) end |
#request(method, action, params = {}, options = {}) ⇒ Object
Generic request wrapper
25 26 27 28 29 30 31 32 33 |
# File 'lib/saddle/endpoint.rb', line 25 def request(method, action, params={}, ={}) # Augment in interesting options [:saddle] ||= {} [:saddle] = { :call_chain => _path_array(), :action => action, } @requester.send(method, _path(action), params, ) end |