Class: LoadBalanceClient
- Inherits:
-
Object
- Object
- LoadBalanceClient
- Defined in:
- lib/load_balance_client.rb
Class Method Summary collapse
- .delete(url) ⇒ Object
- .get(url) ⇒ Object
- .post(url, body) ⇒ Object
- .put(url, body) ⇒ Object
- .server_list ⇒ Object
- .server_list=(values) ⇒ Object
Class Method Details
.delete(url) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/load_balance_client.rb', line 53 def self.delete(url) server = @@servers[rand(@@servers.length)] begin RestClient.delete(server + url) do |response, request, result, &block| case response.code when 200 JSON.parse(response.body) else response end end rescue Errno::ECONNREFUSED => e @@servers.delete(server) return delete(url) unless @@servers.length == 0 end end |
.get(url) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/load_balance_client.rb', line 2 def self.get(url) server = @@servers[rand(@@servers.length)] begin RestClient.get(server + url) do |response, request, result, &block| case response.code when 200 JSON.parse(response.body) else response end end rescue Errno::ECONNREFUSED => e @@servers.delete(server) return get(url) unless @@servers.length == 0 end end |
.post(url, body) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/load_balance_client.rb', line 36 def self.post(url, body) server = @@servers[rand(@@servers.length)] begin RestClient.post(server + url, body) do |response, request, result, &block| case response.code when 200 JSON.parse(response.body) else response end end rescue Errno::ECONNREFUSED => e @@servers.delete(server) return post(url, body) unless @@servers.length == 0 end end |
.put(url, body) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/load_balance_client.rb', line 19 def self.put(url, body) server = @@servers[rand(@@servers.length)] begin RestClient.put(server + url, body) do |response, request, result, &block| case response.code when 200 JSON.parse(response.body) else response end end rescue Errno::ECONNREFUSED => e @@servers.delete(server) return put(url, body) unless @@servers.length == 0 end end |
.server_list ⇒ Object
75 76 77 |
# File 'lib/load_balance_client.rb', line 75 def self.server_list @@servers end |
.server_list=(values) ⇒ Object
70 71 72 73 |
# File 'lib/load_balance_client.rb', line 70 def self.server_list=(values) list = values.is_a?(Array) ? values : [values] @@servers = list end |