rufus-jig 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.swp
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,13 @@
2
2
  = rufus-jig CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-jig - 0.1.5 not yet released
6
+
7
+ - returning true (not raising) in case of 409 conflict
8
+ - using jeweler (thanks Kenneth)
9
+ - made jig/patron really thread-safe
10
+
11
+
5
12
  == rufus-jig - 0.1.4 released 2009/12/15
6
13
 
7
14
  - made jig/patron thread-safe
data/Rakefile ADDED
@@ -0,0 +1,84 @@
1
+
2
+
3
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
4
+ require 'lib/rufus/jig.rb'
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+
9
+
10
+ #
11
+ # CLEAN
12
+
13
+ require 'rake/clean'
14
+ CLEAN.include('pkg', 'tmp', 'html')
15
+ task :default => [ :clean ]
16
+
17
+
18
+ #
19
+ # GEM
20
+
21
+ require 'jeweler'
22
+
23
+ Jeweler::Tasks.new do |gem|
24
+
25
+ gem.version = Rufus::Jig::VERSION
26
+ gem.name = 'rufus-jig'
27
+ gem.summary = 'An HTTP client, greedy with JSON content, GETting conditionally.'
28
+
29
+ gem.description = %{
30
+ Json Internet Get.
31
+
32
+ An HTTP client, greedy with JSON content, GETting conditionally.
33
+
34
+ Uses Patron and Yajl-ruby whenever possible.
35
+ }
36
+ gem.email = 'jmettraux@gmail.com'
37
+ gem.homepage = 'http://github.com/jmettraux/rufus-jig/'
38
+ gem.authors = [ 'John Mettraux', 'Kenneth Kalmer' ]
39
+ gem.rubyforge_project = 'rufus'
40
+
41
+ gem.test_file = 'test/test.rb'
42
+
43
+ #gem.add_dependency 'yajl-ruby'
44
+ #gem.add_dependency 'json'
45
+ gem.add_development_dependency 'yard', '>= 0'
46
+
47
+ # gemspec spec : http://www.rubygems.org/read/chapter/20
48
+ end
49
+ Jeweler::GemcutterTasks.new
50
+
51
+
52
+ #
53
+ # DOC
54
+
55
+ begin
56
+
57
+ require 'yard'
58
+
59
+ YARD::Rake::YardocTask.new do |doc|
60
+ doc.options = [
61
+ '-o', 'html/rufus-jig', '--title',
62
+ "rufus-jig #{Rufus::Jig::VERSION}"
63
+ ]
64
+ end
65
+
66
+ rescue LoadError
67
+
68
+ task :yard do
69
+ abort "YARD is not available : sudo gem install yard"
70
+ end
71
+ end
72
+
73
+
74
+ #
75
+ # TO THE WEB
76
+
77
+ task :upload_website => [ :clean, :yard ] do
78
+
79
+ account = 'jmettraux@rubyforge.org'
80
+ webdir = '/var/www/gforge-projects/rufus'
81
+
82
+ sh "rsync -azv -e ssh html/rufus-jig #{account}:#{webdir}/"
83
+ end
84
+
data/TODO.txt CHANGED
@@ -7,17 +7,17 @@
7
7
  [o] POST/PUT cache if etag is given back (couch)
8
8
  [o] get('/x', :cache => false)
9
9
  [o] get('x') vs get('/x') ?
10
+ [o] Net::HTTP (when no Patron)
11
+ [x] couch : attachments
12
+ [o] put_doc(h) where _id is set ?
13
+ [o] :update_rev => true...
10
14
 
11
15
  [ ] long polling/continuous change (patron ?)
12
16
  [ ] HEAD
13
17
 
14
- [ ] Net::HTTP (when no Patron)
15
-
16
- [ ] couch : attachments
17
-
18
18
  [ ] redirections ? (Patron seem to understand them)
19
19
 
20
- [ ] put_doc(h) where _id is set ?
20
+ [ ] don't abuse exceptions ! (especially with couch)
21
21
 
22
- [ ] :update_rev => true...
22
+ [ ] patron : http can steal each other's patron session (inside the same thread)
23
23
 
@@ -51,24 +51,16 @@ module Rufus::Jig
51
51
 
52
52
  path = adjust(path)
53
53
 
54
- begin
54
+ r = @http.put(path, payload, :content_type => :json, :cache => false)
55
55
 
56
- r = @http.put(path, payload, :content_type => :json, :cache => false)
56
+ return true if r == true
57
+ # conflict
57
58
 
