Class: RESTRack::Balancer

Inherits:
Object
  • Object
show all
Defined in:
lib/restrack-balancer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_list, format = :JSON) ⇒ Balancer

Returns a new instance of Balancer.



9
10
11
12
13
14
15
16
17
18
# File 'lib/restrack-balancer.rb', line 9

def initialize(uri_list, format=:JSON)
  raise 'You must supply an array as the list of URI to RESTRack::Balancer#initialize.' unless uri_list.is_a? Array
  @clients = uri_list.collect do |uri|
    RESTRack::Client.new(uri, format)
  end
  @path = ''
  @uri = uri_list
  @last_client = nil
  @next_client_i = rand(@clients.length)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(resource_name, *args) ⇒ Object



20
21
22
23
# File 'lib/restrack-balancer.rb', line 20

def method_missing(resource_name, *args)
  @clients[@next_client_i].__send__(resource_name.to_sym, *args)
  self
end

Instance Attribute Details

#clientsObject (readonly)

Returns the value of attribute clients.



7
8
9
# File 'lib/restrack-balancer.rb', line 7

def clients
  @clients
end

#last_clientObject (readonly)

Returns the value of attribute last_client.



7
8
9
# File 'lib/restrack-balancer.rb', line 7

def last_client
  @last_client
end

Instance Method Details

#deleteObject



31
32
33
34
35
# File 'lib/restrack-balancer.rb', line 31

def delete
  response = @clients[@next_client_i].delete
  get_next_client
  response
end

#getObject



25
26
27
28
29
# File 'lib/restrack-balancer.rb', line 25

def get
  response = @clients[@next_client_i].get
  get_next_client
  response
end

#post(data = nil) ⇒ Object



37
38
39
40
41
# File 'lib/restrack-balancer.rb', line 37

def post(data=nil)
  response = @clients[@next_client_i].post(data)
  get_next_client
  response
end

#put(data = nil) ⇒ Object



43
44
45
46
47
# File 'lib/restrack-balancer.rb', line 43

def put(data=nil)
  response = @clients[@next_client_i].put(data)
  get_next_client
  response
end