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.



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

#parentObject (readonly)

Returns the value of attribute parent.



14
15
16
# File 'lib/saddle/endpoint.rb', line 14

def parent
  @parent
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



14
15
16
# File 'lib/saddle/endpoint.rb', line 14

def relative_path
  @relative_path
end

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

#endpoint_chainObject

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



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