rufus-jig 0.1.17 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -71,6 +71,8 @@ posting...
71
71
  For the real thing : http://github.com/couchrest/couchrest
72
72
  There is also the excellent : http://github.com/langalex/couch_potato
73
73
 
74
+ Warning : rufus-jig >= 0.1.18 is meant for CouchDB >= 0.11.
75
+
74
76
  The class Rufus::Jig::Couch provides a get/put/delete trio that is couch-oriented. Json encoding/decoding is automatically handled as well as
75
77
 
76
78
  put and delete return nil in case of success and true (conflict) or and exception else.
@@ -110,6 +112,23 @@ put and delete return nil in case of success and true (conflict) or and exceptio
110
112
 
111
113
  Warning : Patron, as of 0.4.5 is timing out when PUTting images.
112
114
 
115
+ === on_change
116
+
117
+ Continuous feed with a 20s heartbeat :
118
+
119
+ db.on_change do |doc_id, deleted|
120
+ puts "doc #{doc_id} has been #{deleted ? 'deleted' : 'changed'}"
121
+ end
122
+
123
+ db.on_change do |doc_id, deleted, doc|
124
+ puts "doc #{doc_id} has been #{deleted ? 'deleted' : 'changed'}"
125
+ p doc
126
+ end
127
+
128
+ Please note the 3 param block, it includes the changed document (Note : this only work with CouchDB >= 0.11).
129
+
130
+ This is a blocking method (one may want to wrap it in a thread).
131
+
113
132
 
114
133
  == rdoc
115
134
 
data/Rakefile CHANGED
@@ -40,7 +40,7 @@ Jeweler::Tasks.new do |gem|
40
40
  gem.test_file = 'test/test.rb'
41
41
 
42
42
  gem.add_dependency 'rufus-lru'
43
- gem.add_dependency 'rufus-json'
43
+ gem.add_dependency 'rufus-json', '>= 0.2.1'
44
44
  gem.add_development_dependency 'rake'
45
45
  gem.add_development_dependency 'yard'
46
46
  gem.add_development_dependency 'jeweler'
@@ -22,6 +22,9 @@
22
22
  # Made in Japan.
23
23
  #++
24
24
 
25
+ require 'socket'
26
+ # for #on_change
27
+
25
28
 
26
29
  module Rufus::Jig
27
30
 
@@ -61,10 +64,10 @@ module Rufus::Jig
61
64
 
62
65
  pa = adjust(path)
63
66
 
64
- if @opts[:re_put_ok] == false && payload['_rev']
65
- rr = delete(path, payload['_rev'])
66
- return rr unless rr.nil?
67
- end
67
+ #if @opts[:re_put_ok] == false && payload['_rev']
68
+ # rr = delete(path, payload['_rev'])
69
+ # return rr unless rr.nil?
70
+ #end
68
71
 
69
72
  r = @http.put(pa, payload, :content_type => :json, :cache => false)
70
73
 
@@ -224,6 +227,49 @@ module Rufus::Jig
224
227
  @http.delete(path)
225
228
  end
226
229
 
230
+ # Watches the database for changes.
231
+ #
232
+ # db.on_change do |doc_id, deleted|
233
+ # puts "doc #{doc_id} has been #{deleted ? 'deleted' : 'changed'}"
234
+ # end
235
+ #
236
+ # db.on_change do |doc_id, deleted, doc|
237
+ # puts "doc #{doc_id} has been #{deleted ? 'deleted' : 'changed'}"
238
+ # p doc
239
+ # end
240
+ #
241
+ # This is a blocking method. One might want to wrap it inside of a Thread.
242
+ #
243
+ # Note : doc inclusion (third parameter to the block) only works with
244
+ # CouchDB >= 0.11.
245
+ #
246
+ def on_change (opts={}, &block)
247
+
248
+ query = {
249
+ 'feed' => 'continuous',
250
+ 'heartbeat' => opts[:heartbeat] || 20_000 }
251
+ query['include_docs'] = true if block.arity > 2
252
+ query = query.map { |k, v| "#{k}=#{v}" }.join('&')
253
+
254
+ socket = TCPSocket.open(@http.host, @http.port)
255
+
256
+ socket.print("GET /#{path}/_changes?#{query} HTTP/1.1\r\n")
257
+ socket.print("User-Agent: rufus-jig #{Rufus::Jig::VERSION}\r\n")
258
+ socket.print("\r\n")
259
+
260
+ loop do
261
+ data = socket.gets
262
+ break if data.nil?
263
+ data = (Rufus::Json.decode(data) rescue nil)
264
+ next unless data.is_a?(Hash)
265
+ args = [ data['id'], (data['deleted'] == true) ]
266
+ args << data['doc'] if block.arity > 2
267
+ block.call(*args)
268
+ end
269
+
270
+ on_change(opts, &block) if opts[:reconnect]
271
+ end
272
+
227
273
  protected
