Class: RabbitMQ::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbitmq/http/client.rb,
lib/rabbitmq/http/client/version.rb

Constant Summary collapse

VERSION =
"1.3.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, options = {}) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
29
# File 'lib/rabbitmq/http/client.rb', line 24

def initialize(endpoint, options = {})
  @endpoint = endpoint
  @options  = options

  initialize_connection(endpoint, options)
end

Instance Attribute Details

#endpointObject (readonly)

API



18
19
20
# File 'lib/rabbitmq/http/client.rb', line 18

def endpoint
  @endpoint
end

Class Method Details

.connect(endpoint, options = {}) ⇒ Object



20
21
22
# File 'lib/rabbitmq/http/client.rb', line 20

def self.connect(endpoint, options = {})
  new(endpoint, options)
end

Instance Method Details

#aliveness_test(vhost) ⇒ Object



365
366
367
368
# File 'lib/rabbitmq/http/client.rb', line 365

def aliveness_test(vhost)
  r = @connection.get("/api/aliveness-test/#{uri_encode(vhost)}")
  r.body["status"] == "ok"
end

#bind_queue(vhost, queue, exchange, routing_key, arguments = []) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/rabbitmq/http/client.rb', line 204

def bind_queue(vhost, queue, exchange, routing_key, arguments = [])
  resp = @connection.post("/api/bindings/#{uri_encode(vhost)}/e/#{uri_encode(exchange)}/q/#{uri_encode(queue)}") do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = MultiJson.dump({:routing_key => routing_key, :arguments => arguments})
  end
  resp.headers['location']
end

#channel_info(name) ⇒ Object



99
100
101
# File 'lib/rabbitmq/http/client.rb', line 99

def channel_info(name)
  decode_resource(@connection.get("/api/channels/#{uri_encode(name)}"))
end

#clear_parameters_of(component, vhost, name) ⇒ Object



359
360
361
# File 'lib/rabbitmq/http/client.rb', line 359

def clear_parameters_of(component, vhost, name)
  decode_resource(@connection.delete("/api/parameters/#{uri_encode(component)}/#{uri_encode(vhost)}/#{uri_encode(name)}"))
end

#clear_permissions_of(vhost, user) ⇒ Object



262
263
264
# File 'lib/rabbitmq/http/client.rb', line 262

def clear_permissions_of(vhost, user)
  decode_resource(@connection.delete("/api/permissions/#{uri_encode(vhost)}/#{uri_encode(user)}"))
end

#clear_policies_of(vhost, name) ⇒ Object



326
327
328
# File 'lib/rabbitmq/http/client.rb', line 326

def clear_policies_of(vhost, name)
  decode_resource(@connection.delete("/api/policies/#{uri_encode(vhost)}/#{uri_encode(name)}"))
end

#close_connection(name) ⇒ Object



91
92
93
# File 'lib/rabbitmq/http/client.rb', line 91

def close_connection(name)
  decode_resource(@connection.delete("/api/connections/#{uri_encode(name)}"))
end

#connection_info(name) ⇒ Object



87
88
89
# File 'lib/rabbitmq/http/client.rb', line 87

def connection_info(name)
  decode_resource(@connection.get("/api/connections/#{uri_encode(name)}"))
end

#create_vhost(name) ⇒ Object



227
228
229
230
231
232
# File 'lib/rabbitmq/http/client.rb', line 227