58
- if opts[:update_rev] && doc_or_path.is_a?(Hash)
59
- doc_or_path['_rev'] = r['rev']
60
- end
61
-
62
- nil
63
-
64
- rescue Rufus::Jig::HttpError => he
65
-
66
- if he.status == 409
67
- true
68
- else
69
- raise he
70
- end
59
+ if opts[:update_rev] && doc_or_path.is_a?(Hash)
60
+ doc_or_path['_rev'] = r['rev']
71
61
  end
62
+
63
+ nil
72
64
  end
73
65
 
74
66
  def get (doc_or_path)
@@ -89,34 +81,23 @@ module Rufus::Jig
89
81
 
90
82
  doc_or_path = { '_id' => doc_or_path, '_rev' => rev } if rev
91
83
 
92
- begin
93
-
94
- if doc_or_path.is_a?(String)
84
+ r = if doc_or_path.is_a?(String)
95
85
 
96
- @http.delete(adjust(doc_or_path))
86
+ @http.delete(adjust(doc_or_path))
97
87
 
98
- else
99
-
100
- raise(
101
- ArgumentError.new("cannot delete document without _rev")
102
- ) unless doc_or_path['_rev']
103
-
104
- path = adjust(doc_or_path['_id'])
105
- path = Rufus::Jig::Path.add_params(path, :rev => doc_or_path['_rev'])
106
-
107
- @http.delete(path)
108
- end
88
+ else
109
89
 
110
- nil
90
+ raise(
91
+ ArgumentError.new("cannot delete document without _rev")
92
+ ) unless doc_or_path['_rev']
111
93
 
112
- rescue Rufus::Jig::HttpError => he
94
+ path = adjust(doc_or_path['_id'])
95
+ path = Rufus::Jig::Path.add_params(path, :rev => doc_or_path['_rev'])
113
96
 
114
- if he.status == 409
115
- true
116
- else
117
- raise he
118
- end
97
+ @http.delete(path)
119
98
  end
99
+
100
+ r == true ? true : nil
120
101
  end
121
102
 
122
103
  protected
@@ -162,6 +162,7 @@ module Rufus::Jig
162
162
 
163
163
  return Rufus::Jig.marshal_copy(cached.last) if r.status == 304
164
164
  return nil if method == :get && r.status == 404
165
+ return true if r.status == 409
165
166
 
166
167
  raise @error_class.new(r.status, r.body) \
167
168
  if r.status >= 400 && r.status < 600
@@ -297,7 +298,9 @@ if defined?(Patron) # gem install patron
297
298
  #
298
299
  def get_patron
299
300
 
300
- patron = Thread.current["#{self.class}_patron"]
301
+ k = "#{self.class} #{self.object_id}"
302
+
303
+ patron = Thread.current[k]
301
304
 
302
305
  return patron if patron
303
306
 
@@ -306,9 +309,11 @@ if defined?(Patron) # gem install patron
306
309
 
307
310
  patron.headers['User-Agent'] =
308
311
  @options[:user_agent] ||
309
- "#{self.class} #{Rufus::Jig::VERSION} #{Thread.current.object_id} (patron)"
312
+ [
313
+ self.class, Rufus::Jig::VERSION, Thread.current.object_id, '(patron)'
314
+ ].join(' ')
310
315
 
311
- Thread.current["#{self.class}_patron"] = patron
316
+ Thread.current[k] = patron
312
317
  end
313
318
 
314
319
  def do_get (path, data, opts)
data/lib/rufus/jig.rb CHANGED
@@ -29,7 +29,7 @@ module Jig
29
29
  require 'rufus/jig/http'
30
30
  require 'rufus/jig/json'
31
31
 
32
- VERSION = '0.1.4'
32
+ VERSION = '0.1.5'
33
33
 
34
34
  autoload :Couch, 'rufus/jig/couch'
35
35
  autoload :CouchError, 'rufus/jig/couch'
