carrot-top 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/carrot-top.gemspec +2 -2
  2. data/lib/carrot-top.rb +33 -12
  3. metadata +5 -5
@@ -2,12 +2,12 @@ $:.push File.expand_path("../lib", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "carrot-top"
5
- s.version = "0.0.6"
5
+ s.version = "0.0.7"
6
6
  s.authors = ["Sean Porter"]
7
7
  s.email = ["portertech@gmail.com"]
8
8
  s.homepage = "https://github.com/portertech/carrot-top"
9
9
  s.summary = "A Ruby library for querying the RabbitMQ Management API"
10
- s.description = "A Ruby library for querying the RabbitMQ Management API"
10
+ s.description = "A Ruby library for querying the RabbitMQ Management API, `top` for RabbitMQ."
11
11
 
12
12
  s.rubyforge_project = "carrot-top"
13
13
 
@@ -9,7 +9,9 @@ class CarrotTop
9
9
 
10
10
  def initialize(options={})
11
11
  [:host, :port, :user, :password].each do |option|
12
- raise "You must supply a RabbitMQ management API #{option}" if options[option].nil?
12
+ if options[option].nil?
13
+ raise ArgumentError, "You must supply a RabbitMQ management API #{option}"
14
+ end
13
15
  end
14
16
  protocol = options[:ssl] ? "https" : "http"
15
17
  credentials = "#{options[:user]}:#{options[:password]}"
@@ -18,17 +20,8 @@ class CarrotTop
18
20
  end
19
21
 
20
22
  def query_api(options={})
21
- raise "You must supply an API path" if options[:path].nil?
22
- uri = URI.parse(@rabbitmq_api + options[:path])
23
- http = Net::HTTP.new(uri.host, uri.port)
24
- if uri.scheme == "https"
25
- http.use_ssl = true
26
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
27
- end
28
- request = Net::HTTP::Get.new(uri.request_uri)
29
- request.add_field("content-type", "application/json")
30
- request.basic_auth(uri.user, uri.password)
31
- http.request(request)
23
+ raise ArgumentError, "You must supply an API path" if options[:path].nil?
24
+ fetch_uri(@rabbitmq_api + options[:path])
32
25
  end
33
26
 
34
27
  def method_missing(method, *args, &block)
@@ -39,4 +32,32 @@ class CarrotTop
39
32
  Hash.new
40
33
  end
41
34
  end
35
+
36
+ private
37
+
38
+ def fetch_uri(uri, limit=5)
39
+ raise ArgumentError, "HTTP redirect too deep" if limit == 0
40
+ url = URI.parse(uri)
41
+ http = Net::HTTP.new(url.host, url.port)
42
+ if url.scheme == "https"
43
+ http.use_ssl = true
44
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
45
+ end
46
+ request = Net::HTTP::Get.new(url.request_uri)
47
+ request.add_field("content-type", "application/json")
48
+ request.basic_auth(url.user, url.password)
49
+ response = http.request(request)
50
+ case response
51
+ when Net::HTTPSuccess
52
+ response
53
+ when Net::HTTPRedirection
54
+ redirect_url = URI.parse(response["location"])
55
+ redirect_url.user = url.user
56
+ redirect_url.password = url.password
57
+ fetch_uri(redirect_url.to_s, limit - 1)
58
+ else
59
+ response.error!
60
+ end
61
+ end
62
+
42
63
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrot-top
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sean Porter
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-10-13 00:00:00 -07:00
18
+ date: 2012-12-25 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -60,7 +60,7 @@ dependencies:
60
60
  version: "0"
61
61
  type: :development
62
62
  version_requirements: *id003
63
- description: A Ruby library for querying the RabbitMQ Management API
63
+ description: A Ruby library for querying the RabbitMQ Management API, `top` for RabbitMQ.
64
64
  email:
65
65
  - portertech@gmail.com
66
66
  executables: []