consul-rb 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7aa41c5c7cdf1de482586293d03c6d636fa2e6fd
4
- data.tar.gz: 127fa25ede589e931a54080852c8a8050c3d304b
3
+ metadata.gz: 4dba1c408a1ba96cbe19fa128f36c912f6e15747
4
+ data.tar.gz: a63f2fe473601e515155ea70c68312db8ac5a4ee
5
5
  SHA512:
6
- metadata.gz: d794949c1c7df9bead7048ae1731886d22ef733224bd925467e55fd92413d11dbb4d3edc3360f4d8eda5ba6a59b4abfc79113581bdae905ccf06143aa7e4d5bf
7
- data.tar.gz: 2063d204ccbea95806df55bca7f3da8c21b833ac52ec1b72456c58c7e992ac0844cae6e80fba61d33b47b28118dc66f70245b2032615f6f1cc13f6aa8c8f032a
6
+ metadata.gz: 5654b4a3e177648bba8a11e241a6b54463e5b1dd52f4ac3bc4eca078b2324a572ac1431d9aaf8e5dee5c828f96faae572a4562ffd0faa31c3088b49d84443189
7
+ data.tar.gz: 609103b6d2e859f6efe27f14e7380be248ed31dc1863e2ed8a0abf39ed28f5f778f5cd05ab36585eda1fcbb4ad8385863291d172eb4aa3b82bcb4403af30e24c
@@ -8,6 +8,8 @@ module Consul
8
8
  NotFoundException = Class.new(StandardError)
9
9
  ResponseException = Class.new(StandardError)
10
10
  RpcErrorException = Class.new(StandardError)
11
+ TimeoutException = Class.new(StandardError)
12
+ InavlidArgument = Class.new(StandardError)
11
13
 
12
14
  class HTTP
13
15
  def initialize(host:, port:, logger:)
@@ -16,15 +18,18 @@ module Consul
16
18
  @logger = logger
17
19
  end
18
20
 
19
- def get(request_uri)
21
+ def get(request_uri, recurse: false, raw: false)
20
22
  url = base_uri + request_uri
21
23
  logger.debug("GET #{url}")
22
24
 
23
25
  uri = URI.parse(url)
26
+ uri.query ||= ""
27
+ uri.query += "&recurse" if recurse
28
+ uri.query += "&raw" if raw
24
29
 
25
30
  response = http_request(:get, uri)
26
31
 
27
- parse_body(response)
32
+ raw ? response.body : parse_body(response)
28
33
  end
29
34
 
30
35
  def exists?(request_uri)
@@ -49,17 +54,29 @@ module Consul
49
54
  uri.query += "&index=#{index}&wait=#{wait}"
50
55
  uri.query += "&recurse" if recurse
51
56
 
57
+ wait_timeout = 0
58
+ case wait
59
+ when /^(.+)s$/
60
+ wait_timeout = $1.to_i + 5
61
+ when /^(.+)m$/
62
+ wait_timeout = ($1.to_i * 60) + 5
63
+ else
64
+ raise InavlidArgument
65
+ end
66
+
52
67
  while true do
53
68
  begin
54
69
  logger.debug("GET #{uri}")
55
70
 
56
- response = http_request(:get, uri)
71
+ response = http_request(:get, uri, nil, wait_timeout)
57
72
  if response['x-consul-index'].to_i > index
58
- return { index: response['x-consul-index'], body: parse_body(response) }
73
+ return { index: response['x-consul-index'].to_i, body: parse_body(response) }
59
74
  end
60
75
  rescue RpcErrorException
61
76
  logger.warn("Got 500 while watching #{uri}")
62
77
  sleep 5
78
+ rescue TimeoutException
79
+ logger.warn("Got timeout while watching #{uri}")
63
80
  end
64
81
  end
65
82
  end
@@ -87,23 +104,29 @@ module Consul
87
104
  JSON.parse("[#{response.body}]")[0]
88
105
  end
89
106
 
90
- def http_request(method, uri, data = nil)
107
+ def http_request(method, uri, data = nil, timeout = 60)
91
108
  method = {
92
109
  get: Net::HTTP::Get,
93
110
  put: Net::HTTP::Put,
94
111
  }.fetch(method)
95
112
 
96
- http = Net::HTTP.new(uri.host, uri.port)
97
- request = method.new(uri.request_uri)
113
+ http = Net::HTTP.new(uri.host, uri.port)
114
+ http.read_timeout = timeout
115
+ request = method.new(uri.request_uri)
98
116
  request.body = data
99
- response = http.request(request)
100
-
101
- if response.code.to_i == 404
102
- raise NotFoundException, "#{response.code} on #{uri}"
103
- elsif response.code.to_i >= 500
104
- raise RpcErrorException, "#{response.code} on #{uri}"
105
- elsif response.code.to_i >= 400
106
- raise ResponseException, "#{response.code} on #{uri}"
117
+
118
+ begin
119
+ response = http.request(request)
120
+
121
+ if response.code.to_i == 404
122
+ raise NotFoundException, "#{response.code} on #{uri}"
123
+ elsif response.code.to_i >= 500
124
+ raise RpcErrorException, "#{response.code} on #{uri}"
125
+ elsif response.code.to_i >= 400
126
+ raise ResponseException, "#{response.code} on #{uri}"
127
+ end
128
+ rescue Net::ReadTimeout
129
+ raise TimeoutException, "timeout on #{uri}"
107
130
  end
108
131
 
109
132
  response
@@ -0,0 +1,5 @@
1
+ module Consul
2
+ module KV
3
+
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  module Consul
2
2
  module Client
3
- VERSION ||= '0.0.2'
3
+ VERSION ||= '0.0.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Piavlo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-21 00:00:00.000000000 Z
11
+ date: 2015-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -35,6 +35,7 @@ files:
35
35
  - consul-rb.gemspec
36
36
  - lib/consul/client.rb
37
37
  - lib/consul/client/http.rb
38
+ - lib/consul/kv.rb
38
39
  - lib/consul/version.rb
39
40
  homepage: https://github.com/SupersonicAds/consul-rb
40
41
  licenses: