diplomat 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3327f89ee1a1649aac458741a7dece72edd9617e
4
- data.tar.gz: d6f82af7f86a6ddf2ac9f10e3b7726317f6e6c92
3
+ metadata.gz: dbf402f2ee123f9bd5695a8faa1d1419cf504c7e
4
+ data.tar.gz: 183ea346004b5658c96dfdd9c7a023bd4a39a685
5
5
  SHA512:
6
- metadata.gz: 05b006160976a65d944efbf737a49a7177632047dac032d91ee77013dcdd983fe871635bc83daeb956cf6a3d1322c52b37e1862e553269da4b538b038695e658
7
- data.tar.gz: 7858da8128b071bd2001310a6ad5331edd229ed37050782f91b8c6903e29f3bce16dc0e0461383bcdc0b74144b745bc8a0377656c601e2dd3b40949b90e6950a
6
+ metadata.gz: 7dfd0b88b712187fe1ad8f025b410e687c6b655c60ded8e448f43d0a586ce0ea030387355f3c45f9b569b15956bc595843ca63a90c2a9fb67d0352aad245b6cf
7
+ data.tar.gz: e92caa5c03cf316657b98b3c431077e456326d574715f183ba91c084cfcc542dc81cc260b2532193e5c419b522b87facb3dc4cf1c3cd55dac92efe77a871a917
@@ -52,15 +52,14 @@ module Diplomat
52
52
  def get_all name=nil, not_found=:reject, found=:return
53
53
  url = ["/v1/event/list"]
54
54
  url += use_named_parameter("name", name)
55
+ url = concat_url url
55
56
 
56
57
  # Event list never returns 404 or blocks, but may return an empty list
57
- @raw = @conn.get concat_url url
58
+ @raw = @conn.get url
58
59
  if JSON.parse(@raw.body).count == 0
59
60
  case not_found
60
61
  when :reject
61
62
  raise Diplomat::EventNotFound, name
62
- when :wait
63
- index = @raw.headers["x-consul-index"]
64
63
  end
65
64
  else
66
65
  case found
@@ -69,17 +68,10 @@ module Diplomat
69
68
  when :return
70
69
  parse_body
71
70
  return return_payload
72
- when :wait
73
- index = @raw.headers["x-consul-index"]
74
71
  end
75
72
  end
76
73
 
77
- # Wait for first/next event
78
- url += use_named_parameter("index", index)
79
- @raw = @conn.get do |req|
80
- req.url concat_url url
81
- req.options.timeout = 86400
82
- end
74
+ @raw = wait_for_next_event(url)
83
75
  parse_body
84
76
  return_payload
85
77
  end
@@ -107,7 +99,8 @@ module Diplomat
107
99
  def get name=nil, token=:last, not_found=:wait, found=:return
108
100
  url = ["/v1/event/list"]
109
101
  url += use_named_parameter("name", name)
110
- @raw = @conn.get concat_url url
102
+ url = concat_url url
103
+ @raw = @conn.get url
111
104
  body = JSON.parse(@raw.body)
112
105
  # TODO: deal with unknown symbols, invalid indices (find_index will return nil)
113
106
  idx = case token
@@ -121,15 +114,11 @@ module Diplomat
121
114
  when :reject
122
115
  raise Diplomat::EventNotFound, name
123
116
  when :wait
124
- # Wait for next event
125
- index = @raw.headers["x-consul-index"]
126
- url += use_named_parameter("index", index)
127
- @raw = @conn.get do |req|
128
- req.url concat_url url
129
- req.options.timeout = 86400
130
- end
131
- body = JSON.parse(@raw.body)
132
- event = body.last # If it's possible for two events to arrive at once, this needs to #find again
117
+ @raw = wait_for_next_event(url)
118
+ parse_body
119
+ # If it's possible for two events to arrive at once,
120
+ # this needs to #find again:
121
+ event = @raw.last
133
122
  end
134
123
  else
135
124
  case found
@@ -140,8 +129,14 @@ module Diplomat
140
129
  end
141
130
  end
142
131
 
143
- { :value => { :name => event["Name"], :payload => Base64.decode64(event["Payload"]) },
144
- :token => event["ID"] }
132
+ {
133
+ :value => {
134
+ :name => event["Name"],
135
+ :payload => Base64.decode64(event["Payload"])
136
+ },
137
+ :token => event["ID"]
138
+ }
139
+
145
140
  end
146
141
 
147
142
 
@@ -159,5 +154,17 @@ module Diplomat
159
154
  def self.get *args
160
155
  Diplomat::Event.new.get *args
161
156
  end