data/rufus-jig.gemspec ADDED
@@ -0,0 +1,77 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rufus-jig}
8
+ s.version = "0.1.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Mettraux", "Kenneth Kalmer"]
12
+ s.date = %q{2009-12-17}
13
+ s.description = %q{
14
+ Json Internet Get.
15
+
16
+ An HTTP client, greedy with JSON content, GETting conditionally.
17
+
18
+ Uses Patron and Yajl-ruby whenever possible.
19
+ }
20
+ s.email = %q{jmettraux@gmail.com}
21
+ s.extra_rdoc_files = [
22
+ "LICENSE.txt",
23
+ "README.rdoc"
24
+ ]
25
+ s.files = [
26
+ ".gitignore",
27
+ "CHANGELOG.txt",
28
+ "CREDITS.txt",
29
+ "LICENSE.txt",
30
+ "README.rdoc",
31
+ "Rakefile",
32
+ "TODO.txt",
33
+ "lib/rufus-jig.rb",
34
+ "lib/rufus/jig.rb",
35
+ "lib/rufus/jig/couch.rb",
36
+ "lib/rufus/jig/http.rb",
37
+ "lib/rufus/jig/json.rb",
38
+ "lib/rufus/jig/path.rb",
39
+ "rufus-jig.gemspec",
40
+ "test/base.rb",
41
+ "test/couch_base.rb",
42
+ "test/ct_0_couch.rb",
43
+ "test/ct_1_couchdb.rb",
44
+ "test/server.rb",
45
+ "test/test.rb",
46
+ "test/ut_0_http_get.rb",
47
+ "test/ut_1_http_post.rb",
48
+ "test/ut_2_http_delete.rb",
49
+ "test/ut_3_http_put.rb",
50
+ "test/ut_4_http_prefix.rb",
51
+ "test/ut_5_http_misc.rb",
52
+ "test/ut_6_args.rb"
53
+ ]
54
+ s.homepage = %q{http://github.com/jmettraux/rufus-jig/}
55
+ s.rdoc_options = ["--charset=UTF-8"]
56
+ s.require_paths = ["lib"]
57
+ s.rubyforge_project = %q{rufus}
58
+ s.rubygems_version = %q{1.3.5}
59
+ s.summary = %q{An HTTP client, greedy with JSON content, GETting conditionally.}
60
+ s.test_files = [
61
+ "test/test.rb"
62
+ ]
63
+
64
+ if s.respond_to? :specification_version then
65
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
66
+ s.specification_version = 3
67
+
68
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
69
+ s.add_development_dependency(%q<yard>, [">= 0"])
70
+ else
71
+ s.add_dependency(%q<yard>, [">= 0"])
72
+ end
73
+ else
74
+ s.add_dependency(%q<yard>, [">= 0"])
75
+ end
76
+ end
77
+
data/test/base.rb ADDED
@@ -0,0 +1,57 @@
1
+
2
+ lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ $: << lib unless $:.include?(lib)
4
+
5
+ require 'rubygems'
6
+ require 'yajl'
7
+
8
+ # Our default
9
+ transport_library = 'patron'
10
+
11
+ if ARGV.include?( '--em' )
12
+ require 'openssl'
13
+ transport_library = 'em-http'
14
+ elsif ARGV.include?( '--net' )
15
+ transport_library = 'net/http'
16
+ end
17
+
18
+ require transport_library
19
+
20
+ require 'rufus/jig'
21
+
22
+ require 'test/unit'
23
+
24
+ if transport_library == 'em-http'
25
+ Thread.new { EM.run {} }
26
+ Thread.pass until EM.reactor_running?
27
+ end
28
+
29
+ #unless $test_server
30
+ #
31
+ # pss = `ps aux | grep "test\/server.rb"`
32
+ # puts pss
33
+ #
34
+ # $test_server = pss.index(' ruby ') || fork do
35
+ # #exec('ruby test/server.rb')
36
+ # exec('ruby test/server.rb > /dev/null 2>&1')
37
+ # end
38
+ # puts
39
+ # puts "...test server is at #{$test_server}"
40
+ # puts
41
+ # sleep 1
42
+ #end
43
+
44
+ begin
45
+ Rufus::Jig::Http.new('127.0.0.1', 4567).get('/document')
46
+ rescue Exception => e
47
+ puts
48
+ p e
49
+ e.backtrace.each { |l| puts l }
50
+ puts
51
+ puts "test server not running, please run :"
52
+ puts
53
+ puts " ruby test/server.rb"
54
+ puts
55
+ exit(1)
56
+ end
57
+
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ $: << lib unless $:.include?(lib)
4
+
5
+ require 'rubygems'
6
+ require 'yajl'
7
+
8
+ # Our default
9
+ transport_library = 'patron'
10
+
11
+ if ARGV.include?( '--em' )
12
+ require 'openssl'
13
+ transport_library = 'em-http'
14
+ elsif ARGV.include?( '--net' )
15
+ transport_library = 'net/http'
16
+ end
17
+
18
+ p [ :lib, transport_library ]
19
+
20
+ require transport_library
21
+
22
+ require 'rufus/jig'
23
+
24
+ require 'test/unit'
25
+
26
+ if transport_library == 'em-http'
27
+ Thread.new { EM.run {} }
28
+ Thread.pass until EM.reactor_running?
29
+ end
30
+
31
+
32
+ begin
33
+ Rufus::Jig::Http.new('127.0.0.1', 5984).get('/_all_dbs')
34
+ rescue Exception => e
35
+ p e
36
+ puts
37
+ puts "couch not running..."
38
+ puts
39
+ exit(1)
40
+ end
41
+
@@ -0,0 +1,56 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Sun Nov 1 21:42:34 JST 2009
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'couch_base')
9
+
10
+
11
+ class CtCouchTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+
15
+ begin
16
+ Rufus::Jig::Http.new('127.0.0.1', 5984).delete('/rufus_jig_test')
17
+ rescue Exception => e
18
+ #p e
19
+ end
20
+
21
+ @c = Rufus::Jig::Couch.new('127.0.0.1', 5984)
22
+ end
23
+
24
+ def test_welcome
25
+
26
+ assert_equal 'Welcome', @c.get('.')['couchdb']
27
+ end
28
+
29
+ def test_put_db
30
+
31
+ @c.put('rufus_jig_test')
32
+
33
+ assert_not_nil Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test')
34
+ end
35
+
36
+ def test_delete_db
37
+
38
+ Rufus::Jig::Http.new('127.0.0.1', 5984).put('/rufus_jig_test', '')
39
+
40
+ @c.delete('rufus_jig_test')
41
+
42
+ assert_nil Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test')
43
+ end
44
+
45
+ # def test_uuids
46
+ #
47
+ # uuids = @c.get_uuids
48
+ #
49
+ # assert_equal 1, uuids.size
50
+ #
51
+ # uuids = @c.get_uuids(5)
52
+ #
53
+ # assert_equal 5, uuids.size
54
+ # end
55
+ end
56
+
@@ -0,0 +1,127 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Sun Dec 13 15:03:36 JST 2009
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'couch_base')
9
+
10
+
11
+ class CtCouchDbTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+
15
+ begin
16
+ Rufus::Jig::Http.new('127.0.0.1', 5984).delete('/rufus_jig_test')
17
+ rescue Exception => e
18
+ #p e
19
+ end
20
+ Rufus::Jig::Http.new('127.0.0.1', 5984).put('/rufus_jig_test', '')
21
+
22
+ Rufus::Jig::Http.new('127.0.0.1', 5984).put(
23
+ '/rufus_jig_test/coffee1',
24
+ '{"_id":"coffee1","type":"ristretto"}')
25
+
26
+ @c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'rufus_jig_test')
27
+ end
28
+
29
+ def test_put_doc
30
+
31
+ r = @c.put('_id' => 'coffee0', 'type' => 'espresso')
32
+
33
+ assert_nil r
34
+
35
+ doc = Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test/coffee0')
36
+
37
+ assert_not_nil doc['_rev']
38
+ end
39
+
40
+ def test_put_doc_fail
41
+
42
+ r = @c.put('_id' => 'coffee1', 'type' => 'espresso')
43
+
44
+ assert_equal true, r
45
+ end
46
+
47
+ def test_put_update_rev
48
+
49
+ doc = { '_id' => 'soda0', 'type' => 'lemon' }
50
+
51
+ r = @c.put(doc, :update_rev => true)
52
+
53
+ assert_nil r
54
+ assert_not_nil doc['_rev']
55
+ end
56
+
57
+ def test_put_update_rev_2nd_time
58
+
59
+ @c.put({ '_id' => 'soda0b', 'type' => 'lemon' })
60
+ doc = @c.get('soda0b')
61
+ rev = doc['_rev']
62
+
63
+ r = @c.put(doc, :update_rev => true)
64
+
65
+ assert_nil r
66
+ assert_not_equal rev, doc['_rev']
67
+ end
68
+
69
+ def test_get_doc
70
+
71
+ doc = @c.get('coffee1')
72
+
73
+ assert_not_nil doc['_rev']
74
+ end
75
+
76
+ def test_get_missing_doc
77
+
78
+ doc = @c.get('tea0')
79
+
80
+ assert_nil doc
81
+ end
82
+
83
+ def test_delete_doc
84
+
85
+ doc = @c.get('coffee1')
86
+
87
+ r = @c.delete(doc)
88
+
89
+ assert_nil r
90
+
91
+ assert_nil(
92
+ Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test/coffee1'))
93
+ end
94
+
95
+ def test_delete_doc_2_args
96
+
97
+ doc = @c.get('coffee1')
98
+
99
+ @c.delete(doc['_id'], doc['_rev'])
100
+
101
+ assert_nil(
102
+ Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test/coffee1'))
103
+ end
104
+
105
+ def test_delete_doc_fail
106
+
107
+ doc = @c.get('coffee1')
108
+
109
+ rev = doc['_rev']
110
+
111
+ doc['_rev'] = rev + '99'
112
+
113
+ r = @c.delete(doc)
114
+
115
+ assert_equal true, r
116
+ end
117
+
118
+ def test_get_doc_304
119
+
120
+ doc = @c.get('coffee1')
121
+ assert_equal 200, @c.http.last_response.status
122
+
123
+ doc = @c.get('coffee1')
124
+ assert_equal 304, @c.http.last_response.status
125
+ end
126
+ end
127
+