rufus-jig 0.1.21 → 0.1.22

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,7 +22,6 @@
22
22
  # Made in Japan.
23
23
  #++
24
24
 
25
- require 'uri'
26
25
  require 'ostruct'
27
26
 
28
27
  require 'rufus/lru' # gem install rufus-lru
@@ -69,7 +68,7 @@ module Rufus::Jig
69
68
  attr_reader :original
70
69
  end
71
70
 
72
- URI_REGEX = /https?:\/\/([^\/]+)([^\?]*)(\?.+)?$/
71
+ URI_REGEX = /(https?):\/\/([^@]+:[^@]+@)?([^\/]+)([^\?]*)(\?.+)?$/
73
72
 
74
73
  # The current URI lib is not UTF-8 friendly, so this is a workaround.
75
74
  # Temporary hopefully.
@@ -78,23 +77,29 @@ module Rufus::Jig
78
77
 
79
78
  m = URI_REGEX.match(s)
80
79
 
81
- host, port, path, query = if m
80
+ scheme, uname, pass, host, port, path, query = if m
82
81
 
83
- h, p = m[1].split(':')
84
- p ||= 80
82
+ ho, po = m[3].split(':')
83
+ po = (po || 80).to_i
85
84
 
86
- query = m[3] ? m[3][1..-1] : nil
85
+ query = m[5] ? m[5][1..-1] : nil
87
86
 
88
- [ h, p, m[2], query ]
87
+ un, pa = m[2] ? m[2][0..-2].split(':') : [ nil, nil ]
88
+
89
+ [ m[1], un, pa, ho, po, m[4], query ]
89
90
 
90
91
  else
91
92
 
92
- p, q = s.split('?')
93
+ pa, qu = s.split('?')
93
94
 
94
- [ nil, nil, p, q ]
95
+ [ nil, nil, nil, nil, nil, pa, qu ]
95
96
  end
96
97
 
97
- OpenStruct.new(:host => host, :port => port, :path => path, :query => query)
98
+ OpenStruct.new(
99
+ :scheme => scheme,
100
+ :host => host, :port => port,
101
+ :path => path, :query => query,
102
+ :username => uname, :password => pass)
98
103
  end
99
104
 
100
105
  # The current URI lib is not UTF-8 friendly, so this is a workaround.
@@ -123,33 +128,59 @@ module Rufus::Jig
123
128
  #
124
129
  attr_reader :options
125
130
 
126
- # host and port, vanilla
131
+ # scheme, host and port, vanilla
127
132
  #
128
- attr_reader :host, :port
133
+ attr_reader :scheme, :host, :port
129
134
 
130
135
  # The class of the error that should be raised when a request is not 2xx.
131
136
  #
132
137
  attr_accessor :error_class
133
138
 
134
- def initialize (host, port, opts)
139
+ # Sometimes a URI is passed for initialization, if the URI contained a
140
+ # path, it is stored in @_path (and not used).
141
+ # Rufus::Jig::Couch uses it though.
142
+ #
143
+ attr_accessor :_path, :_query
144
+
145
+ def initialize (*args)
146
+
147
+ @options = args.last.is_a?(Hash) ? args.pop.dup : {}
148
+
149
+ if args[1].is_a?(Fixnum) # host, port[, path]
135
150
 
136
- @host = host
137
- @port = port
151
+ @scheme = 'http'
152
+ @host, @port, @_path = args
138
153
 
139
- @options = opts.dup
154
+ else # uri
155
+
156
+ u = Rufus::Jig.parse_uri(args.first)
157
+
158
+ @scheme = u.scheme
159
+ @host = u.host
160
+ @port = u.port
161
+
162
+ @options[:basic_auth] ||= [ u.username, u.password ] if u.username
163
+
164
+ if args[1]
165
+ @_path, @_query = args[1].split('?')
166
+ else
167
+ @_path = u.path
168
+ @_query = u.query
169
+ end
170
+ end
140
171
 
141
- @cache = LruHash.new((opts[:cache_size] || 35).to_i)
172
+ @cache = LruHash.new((@options[:cache_size] || 35).to_i)
142
173
 
143
174
  if pf = @options[:prefix]
