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 = 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

#parentObject (readonly)

Returns the value of attribute parent.



10
11
12
# File 'lib/saddle/endpoint.rb', line 10

def parent
  @parent
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



10
11
12
# File 'lib/saddle/endpoint.rb', line 10

def relative_path
  @relative_path
end

#requesterObject (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

#clientObject

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_nameObject

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={}, options={})
  request(:delete, action, params, options)
end

#endpoint_chainObject

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={}, options={})
  request(:get, action, params, options)
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_arrayObject



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={}, options={})
  request(:post, action, params, options)
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={}, options={})
  request(:put, action, params, options)
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={}, options={})
  # Augment in interesting options
  options[:saddle] = {
    :client_name => self.client_name,
    :call_chain => path_array,
    :action => action,
  }
  @requester.send(method, path(action), params, options)
end