228
274
 
229
275
  def adjust (path)
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Rufus
3
3
  module Jig
4
- VERSION = '0.1.17'
4
+ VERSION = '0.1.18'
5
5
  end
6
6
  end
7
7
 
data/rufus-jig.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rufus-jig}
8
- s.version = "0.1.17"
8
+ s.version = "0.1.18"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Mettraux", "Kenneth Kalmer"]
12
- s.date = %q{2010-03-22}
12
+ s.date = %q{2010-05-11}
13
13
  s.description = %q{
14
14
  Json Interwebs Get.
15
15
 
@@ -48,6 +48,7 @@ Gem::Specification.new do |s|
48
48
  "test/ct_2_couchdb_options.rb",
49
49
  "test/ct_3_couchdb_views.rb",
50
50
  "test/ct_4_attachments.rb",
51
+ "test/ct_5_couchdb_continuous.rb",
51
52
  "test/server.rb",
52
53
  "test/test.rb",
53
54
  "test/to.sh",
@@ -77,7 +78,7 @@ Gem::Specification.new do |s|
77
78
 
78
79
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
79
80
  s.add_runtime_dependency(%q<rufus-lru>, [">= 0"])
80
- s.add_runtime_dependency(%q<rufus-json>, [">= 0"])
81
+ s.add_runtime_dependency(%q<rufus-json>, [">= 0.2.1"])
81
82
  s.add_development_dependency(%q<rake>, [">= 0"])
82
83
  s.add_development_dependency(%q<yard>, [">= 0"])
83
84
  s.add_development_dependency(%q<jeweler>, [">= 0"])
@@ -85,7 +86,7 @@ Gem::Specification.new do |s|
85
86
  s.add_development_dependency(%q<em-http-request>, [">= 0"])
86
87
  else
87
88
  s.add_dependency(%q<rufus-lru>, [">= 0"])
88
- s.add_dependency(%q<rufus-json>, [">= 0"])
89
+ s.add_dependency(%q<rufus-json>, [">= 0.2.1"])
89
90
  s.add_dependency(%q<rake>, [">= 0"])
90
91
  s.add_dependency(%q<yard>, [">= 0"])
91
92
  s.add_dependency(%q<jeweler>, [">= 0"])
@@ -94,7 +95,7 @@ Gem::Specification.new do |s|
94
95
  end
95
96
  else
96
97
  s.add_dependency(%q<rufus-lru>, [">= 0"])
97
- s.add_dependency(%q<rufus-json>, [">= 0"])
98
+ s.add_dependency(%q<rufus-json>, [">= 0.2.1"])
98
99
  s.add_dependency(%q<rake>, [">= 0"])
99
100
  s.add_dependency(%q<yard>, [">= 0"])
100
101
  s.add_dependency(%q<jeweler>, [">= 0"])
data/test/ct_1_couchdb.rb CHANGED
@@ -100,7 +100,11 @@ class CtCouchDbTest < Test::Unit::TestCase
100
100
 
101
101
  r = @c.put(doc)
102
102
 
103
- assert_not_nil @c.get('coffee1')['_rev']
103
+ assert_equal true, r
104
+ assert_nil @c.get('coffee1')
105
+
106
+ #assert_not_nil @c.get('coffee1')['_rev']
107
+ # CouchDB < 0.11
104
108
  end
105
109
 
106
110
  def test_get
@@ -21,12 +21,15 @@ class CtCouchDbOptionsTest < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
  h.put('/rufus_jig_test', '')
24
- h.put('/rufus_jig_test/coffee1', '{"_id":"coffee1","type":"ristretto"}')
24
+ h.put('/rufus_jig_test/coffee2_0', '{"_id":"coffee2_0","type":"ristretto"}')
25
25
 
26
26
  h.close
27
27
 
28
28
  @c = Rufus::Jig::Couch.new(
29
- '127.0.0.1', 5984, 'rufus_jig_test', :re_put_ok => false)
29
+ #'127.0.0.1', 5984, 'rufus_jig_test', :re_put_ok => false)
30
+ '127.0.0.1', 5984, 'rufus_jig_test')
31
+
32
+ # CouchDB >= 0.11 rendered re_put_ok useless
30
33
  end