144
175
  @options[:prefix] = "/#{pf}" if (not pf.match(/^\//))
145
176
  end
146
177
 
147
- @error_class = opts[:error_class] || HttpError
178
+ @error_class = @options[:error_class] || HttpError
148
179
  end
149
180
 
150
- def close
181
+ def uri
151
182
 
152
- # default implementation does nothing
183
+ OpenStruct.new(:scheme => @scheme, :host => @host, :port => @port)
153
184
  end
154
185
 
155
186
  def get (path, opts={})
@@ -335,8 +366,15 @@ module Rufus::Jig
335
366
  end
336
367
  end
337
368
 
369
+ #--
370
+ # now load an adapter
371
+ #++
372
+
373
+ if defined?(Net::HTTP::Persistent) # gem install net-http-persistent
374
+
375
+ require 'rufus/jig/adapters/net_persistent'
338
376
 
339
- if defined?(Patron) # gem install patron
377
+ elsif defined?(Patron) # gem install patron
340
378
 
341
379
  require 'rufus/jig/adapters/patron'
342
380
 
@@ -350,56 +388,3 @@ else
350
388
 
351
389
  end
352
390
 
353
- #--
354
- #
355
- # re-opening the HTTP class to add some class methods
356
- #
357
- #++
358
- class Rufus::Jig::Http
359
-
360
- # Makes sense of arguments and extract an array that goes like
361
- # [ http, path, payload, opts ].
362
- #
363
- # Typical input :
364
- #
365
- # a = Rufus::Jig::Http.extract_http(false, 'http://127.0.0.1:5984')
366
- # a = Rufus::Jig::Http.extract_http(false, '127.0.0.1', 5984, '/')
367
- # a = Rufus::Jig::Http.extract_http(true, 'http://127.0.0.1:5984', :payload)
368
- #
369
- def self.extract_http (payload_expected, *args)
370
-
371
- host, port = case args.first
372
-
373
- when Rufus::Jig::Http
374
- args.shift
375
-
376
- when /^http:\/\//
377
- u = Rufus::Jig.parse_uri(args.shift)
378
- args.unshift(u.path)
379
- [ u.host, u.port ]
380
-
381
- else
382
- [ args.shift, args.shift ]
383
- end
384
-
385
- port = port.to_i
386
-
387
- path = args.shift
388
- path = '/' if path == ''
389
-
390
- payload = payload_expected ? args.shift : nil
391
-
392
- opts = args.shift || {}
393
-
394
- raise(
395
- ArgumentError.new("option Hash expected, not #{opts.inspect}")
396
- ) unless opts.is_a?(Hash)
397
-
398
- http = host.is_a?(Rufus::Jig::Http) ?
399
- host :
400
- Rufus::Jig::Http.new(host, port, opts)
401
-
402
- [ http, path, payload, opts ]
403
- end
404
- end
405
-
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Rufus
3
3
  module Jig
4
- VERSION = '0.1.21'
4
+ VERSION = '0.1.22'
5
5
  end
6
6
  end
7
7
 
@@ -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.21"
8
+ s.version = "0.1.22"
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-09-21}
12
+ s.date = %q{2010-09-30}
13
13
  s.description = %q{
14
14
  Json Interwebs Get.
15
15
 
@@ -34,6 +34,8 @@ Gem::Specification.new do |s|
34
34
  "lib/rufus/jig.rb",
35
35
  "lib/rufus/jig/adapters/em.rb",
36
36
  "lib/rufus/jig/adapters/net.rb",
37
+ "lib/rufus/jig/adapters/net_persistent.rb",
38
+ "lib/rufus/jig/adapters/net_response.rb",
37
39
  "lib/rufus/jig/adapters/patron.rb",
38
40
  "lib/rufus/jig/couch.rb",
39
41
  "lib/rufus/jig/http.rb",
@@ -41,14 +43,18 @@ Gem::Specification.new do |s|
41
43
  "lib/rufus/jig/version.rb",
42
44
  "rufus-jig.gemspec",
43
45
  "test/base.rb",
46
+ "test/bm/bm0.rb",
47
+ "test/bm/bm1.rb",
44
48
  "test/conc/put_vs_delete.rb",
45
49
  "test/couch_base.rb",
50
+ "test/couch_url.txt",
46
51
  "test/ct_0_couch.rb",
47
52
  "test/ct_1_couchdb.rb",
48
53
  "test/ct_2_couchdb_options.rb",
49
54
  "test/ct_3_couchdb_views.rb",
50
55
  "test/ct_4_attachments.rb",
51
56
  "test/ct_5_couchdb_continuous.rb",
57
+ "test/cut_0_auth_couch.rb",
52
58
  "test/server.rb",
53
59
  "test/test.rb",
54
60
  "test/to.sh",
@@ -61,7 +67,8 @@ Gem::Specification.new do |s|
61
67
  "test/ut_4_http_prefix.rb",
62
68
  "test/ut_5_http_misc.rb",
63
69
  "test/ut_6_args.rb",
64
- "test/ut_7_parse_uri.rb"
70
+ "test/ut_7_parse_uri.rb",
71
+ "test/ut_8_auth.rb"
65
72
  ]
66
73
  s.homepage = %q{http://github.com/jmettraux/rufus-jig/}
67
74
  s.rdoc_options = ["--charset=UTF-8"]
@@ -6,17 +6,24 @@ require 'rubygems'
6
6
  require 'yajl'
7
7
 
8
8
  # Our default
9
- transport_library = 'patron'
9
+ transport_library = 'net/http'
10
10
 
11
11
  if ARGV.include?( '--em' )
12
12
  require 'openssl'
13
13
  transport_library = 'em-http'
14
- elsif ARGV.include?( '--net' )
15
- transport_library = 'net/http'
14
+ elsif ARGV.include?( '--netp' )
15
+ transport_library = 'net/http/persistent'
16
+ elsif ARGV.include?( '--patron' )
17
+ transport_library = 'patron'
16
18
  end
17
19
 
18
20
  require transport_library
19
21
 
22
+ unless $advertised
23
+ p transport_library
24
+ $advertised = true
25
+ end
26
+
20
27
  require 'rufus/jig'
21
28
 
22
29
  require 'test/unit'
@@ -0,0 +1,49 @@
1
+
2
+ $:.unshift('lib')
3
+ require 'benchmark'
4
+ require 'patron'
5
+ require 'yajl'
6
+ require 'rufus/jig'
7
+
8
+ N = 1
9
+
10
+ DOC = {}
11
+ 1000.times { |i| DOC["key#{i}"] = { 'a' => 'b', 'c' => 'd', 'e' =>'f' } }
12
+
13
+ Rufus::Jig::Couch.delete_db('http://127.0.0.1:5984/test_bm0') rescue nil
14
+ CDB = Rufus::Jig::Couch.put_db('http://127.0.0.1:5984/test_bm0')
15
+
16
+ Benchmark.benchmark(' ' * 31 + Benchmark::Tms::CAPTION, 31) do |b|
17
+
18
+ b.report('marshal to file') do
19
+ N.times do
20
+ File.open('out.marshal', 'wb') { |f| f.write(Marshal.dump(DOC)) }
21
+ end
22
+ end
23
+ b.report('yajl to file') do
24
+ N.times do
25
+ File.open('out.json', 'wb') { |f| f.write(Rufus::Jig::Json.encode(DOC)) }
26
+ end
27
+ end
28
+ b.report('to couch') do
29
+ N.times do |i|
30
+ CDB.put_doc("out#{i}", DOC)
31
+ end
32
+ end
33
+
34
+ b.report('marshal from file') do
35
+ N.times do
36
+ doc = Marshal.load(File.read('out.marshal'))
37
+ end
38
+ end
39
+ b.report('yajl from file') do
40
+ N.times do
41
+ doc = Rufus::Jig::Json.decode(File.read('out.json'))
42
+ end
43
+ end
44
+ b.report('from couch') do
45
+ N.times do |i|
46
+ doc = CDB.get_doc("out#{i}")
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,43 @@
1
+
2
+ $:.unshift('lib')
3
+ require 'rubygems'
4
+ require 'yajl'
5
+
6
+ # Our default
7
+ transport_library = 'net/http'
8
+
9
+ if ARGV.include?( '--em' )
10
+ require 'openssl'
11
+ transport_library = 'em-http'
12
+ elsif ARGV.include?( '--netp' )
13
+ transport_library = 'net/http/persistent'
14
+ elsif ARGV.include?( '--patron' )
15
+ transport_library = 'patron'
16
+ end
17
+
18
+ require transport_library
19
+
20
+ require 'rufus/jig'
21
+
22
+ #c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'artsr_development_ruote_msgs')
23
+ #p c.get('_all_docs')
24
+ #p c.delete("1100!2148379060!2010-06-28!1277767412.550787!002")
25
+
26
+ require 'benchmark'
27
+
28
+ N = 10_000
29
+
30
+ c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'artsr_development_ruote_msgs')
31
+
32
+ puts
33
+ puts RUBY_VERSION
34
+ puts c.http.variant
35
+ puts
36
+
37
+ Benchmark.benchmark(' ' * 31 + Benchmark::Tms::CAPTION, 31) do |b|
38
+
39
+ b.report('get') do
40
+ N.times { c.get('_all_docs?include_docs=true') }
41
+ end
42
+ end
43
+
@@ -6,19 +6,24 @@ require 'rubygems'
6
6
  require 'yajl'
7
7
 
8
8
  # Our default
9
- transport_library = 'patron'
9
+ transport_library = 'net/http'
10
10
 
11
11
  if ARGV.include?( '--em' )
12
12
  require 'openssl'
13
13
  transport_library = 'em-http'
14
- elsif ARGV.include?( '--net' )
15
- transport_library = 'net/http'
14
+ elsif ARGV.include?( '--netp' )
15
+ transport_library = 'net/http/persistent'
16
+ elsif ARGV.include?( '--patron' )
17
+ transport_library = 'patron'
16
18
  end
17
19
 
18
- p [ :lib, transport_library ]
19
-
20
20
  require transport_library
21
21
 
22
+ unless $advertised
23
+ p transport_library
24
+ $advertised = true
25
+ end
26
+
22
27
  require 'rufus/jig'
23
28
 
24
29
  require 'test/unit'
@@ -39,3 +44,9 @@ rescue Exception => e
39
44
  exit(1)
40
45
  end
41
46
 
47
+ class Test::Unit::TestCase
48
+ def couch_url
49
+ File.read('test/couch_url.txt').strip
50
+ end
51
+ end
52
+
@@ -0,0 +1 @@
1
+ http://admin:admin@127.0.0.1:5984
@@ -12,7 +12,7 @@ class CtCouchTest < Test::Unit::TestCase
12
12
 
13
13
  def setup
14
14
 
15
- h = Rufus::Jig::Http.new('127.0.0.1', 5984)
15
+ h = Rufus::Jig::Http.new(couch_url)
16
16
  begin
17
17
  h.delete('/rufus_jig_test')
18
18
  rescue Exception => e
@@ -21,7 +21,7 @@ class CtCouchTest < Test::Unit::TestCase
21
21
  h.close rescue nil
22
22
  end
23
23
 
24
- @c = Rufus::Jig::Couch.new('127.0.0.1', 5984)
24
+ @c = Rufus::Jig::Couch.new(couch_url)
25
25
  end
26
26
 
27
27
  def teardown
@@ -38,16 +38,16 @@ class CtCouchTest < Test::Unit::TestCase
38
38
 
39
39
  @c.put('rufus_jig_test')
40
40
 
41
- assert_not_nil Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test')
41
+ assert_not_nil Rufus::Jig::Http.new(couch_url).get('/rufus_jig_test')
42
42
  end
43
43
 
44
44
  def test_delete_db
45
45
 
46
- Rufus::Jig::Http.new('127.0.0.1', 5984).put('/rufus_jig_test', '')
46
+ Rufus::Jig::Http.new(couch_url).put('/rufus_jig_test', '')
47
47
 
48
48
  @c.delete('rufus_jig_test')
49
49
 
50
- assert_nil Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test')
50
+ assert_nil Rufus::Jig::Http.new(couch_url).get('/rufus_jig_test')
51
51
  end
52
52
 
53
53
  # def test_uuids