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
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={}, ={}) request(:delete, action, params, ) 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={}, ={}) request(:get, action, params, ) 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={}, ={}) request(:post, action, params, ) 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={}, ={}) request(:put, action, params, ) 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={}, ={}) # Augment in interesting options [:call_chain] = _path_array [:action] = action @requester.send(method, _path(action), params, ) end |