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.0.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



347
348
349
350
# File 'lib/rabbitmq/http/client.rb', line 347

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



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

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



341
342
343
# File 'lib/rabbitmq/http/client.rb', line 341

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



244
245
246
# File 'lib/rabbitmq/http/client.rb', line 244

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



308
309
310
# File 'lib/rabbitmq/http/client.rb', line 308

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



209
210
211
212
213
214
# File 'lib/rabbitmq/http/client.rb', line 209

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_queue(vhost, name, attributes) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/rabbitmq/http/client.rb', line 139

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_queue(vhost, name) ⇒ Object



147
148
149
# File 'lib/rabbitmq/http/client.rb', line 147

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



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

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



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

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

#delete_vhost(name) ⇒ Object



216
217
218
# File 'lib/rabbitmq/http/client.rb', line 216

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



113
114
115
# File 'lib/rabbitmq/http/client.rb', line 113

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

#get_messages(vhost, name, options) ⇒ Object



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

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



168
169
170
171
172
173
174
175
176
# File 'lib/rabbitmq/http/client.rb', line 168

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



178
179
180
# File 'lib/rabbitmq/http/client.rb', line 178

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



121
122
123
# File 'lib/rabbitmq/http/client.rb', line 121

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



117
118
119
# File 'lib/rabbitmq/http/client.rb', line 117

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



315
316
317
318
319
320
321
322
# File 'lib/rabbitmq/http/client.rb', line 315

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



324
325
326
327
328
329
330
331
# File 'lib/rabbitmq/http/client.rb', line 324

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



222
223
224
225
226
227
228
229
230
# File 'lib/rabbitmq/http/client.rb', line 222

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



232
233
234
# File 'lib/rabbitmq/http/client.rb', line 232

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

#list_policies(vhost = nil) ⇒ Object



281
282
283
284
285
286
287
288
289
# File 'lib/rabbitmq/http/client.rb', line 281

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



291
292
293
294
295
296
297
298
# File 'lib/rabbitmq/http/client.rb', line 291

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



151
152
153
# File 'lib/rabbitmq/http/client.rb', line 151

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



125
126
127
128
129
130
131
132
133
# File 'lib/rabbitmq/http/client.rb', line 125

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



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

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

#list_vhostsObject



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

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



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

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



182
183
184
# File 'lib/rabbitmq/http/client.rb', line 182

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



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

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



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

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



236
237
238
239
240
241
242
# File 'lib/rabbitmq/http/client.rb', line 236

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



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

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



258
259
260
261
262
263
264
# File 'lib/rabbitmq/http/client.rb', line 258

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



254
255
256
# File 'lib/rabbitmq/http/client.rb', line 254

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

#user_permissions(name) ⇒ Object



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

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

#vhost_info(name) ⇒ Object



205
206
207
# File 'lib/rabbitmq/http/client.rb', line 205

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

#whoamiObject



275
276
277
# File 'lib/rabbitmq/http/client.rb', line 275

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