def create_vhost(name)
  response = @connection.put("/api/vhosts/#{uri_encode(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
  end
  decode_resource(response)
end

#declare_exchange(vhost, name, attributes = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rabbitmq/http/client.rb', line 113

def declare_exchange(vhost, name, attributes = {})
  opts = {
    :type => "direct",
    :auto_delete => false,
    :durable => true,
    :arguments => {}
  }.merge(attributes)

  response = @connection.put("/api/exchanges/#{uri_encode(vhost)}/#{uri_encode(name)}") do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = MultiJson.dump(opts)
  end
  decode_resource(response)
end

#declare_queue(vhost, name, attributes) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/rabbitmq/http/client.rb', line 158

def declare_queue(vhost, name, attributes)
  response = @connection.put("/api/queues/#{uri_encode(vhost)}/#{uri_encode(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#delete_exchange(vhost, name) ⇒ Object



128
129
130
# File 'lib/rabbitmq/http/client.rb', line 128

def delete_exchange(vhost, name)
  decode_resource(@connection.delete("/api/exchanges/#{uri_encode(vhost)}/#{uri_encode(name)}"))
end

#delete_queue(vhost, name) ⇒ Object



166
167
168
# File 'lib/rabbitmq/http/client.rb', line 166

def delete_queue(vhost, name)
  decode_resource(@connection.delete("/api/queues/#{uri_encode(vhost)}/#{uri_encode(name)}"))
end

#delete_queue_binding(vhost, queue, exchange, properties_key) ⇒ Object



212
213
214
215
# File 'lib/rabbitmq/http/client.rb', line 212

def delete_queue_binding(vhost, queue, exchange, properties_key)
  resp = @connection.delete("/api/bindings/#{uri_encode(vhost)}/e/#{uri_encode(exchange)}/q/#{uri_encode(queue)}/#{uri_encode(properties_key)}")
  resp.success?
end

#delete_user(name) ⇒ Object



285
286
287
# File 'lib/rabbitmq/http/client.rb', line 285

def delete_user(name)
  decode_resource(@connection.delete("/api/users/#{uri_encode(name)}"))
end

#delete_vhost(name) ⇒ Object



234
235
236
# File 'lib/rabbitmq/http/client.rb', line 234

def delete_vhost(name)
  decode_resource(@connection.delete("/api/vhosts/#{uri_encode(name)}"))
end

#enabled_protocolsArray<String>

Returns a list of messaging protocols supported by the node (or cluster).

Common values are:

  • amqp

  • amqp/ssl

  • mqtt

  • stomp

The exact value depends on RabbitMQ configuration and enabled plugins.

Returns:

  • (Array<String>)

    Enabled protocols



49
50
51
52
53
# File 'lib/rabbitmq/http/client.rb', line 49

def enabled_protocols
  self.overview.listeners.
    map { |lnr| lnr.protocol }.
    uniq
end

#exchange_info(vhost, name) ⇒ Object



132
133
134
# File 'lib/rabbitmq/http/client.rb', line 132

def exchange_info(vhost, name)
  decode_resource(@connection.get("/api/exchanges/#{uri_encode(vhost)}/#{uri_encode(name)}"))
end

#get_messages(vhost, name, options) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/rabbitmq/http/client.rb', line 178

def get_messages(vhost, name, options)
  response = @connection.post("/api/queues/#{uri_encode(vhost)}/#{uri_encode(name)}/get") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(options)
  end
  decode_resource_collection(response)
end

#list_bindings(vhost = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/rabbitmq/http/client.rb', line 186

def list_bindings(vhost = nil)
  path = if vhost.nil?
           "/api/bindings"
         else
           "/api/bindings/#{uri_encode(vhost)}"
         end

  decode_resource_collection(@connection.get(path))
end

#list_bindings_between_queue_and_exchange(vhost, queue, exchange) ⇒ Object



196
197
198
# File 'lib/rabbitmq/http/client.rb', line 196

def list_bindings_between_queue_and_exchange(vhost, queue, exchange)
  decode_resource_collection(@connection.get("/api/bindings/#{uri_encode(vhost)}/e/#{uri_encode(exchange)}/q/#{uri_encode(queue)}"))
end

#list_bindings_by_destination(vhost, exchange) ⇒ Object



140
141
142
# File 'lib/rabbitmq/http/client.rb', line 140

def list_bindings_by_destination(vhost, exchange)
  decode_resource_collection(@connection.get("/api/exchanges/#{uri_encode(vhost)}/#{uri_encode(exchange)}/bindings/destination"))
end

#list_bindings_by_source(vhost, exchange) ⇒ Object



136
137
138
# File 'lib/rabbitmq/http/client.rb', line 136

def list_bindings_by_source(vhost, exchange)
  decode_resource_collection(@connection.get("/api/exchanges/#{uri_encode(vhost)}/#{uri_encode(exchange)}/bindings/source"))
end

#list_channelsObject



95
96
97
# File 'lib/rabbitmq/http/client.rb', line 95

def list_channels
  decode_resource_collection(@connection.get("/api/channels"))
end

#list_connectionsObject



83
84
85
# File 'lib/rabbitmq/http/client.rb', line 83

def list_connections
  decode_resource_collection(@connection.get("/api/connections"))
end

#list_definitionsObject



75
76
77
# File 'lib/rabbitmq/http/client.rb', line 75

def list_definitions
  decode_resource(@connection.get("/api/definitions"))
end

#list_exchanges(vhost = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/rabbitmq/http/client.rb', line 103

def list_exchanges(vhost = nil)
  path = if vhost.nil?
           "/api/exchanges"
         else
           "/api/exchanges/#{uri_encode(vhost)}"
         end

  decode_resource_collection(@connection.get(path))
end

#list_extensionsObject



71
72
73
# File 'lib/rabbitmq/http/client.rb', line 71

def list_extensions
  decode_resource_collection(@connection.get("/api/extensions"))
end

#list_nodesObject



63
64
65
# File 'lib/rabbitmq/http/client.rb', line 63

def list_nodes
  decode_resource_collection(@connection.get("/api/nodes"))
end

#list_parameters(component = nil) ⇒ Object



333
334
335
336
337
338
339
340
# File 'lib/rabbitmq/http/client.rb', line 333

def list_parameters(component = nil)
  path = if component
           "/api/parameters/#{uri_encode(component)}"
         else
           "/api/parameters"
         end
  decode_resource_collection(@connection.get(path))
end

#list_parameters_of(component, vhost, name = nil) ⇒ Object



342
343
344
345
346
347
348
349
# File 'lib/rabbitmq/http/client.rb', line 342

def list_parameters_of(component, vhost, name = nil)
  path = if name
           "/api/parameters/#{uri_encode(component)}/#{uri_encode(vhost)}/#{uri_encode(name)}"
         else
           "/api/parameters/#{uri_encode(component)}/#{uri_encode(vhost)}"
         end
  decode_resource_collection(@connection.get(path))
end

#list_permissions(vhost = nil) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/rabbitmq/http/client.rb', line 240

def list_permissions(vhost = nil)
  path = if vhost
           "/api/vhosts/#{uri_encode(vhost)}/permissions"
         else
           "/api/permissions"
         end

  decode_resource_collection(@connection.get(path))
end

#list_permissions_of(vhost, user) ⇒ Object



250
251
252
# File 'lib/rabbitmq/http/client.rb', line 250

def list_permissions_of(vhost, user)
  decode_resource(@connection.get("/api/permissions/#{uri_encode(vhost)}/#{uri_encode(user)}"))
end

#list_policies(vhost = nil) ⇒ Object



299
300
301
302
303
304
305
306
307
# File 'lib/rabbitmq/http/client.rb', line 299

def list_policies(vhost = nil)
  path = if vhost
           "/api/policies/#{uri_encode(vhost)}"
         else
           "/api/policies"
         end

  decode_resource_collection(@connection.get(path))
end

#list_policies_of(vhost, name = nil) ⇒ Object



309
310
311
312
313
314
315
316
# File 'lib/rabbitmq/http/client.rb', line 309

def list_policies_of(vhost, name = nil)
  path = if name
           "/api/policies/#{uri_encode(vhost)}/#{uri_encode(name)}"
         else
           "/api/policies/#{uri_encode(vhost)}"
         end
  decode_resource_collection(@connection.get(path))
end

#list_queue_bindings(vhost, queue) ⇒ Object



170
171
172
# File 'lib/rabbitmq/http/client.rb', line 170

def list_queue_bindings(vhost, queue)
  decode_resource_collection(@connection.get("/api/queues/#{uri_encode(vhost)}/#{uri_encode(queue)}/bindings"))
end

#list_queues(vhost = nil) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/rabbitmq/http/client.rb', line 144

def list_queues(vhost = nil)
  path = if vhost.nil?
           "/api/queues"
         else
           "/api/queues/#{uri_encode(vhost)}"
         end

  decode_resource_collection(@connection.get(path))
end

#list_usersObject



268
269
270
# File 'lib/rabbitmq/http/client.rb', line 268

def list_users
  decode_resource_collection(@connection.get("/api/users"))
end

#list_vhostsObject



219
220
221
# File 'lib/rabbitmq/http/client.rb', line 219

def list_vhosts
  decode_resource_collection(@connection.get("/api/vhosts"))
end

#node_info(name) ⇒ Object



67
68
69
# File 'lib/rabbitmq/http/client.rb', line 67

def node_info(name)
  decode_resource(@connection.get("/api/nodes/#{uri_encode(name)}"))
end

#overviewObject



31
32
33
# File 'lib/rabbitmq/http/client.rb', line 31

def overview
  decode_resource(@connection.get("/api/overview"))
end

#protocol_portsHash<String, Integer>

Returns a hash of protocol => port.

Returns:

  • (Hash<String, Integer>)

    Hash of protocol => port



58
59
60
61
# File 'lib/rabbitmq/http/client.rb', line 58

def protocol_ports
  self.overview.listeners.
    reduce(Hash.new) { |acc, lnr| acc[lnr.protocol] = lnr.port; acc }
end

#purge_queue(vhost, name) ⇒ Object



174
175
176
# File 'lib/rabbitmq/http/client.rb', line 174

def purge_queue(vhost, name)
  decode_resource(@connection.delete("/api/queues/#{uri_encode(vhost)}/#{uri_encode(name)}/contents"))
end

#queue_binding_info(vhost, queue, exchange, properties_key) ⇒ Object



200
201
202
# File 'lib/rabbitmq/http/client.rb', line 200

def queue_binding_info(vhost, queue, exchange, properties_key)
  decode_resource(@connection.get("/api/bindings/#{uri_encode(vhost)}/e/#{uri_encode(exchange)}/q/#{uri_encode(queue)}/#{uri_encode(properties_key)}"))
end

#queue_info(vhost, name) ⇒ Object



154
155
156
# File 'lib/rabbitmq/http/client.rb', line 154

def queue_info(vhost, name)
  decode_resource(@connection.get("/api/queues/#{uri_encode(vhost)}/#{uri_encode(name)}"))
end

#update_parameters_of(component, vhost, name, attributes) ⇒ Object



351
352
353
354
355
356
357
# File 'lib/rabbitmq/http/client.rb', line 351

def update_parameters_of(component, vhost, name, attributes)
  response = @connection.put("/api/parameters/#{uri_encode(component)}/#{uri_encode(vhost)}/#{uri_encode(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#update_permissions_of(vhost, user, attributes) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/rabbitmq/http/client.rb', line 254

def update_permissions_of(vhost, user, attributes)
  response = @connection.put("/api/permissions/#{uri_encode(vhost)}/#{uri_encode(user)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#update_policies_of(vhost, name, attributes) ⇒ Object



318
319
320
321
322
323
324
# File 'lib/rabbitmq/http/client.rb', line 318

def update_policies_of(vhost, name, attributes)
  response = @connection.put("/api/policies/#{uri_encode(vhost)}/#{uri_encode(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#update_user(name, attributes) ⇒ Object Also known as: create_user



276
277
278
279
280
281
282
# File 'lib/rabbitmq/http/client.rb', line 276

def update_user(name, attributes)
  response = @connection.put("/api/users/#{uri_encode(name)}") do |req|
    req.headers['Content-Type'] = "application/json"
    req.body = MultiJson.dump(attributes)
  end
  decode_resource(response)
end

#upload_definitions(defs) ⇒ Object

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/rabbitmq/http/client.rb', line 79

def upload_definitions(defs)
  raise NotImplementedError.new
end

#user_info(name) ⇒ Object



272
273
274
# File 'lib/rabbitmq/http/client.rb', line 272

def (name)
  decode_resource(@connection.get("/api/users/#{uri_encode(name)}"))
end

#user_permissions(name) ⇒ Object



289
290
291
# File 'lib/rabbitmq/http/client.rb', line 289

def user_permissions(name)
  decode_resource_collection(@connection.get("/api/users/#{uri_encode(name)}/permissions"))
end

#vhost_info(name) ⇒ Object



223
224
225
# File 'lib/rabbitmq/http/client.rb', line 223

def vhost_info(name)
  decode_resource(@connection.get("/api/vhosts/#{uri_encode(name)}"))
end

#whoamiObject



293
294
295
# File 'lib/rabbitmq/http/client.rb', line 293

def whoami
  decode_resource(@connection.get("/api/whoami"))
end