Class: Etcd::Client
- Inherits:
-
Object
- Object
- Etcd::Client
- Defined in:
- lib/etcd/client.rb
Instance Attribute Summary collapse
-
#allow_redirect ⇒ Object
readonly
Returns the value of attribute allow_redirect.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #api_execute(path, method, params = nil) ⇒ Object
- #delete(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #key_endpoint ⇒ Object
- #leader ⇒ Object
- #machines ⇒ Object
- #redirect?(code) ⇒ Boolean
- #set(key, value, ttl = nil) ⇒ Object
- #test_and_set(key, value, prevValue, ttl = nil) ⇒ Object
- #version_prefix ⇒ Object
- #watch(key, index = nil) ⇒ Object
- #watch_endpoint ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 |
# File 'lib/etcd/client.rb', line 11 def initialize(opts={}) @host = opts[:host] || '127.0.0.1' @port = opts[:port] || 4001 @allow_redirect = opts[:allow_redirect] || true end |
Instance Attribute Details
#allow_redirect ⇒ Object (readonly)
Returns the value of attribute allow_redirect.
9 10 11 |
# File 'lib/etcd/client.rb', line 9 def allow_redirect @allow_redirect end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/etcd/client.rb', line 9 def host @host end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
9 10 11 |
# File 'lib/etcd/client.rb', line 9 def http @http end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
9 10 11 |
# File 'lib/etcd/client.rb', line 9 def port @port end |
Instance Method Details
#api_execute(path, method, params = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/etcd/client.rb', line 77 def api_execute(path, method, params=nil) http = if path=~/^http/ uri = URI.parse(path) path = uri.path Net::HTTP.new(uri.host, uri.port) else Net::HTTP.new(host, port) end case method when :get unless params.nil? encoded_params = URI.encode_www_form(params) path+= "?" + encoded_params end req = Net::HTTP::Get.new(path) when :post encoded_params = URI.encode_www_form(params) req = Net::HTTP::Post.new(path) req.body= encoded_params Log.debug("Setting body for post '#{encoded_params}'") when :delete unless params.nil? encoded_params = URI.encode_www_form(params) path+= "?" + encoded_params end req = Net::HTTP::Delete.new(path) end Log.debug("Invoking: '#{req.class}' against '#{path}") res = http.request(req) Log.debug("Response code: #{res.code}") if res.is_a?(Net::HTTPSuccess) Log.debug("Http success") res.body elsif redirect?(res.code.to_i) and allow_redirect Log.debug("Http redirect, following") api_execute(res['location'], method, params) else Log.debug("Http error") Log.debug(res.body) #res.error! res end end |
#delete(key) ⇒ Object
53 54 55 56 |
# File 'lib/etcd/client.rb', line 53 def delete(key) response = api_execute(key_endpoint + key, :delete) Hashie::Mash.new(JSON.parse(response)) end |
#get(key) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/etcd/client.rb', line 58 def get(key) response = api_execute(key_endpoint + key, :get) obj = JSON.parse(response) if obj.is_a?(Array) obj.map{|e| Hashie::Mash.new(e)} else Hashie::Mash.new(obj) end end |
#key_endpoint ⇒ Object
29 30 31 |
# File 'lib/etcd/client.rb', line 29 def key_endpoint version_prefix + '/keys' end |
#leader ⇒ Object
25 26 27 |
# File 'lib/etcd/client.rb', line 25 def leader api_execute('/leader', :get) end |
#machines ⇒ Object
21 22 23 |
# File 'lib/etcd/client.rb', line 21 def machines api_execute('/machines', :get).split(",") end |
#redirect?(code) ⇒ Boolean
124 125 126 |
# File 'lib/etcd/client.rb', line 124 def redirect?(code) (code >= 300) and (code < 400) end |
#set(key, value, ttl = nil) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/etcd/client.rb', line 45 def set(key, value, ttl=nil) path = key_endpoint + key payload = {'value' => value} payload['ttl'] = ttl unless ttl.nil? response = api_execute(path, :post, payload) Hashie::Mash.new(JSON.parse(response)) end |
#test_and_set(key, value, prevValue, ttl = nil) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/etcd/client.rb', line 37 def test_and_set(key, value, prevValue, ttl = nil) path = key_endpoint + key payload = {'value' => value, 'prevValue' => prevValue } payload['ttl'] = ttl unless ttl.nil? response = api_execute(path, :post, payload) Hashie::Mash.new(JSON.parse(response)) end |
#version_prefix ⇒ Object
17 18 19 |
# File 'lib/etcd/client.rb', line 17 def version_prefix '/v1' end |
#watch(key, index = nil) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/etcd/client.rb', line 68 def watch(key, index=nil) response = if index.nil? api_execute(watch_endpoint + key, :get) else api_execute(watch_endpoint + key, :post, {'index' => index}) end Hashie::Mash.new(JSON.parse(response)) end |
#watch_endpoint ⇒ Object
33 34 35 |
# File 'lib/etcd/client.rb', line 33 def watch_endpoint version_prefix + '/watch' end |