157
+
158
+ private
159
+
160
+ def wait_for_next_event url
161
+ index = @raw.headers["x-consul-index"]
162
+ url = [url, use_named_parameter("index", index)].join("&")
163
+ return @conn.get do |req|
164
+ req.url concat_url url
165
+ req.options.timeout = 86400
166
+ end
167
+ end
168
+
162
169
  end
163
170
  end
@@ -25,19 +25,15 @@ module Diplomat
25
25
  # Build the API Client
26
26
  # @param api_connection [Faraday::Connection,nil] supply mock API Connection
27
27
  def start_connection api_connection=nil
28
- @conn = api_connection || Faraday.new(:url => Diplomat.configuration.url) do |faraday|
29
- faraday.adapter Faraday.default_adapter
30
- faraday.request :url_encoded
31
- faraday.response :raise_error
32
-
33
- Diplomat.configuration.middleware.each do |middleware|
34
- faraday.use middleware
35
- end
36
- end
28
+ @conn = build_connection(api_connection)
29
+ @conn_no_err = build_connection(api_connection, true)
30
+ end
37
31
 
38
- @conn_no_err = api_connection || Faraday.new(:url => Diplomat.configuration.url) do |faraday|
32
+ def build_connection(api_connection, raise_error=false)
33
+ return api_connection || Faraday.new(:url => Diplomat.configuration.url) do |faraday|
39
34
  faraday.adapter Faraday.default_adapter
40
35
  faraday.request :url_encoded
36
+ faraday.response :raise_error if raise_error
41
37
 
42
38
  Diplomat.configuration.middleware.each do |middleware|
43
39
  faraday.use middleware
@@ -3,8 +3,6 @@ require 'faraday'
3
3
 
4
4
  module Diplomat
5
5
  class Service < Diplomat::RestClient
6
- REGISTER_URL = '/v1/agent/service/register'
7
- DEREGISTER_URL = '/v1/agent/service/deregister'
8
6
 
9
7
  # Get a service by it's key
10
8
  # @param key [String] the key
@@ -14,23 +12,12 @@ module Diplomat
14
12
  # @return [OpenStruct] all data associated with the service
15
13
  def get key, scope=:first, options=nil, meta=nil
16
14
 
17
- qs = ""
18
- sep = "?"
19
- if options and options[:wait]
20
- qs = "#{qs}#{sep}wait=#{options[:wait]}"
21
- sep = "&"
22
- end
23
- if options and options[:index]
24
- qs = "#{qs}#{sep}index=#{options[:index]}"
25
- sep = "&"
26
- end
27
- if options and options[:dc]
28
- qs = "#{qs}#{sep}dc=#{options[:dc]}"
29
- sep = "&"
30
- end
15
+ url = ["/v1/catalog/service/#{key}"]
16
+ url << use_named_parameter('wait', options[:wait]) if options and options[:wait]
17
+ url << use_named_parameter('index', options[:index]) if options and options[:index]
18
+ url << use_named_parameter('dc', options[:dc]) if options and options[:dc]
31
19
 
32
-
33
- ret = @conn.get "/v1/catalog/service/#{key}#{qs}"
20
+ ret = @conn.get concat_url url
34
21
 
35
22
  if meta and ret.headers
36
23
  meta[:index] = ret.headers["x-consul-index"]
@@ -41,6 +28,7 @@ module Diplomat
41
28
  if scope == :all
42
29
  return JSON.parse(ret.body).map { |service| OpenStruct.new service }
43
30
  end
31
+
44
32
  return OpenStruct.new JSON.parse(ret.body).first
45
33
  end
46
34
 
@@ -49,7 +37,7 @@ module Diplomat
49
37
  # @return [Boolean]
50
38
  def register(definition)
51
39
  json_definition = JSON.dump(definition)
52
- register = @conn.put Service::REGISTER_URL, json_definition
40
+ register = @conn.put '/v1/agent/service/register', json_definition
53
41
  return register.status == 200
54
42
  end
55
43
 
@@ -57,7 +45,7 @@ module Diplomat
57
45
  # @param service_name [String] Service name to de-register
58
46
  # @return [Boolean]
59
47
  def deregister(service_name)
60
- deregister = @conn.get "#{Service::DEREGISTER_URL}/#{service_name}"
48
+ deregister = @conn.get "/v1/agent/service/deregister/#{service_name}"
61
49
  return deregister.status == 200
62
50
  end
63
51
 
@@ -1,3 +1,3 @@
1
1
  module Diplomat
2
- VERSION = "0.8.2"
2
+ VERSION = "0.8.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diplomat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hamelink