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.
-
#client ⇒ Object
Traverse back until we find the original client.
-
#client_name ⇒ Object
Underscore name of the client.
-
#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.
13 14 15 16 17 |
# File 'lib/saddle/endpoint.rb', line 13 def initialize(requester, relative_path=nil, parent=nil) @requester = requester @relative_path = relative_path @parent = parent end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
10 11 12 |
# File 'lib/saddle/endpoint.rb', line 10 def parent @parent end |
#relative_path ⇒ Object (readonly)
Returns the value of attribute relative_path.
10 11 12 |
# File 'lib/saddle/endpoint.rb', line 10 def relative_path @relative_path end |
#requester ⇒ Object (readonly)
Returns the value of attribute requester.
10 11 12 |
# File 'lib/saddle/endpoint.rb', line 10 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
89 90 91 92 93 94 |
# File 'lib/saddle/endpoint.rb', line 89 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 |
#client ⇒ Object
Traverse back until we find the original client
74 75 76 77 78 79 80 |
# File 'lib/saddle/endpoint.rb', line 74 def client node = self while node.is_a?(BaseEndpoint) node = node.parent end node end |
#client_name ⇒ Object
Underscore name of the client
83 84 85 |
# File 'lib/saddle/endpoint.rb', line 83 def client_name ActiveSupport::Inflector.underscore(self.client.name.split('::')[-2]) end |
#create_resource_endpoint(endpoint_class, resource_id) ⇒ Object
This will create a resource endpoint, based upon the parameters of this current node endpoint
98 99 100 |
# File 'lib/saddle/endpoint.rb', line 98 def create_resource_endpoint(endpoint_class, resource_id) endpoint_class.new(@requester, resource_id, self) end |
#delete(action, params = {}, options = {}) ⇒ Object
Provide DELETE functionality for the implementer class
36 37 38 |
# File 'lib/saddle/endpoint.rb', line 36 def delete(action, params={}, ={}) request(:delete, action, params, ) end |
#endpoint_chain ⇒ Object
Get the parent chain that led to this endpoint
63 64 65 66 67 68 69 70 71 |
# File 'lib/saddle/endpoint.rb', line 63 def endpoint_chain chain = [] node = self while node.is_a?(BaseEndpoint) chain << node node = node.parent end chain.reverse end |
#get(action, params = {}, options = {}) ⇒ Object
Provide GET functionality for the implementer class
21 22 23 |
# File 'lib/saddle/endpoint.rb', line 21 def get(action, params={}, ={}) request(:get, action, params, ) end |
#path(action = nil) ⇒ Object
Get the url path for this endpoint/action combo
52 53 54 55 56 |
# File 'lib/saddle/endpoint.rb', line 52 def path(action=nil) paths = path_array paths << action unless action.nil? '/' + paths.join('/') end |
#path_array ⇒ Object
58 59 60 |
# File 'lib/saddle/endpoint.rb', line 58 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
26 27 28 |
# File 'lib/saddle/endpoint.rb', line 26 def post(action, params={}, ={}) request(:post, action, params, ) end |
#put(action, params = {}, options = {}) ⇒ Object
Provide PUT functionality for the implementer class
31 32 33 |
# File 'lib/saddle/endpoint.rb', line 31 def put(action, params={}, ={}) request(:put, action, params, ) end |
#request(method, action, params = {}, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/saddle/endpoint.rb', line 40 def request(method, action, params={}, ={}) # Augment in interesting options [:saddle] = { :client_name => self.client_name, :call_chain => path_array, :action => action, } @requester.send(method, path(action), params, ) end |