31
34
 
32
35
  def teardown
@@ -36,10 +39,10 @@ class CtCouchDbOptionsTest < Test::Unit::TestCase
36
39
 
37
40
  def test_put_gone
38
41
 
39
- doc = @c.get('coffee1')
42
+ doc = @c.get('coffee2_0')
40
43
  @c.delete(doc)
41
44
 
42
- assert_nil @c.get('coffee1')
45
+ assert_nil @c.get('coffee2_0')
43
46
 
44
47
  doc['whatever'] = 'else'
45
48
 
@@ -0,0 +1,90 @@
1
+ # encoding: UTF-8
2
+
3
+ #
4
+ # testing rufus-jig
5
+ #
6
+ # Tue May 11 09:52:37 JST 2010
7
+ #
8
+
9
+ require File.join(File.dirname(__FILE__), 'couch_base')
10
+
11
+
12
+ class CtCouchDbContinuousTest < Test::Unit::TestCase
13
+
14
+ def setup
15
+
16
+ h = Rufus::Jig::Http.new('127.0.0.1', 5984)
17
+
18
+ begin
19
+ h.delete('/rufus_jig_test')
20
+ rescue Exception => e
21
+ #p e
22
+ end
23
+ h.put('/rufus_jig_test', '')
24
+ h.close
25
+
26
+ @c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'rufus_jig_test')
27
+ end
28
+
29
+ def teardown
30
+
31
+ @c.close
32
+ end
33
+
34
+ def test_on_change
35
+
36
+ stack = []
37
+
38
+ t = Thread.new do
39
+ @c.on_change { |doc_id, deleted| stack << doc_id }
40
+ end
41
+
42
+ @c.put('_id' => 'angel0', 'name' => 'samael')
43
+ @c.put('_id' => 'angel1', 'name' => 'raphael')
44
+
45
+ sleep 0.150
46
+ t.kill
47
+
48
+ assert_equal 2, stack.size
49
+ end
50
+
51
+ def test_on_change_include_docs
52
+
53
+ stack = []
54
+
55
+ Thread.abort_on_exception = true
56
+
57
+ t = Thread.new do
58
+ @c.on_change { |doc_id, deleted, doc| stack << doc }
59
+ end
60
+
61
+ @c.put('_id' => 'angel2', 'name' => 'samael')
62
+ @c.put('_id' => 'angel3', 'name' => 'ゆきひろ')
63
+
64
+ sleep 0.150
65
+ t.kill
66
+
67
+ assert_equal 'ゆきひろ', stack[1]['name']
68
+ end
69
+
70
+ def test_on_change_include_docs_with_deleted
71
+
72
+ stack = []
73
+
74
+ Thread.abort_on_exception = true
75
+
76
+ t = Thread.new do
77
+ @c.on_change { |doc_id, deleted, doc| stack << [ doc_id, deleted ] }
78
+ end
79
+
80
+ @c.put('_id' => 'angel4', 'name' => 'samael')
81
+ sleep 0.077
82
+ @c.delete(@c.get('angel4'))
83
+
84
+ sleep 0.154
85
+ t.kill
86
+
87
+ assert_equal [["angel4", false], ["angel4", true]], stack
88
+ end
89
+ end
90
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 17
9
- version: 0.1.17
8
+ - 18
9
+ version: 0.1.18
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Mettraux
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-03-22 00:00:00 +09:00
18
+ date: 2010-05-11 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -39,7 +39,9 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  segments:
41
41
  - 0
42
- version: "0"
42
+ - 2
43
+ - 1
44
+ version: 0.2.1
43
45
  type: :runtime
44
46
  version_requirements: *id002
45
47
  - !ruby/object:Gem::Dependency
@@ -137,6 +139,7 @@ files:
137
139
  - test/ct_2_couchdb_options.rb
138
140
  - test/ct_3_couchdb_views.rb
139
141
  - test/ct_4_attachments.rb
142
+ - test/ct_5_couchdb_continuous.rb
140
143
  - test/server.rb
141
144
  - test/test.rb
142
145
  - test/to.sh