fraggle 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/fraggle.rb CHANGED
@@ -1,7 +1,10 @@
1
+ require 'fraggle/errors'
2
+ require 'fraggle/logger'
1
3
  require 'fraggle/snap'
2
4
  require 'uri'
3
5
 
4
6
  module Fraggle
7
+ extend Logger
5
8
 
6
9
  def self.connect(uri, *args)
7
10
  params = uri.gsub(/^doozer:\?/, '').split("&")
@@ -23,4 +26,27 @@ module Fraggle
23
26
  Snap.new(0, c)
24
27
  end
25
28
 
29
+ def self.addrs_for(uri)
30
+ params = uri.gsub(/^doozer:\?/, '').split("&")
31
+ addrs = []
32
+
33
+ params.each do |param|
34
+ k, v = param.split("=")
35
+ if k == "ca"
36
+ # Perform a liberal validation to weed out most mistakes
37
+ if v =~ /^[\d\w\-.]+:\d+$/
38
+ addrs << v
39
+ else
40
+ warn "invalid addr (#{v}) in #{uri}"
41
+ end
42
+ end
43
+ end
44
+
45
+ if addrs.empty?
46
+ raise NoAddrs
47
+ end
48
+
49
+ addrs
50
+ end
51
+
26
52
  end
@@ -1,3 +1,4 @@
1
+ require 'fraggle/errors'
1
2
  require 'fraggle/logger'
2
3
  require 'fraggle/meta'
3
4
  require 'fraggle/protocol'
@@ -65,13 +66,13 @@ module Fraggle
65
66
  end
66
67
 
67
68
  def session(prefix=nil, &blk)
68
- name = genkey(prefix)
69
+ name = "#{prefix}#{genkey}"
69
70
  estab = false
70
71
 
71
72
  f = Proc.new do |e|
72
73
  # If this is the first response from the server, it's go-time.
73
74
  if ! estab
74
- blk.call
75
+ blk.call(name)
75
76
  end
76
77
 
77
78
  # We've successfully established a session. Say so.
@@ -315,7 +316,7 @@ module Fraggle
315
316
 
316
317
  if ! @addr
317
318
  # We are all out of addresses to try
318
- raise "No more doozers!"
319
+ raise NoAddrs
319
320
  end
320
321
 
321
322
  host, port = @addr.split(":")
@@ -332,9 +333,8 @@ module Fraggle
332
333
  end
333
334
  end
334
335
 
335
- def genkey(prefix=nil)
336
- postfix = (0...16).map { Nibbles[rand(Nibbles.length)].chr }.join
337
- prefix ? prefix+"."+postfix : postfix
336
+ def genkey
337
+ (0...16).map { Nibbles[rand(Nibbles.length)].chr }.join
338
338
  end
339
339
 
340
340
  end
@@ -21,6 +21,16 @@ module Fraggle
21
21
  callbacks[name] = blk
22
22
  end
23
23
 
24
+ def aggr(coll=[], &blk)
25
+ valid do |item|
26
+ coll << item
27
+ end
28
+
29
+ done do
30
+ blk.call(coll)
31
+ end
32
+ end
33
+
24
34
  end
25
35
 
26
36
  end
@@ -0,0 +1,3 @@
1
+ module Fraggle
2
+ class NoAddrs < StandardError ; end
3
+ end
@@ -3,14 +3,16 @@ module Fraggle
3
3
 
4
4
  DEBUG = 0
5
5
  INFO = 1
6
- ERROR = 2
6
+ WARN = 2
7
+ ERROR = 3
7
8
 
8
- attr_accessor :writer, :level
9
+ attr_writer :writer, :level
9
10
 
10
11
  def log(lv, msg)
11
12
  label = case lv
12
13
  when DEBUG then "debug "
13
14
  when INFO then "info "
15
+ when WARN then "warn "
14
16
  when ERROR then "error "
15
17
  end
16
18
 
@@ -19,8 +21,12 @@ module Fraggle
19
21
  end
20
22
  end
21
23
 
24
+ def writer ; @writer ||= STDERR ; end
25
+ def level ; @level ||= WARN ; end
26
+
22
27
  def debug(msg) ; log(DEBUG, msg) ; end
23
28
  def info(msg) ; log(INFO, msg) ; end
29
+ def warn(msg) ; log(WARN, msg) ; end
24
30
  def error(msg) ; log(ERROR, msg) ; end
25
31
  end
26
32
  end
@@ -38,10 +38,6 @@ module Fraggle
38
38
  send_data("#{head}#{data}")
39
39
  end
40
40
 
41
- def send_data(data)
42
- super(data)
43
- end
44
-
45
41
  end
46
42
 
47
43
  end
data/lib/fraggle/snap.rb CHANGED
@@ -35,6 +35,10 @@ module Fraggle
35
35
  end
36
36
  end
37
37
 
38
+ def delete(&blk)
39
+ @c.delsnap(@id, &blk)
40
+ end
41
+
38
42
  def method_missing(*args, &blk)
39
43
  @c.__send__(*args, &blk)
40
44
  end
@@ -0,0 +1,16 @@
1
+ require 'fraggle'
2
+
3
+ class FraggleTest < Test::Unit::TestCase
4
+ def test_addrs_for
5
+ uri = "doozer:?ca=ec2-123:9999&ca=127.0.0.1:8888"
6
+ addrs = Fraggle.addrs_for(uri)
7
+ assert_equal ["ec2-123:9999", "127.0.0.1:8888"], addrs
8
+ end
9
+
10
+ def test_addrs_for_error
11
+ uri = "doozer:?"
12
+ assert_raises Fraggle::NoAddrs do
13
+ Fraggle.addrs_for(uri)
14
+ end
15
+ end
16
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 2
9
- version: 0.3.2
8
+ - 3
9
+ version: 0.3.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Blake Mizerany
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-09 00:00:00 -08:00
17
+ date: 2011-02-10 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,7 @@ files:
45
45
  - README.md
46
46
  - lib/fraggle/client.rb
47
47
  - lib/fraggle/emitter.rb
48
+ - lib/fraggle/errors.rb
48
49
  - lib/fraggle/logger.rb
49
50
  - lib/fraggle/meta.rb
50
51
  - lib/fraggle/msg.rb
@@ -57,6 +58,7 @@ files:
57
58
  - test/fraggle_client_test.rb
58
59
  - test/fraggle_protocol_test.rb
59
60
  - test/fraggle_snap_test.rb
61
+ - test/fraggle_test.rb
60
62
  has_rdoc: true
61
63
  homepage: http://github.com/bmizerany/fraggle
62
64
  licenses: []
@@ -96,3 +98,4 @@ test_files:
96
98
  - test/fraggle_client_test.rb
97
99
  - test/fraggle_protocol_test.rb
98
100
  - test/fraggle_snap_test.rb
101
+ - test/fraggle_test.rb