couch-db 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd7e0350e725a546206b7e282da8b3da2243aafe
4
- data.tar.gz: 6ca15a89196b694b0da64031c0e71129587c3bcc
3
+ metadata.gz: 3e3033182fa750ebed0db9996c0b2249377a3188
4
+ data.tar.gz: b461244dc466eaeadb3b2de587bbe8844f3f3cc9
5
5
  SHA512:
6
- metadata.gz: e1623c824fc6bf0aaceb843036e75d37fe47a177bdda6e8c9eef3c2a9c952e28c39c561d7a223ec3dcdc22eb2ca5e25a65dd038cacd1f44ee73c5251a51a4eb9
7
- data.tar.gz: 647f9b023c4c202f8f384a208156d890ac126bb5da51fd0adc5f5a73ccd148c0fbca2821902c682fc4744394da0c5294b912a204ee10cd082d70221958117c74
6
+ metadata.gz: 35912a5df5d840696f2b255a84dde4552c6e61e322f631df75d34e6bb9066b1e5bf19dc75451ac518bade1c047a32479aa83e10a5e229748558bb1bff63224ff
7
+ data.tar.gz: d6879f8c757632d04768102b06e3b12f92cfdbe8dbd7cb12931241d6eb761f7b0cafc2e3a15e6f767daab4fe698a6a85d58a457314ecbdd00a7aa11eae8574c9
data/.gitignore CHANGED
@@ -7,4 +7,7 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
- .idea/
10
+ .idea/
11
+
12
+ lol.rb
13
+ rake_secret
@@ -15,54 +15,8 @@ class Hash
15
15
  end
16
16
 
17
17
  module Couch
18
- module BasicRequest
19
- def delete(uri, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
20
- req=Net::HTTP::Delete.new(uri)
21
- req.basic_auth @options[:name], @options[:password]
22
- request(req, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent)
23
- end
24
-
25
- def head(uri, open_timeout: 5*30, read_timeout: 5*30, fail_silent: true)
26
- req = Net::HTTP::Head.new(uri)
27
- req.basic_auth @options[:name], @options[:password]
28
- request(req, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent)
29
- end
30
-
31
- def get(uri, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
32
- req = Net::HTTP::Get.new(uri)
33
- req.basic_auth @options[:name], @options[:password]
34
- request(req, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent)
35
- end
36
-
37
- def put(uri, json, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
38
- posty_request(json, Net::HTTP::Put.new(uri), open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent)
39
- end
40
-
41
- def post(uri, json, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
42
- posty_request(json, Net::HTTP::Post.new(uri), open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent)
43
- end
44
-
45
- def posty_request(json, req, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
46
- req.basic_auth @options[:name], @options[:password]
47
- req['Content-Type'] = 'application/json;charset=utf-8'
48
- req.body = json
49
- request(req, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent)
50
- end
51
-
52
- def request(req, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
53
- res = Net::HTTP.start(@url.host, @url.port,
54
- :use_ssl => @url.scheme =='https') do |http|
55
- http.open_timeout = open_timeout
56
- http.read_timeout = read_timeout
57
- http.request(req)
58
- end
59
- unless fail_silent or res.kind_of?(Net::HTTPSuccess)
60
- # puts "CouchDb responsed with error code #{res.code}"
61
- handle_error(req, res)
62
- end
63
- res
64
- end
65
18
 
19
+ module BasicRequest
66
20
  def create_postfix(query_params, default='')
67
21
  if query_params
68
22
  params_a = []
@@ -76,10 +30,6 @@ module Couch
76
30
  postfix
77
31
  end
78
32
 
79
- def handle_error(req, res)
80
- raise RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
81
- end
82
-
83
33
  module Get
84
34
  # Returns parsed doc from database
85
35
  def get_doc(database, id)
@@ -318,8 +268,18 @@ module Couch
318
268
  def get_bytesize_array(docs)
319
269
  bytesize = 0
320
270
  docs.each do |doc|
321
- # TODO: Note that this may be inexact; see documentation for ObjectSpace.memsize_of
271
+ # Note that this may be inexact; see documentation for ObjectSpace.memsize_of
322
272
  bytesize += ObjectSpace.memsize_of doc
273
+
274
+ if doc['_attachments']
275
+ doc['_attachments'].each do |att|
276
+ att.each do |val|
277
+ if val['data']
278
+ bytesize += val['data'].length # Number of chars, i.e. bytes
279
+ end
280
+ end
281
+ end
282
+ end
323
283
  end
324
284
  bytesize
325
285
  end
@@ -350,11 +310,121 @@ module Couch
350
310
  if url.is_a? String
351
311
  url = URI(url)
352
312
  end
353
- @url = url
313
+ @couch_url = url
354
314
  @options = options
355
315
  @options[:use_ssl] ||= true
356
316
  end
357
317
 
318
+ def delete(uri, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
319
+ Request.new(Net::HTTP::Delete.new(uri), nil,
320
+ @options.merge({couch_url: @couch_url, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent})
321
+ ).perform
322
+ end
323
+
324
+ def new_delete(uri)
325
+ Request.new(Net::HTTP::Delete.new(uri)).couch_url(@couch_url)
326
+ end
327
+
328
+ def head(uri, open_timeout: 5*30, read_timeout: 5*30, fail_silent: true)
329
+ Request.new(Net::HTTP::Head.new(uri), nil,
330
+ @options.merge({couch_url: @couch_url, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent})
331
+ ).perform
332
+ end
333
+
334
+ def new_head(uri)
335
+ Request.new(Net::HTTP::Head.new(uri)).couch_url(@couch_url)
336
+ end
337
+
338
+ def get(uri, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
339
+ Request.new(
340
+ Net::HTTP::Get.new(uri), nil,
341
+ @options.merge({couch_url: @couch_url, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent})
342
+ ).perform
343
+ end
344
+
345
+ def new_get(uri)
346
+ Request.new(Net::HTTP::Get.new(uri)).couch_url(@couch_url)
347
+ end
348
+
349
+ def put(uri, json, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
350
+ Request.new(Net::HTTP::Put.new(uri), json,
351
+ @options.merge({couch_url: @couch_url, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent})
352
+ ).perform
353
+ end
354
+
355
+ def new_put(uri)
356
+ Request.new(Net::HTTP::Put.new(uri)).couch_url(@couch_url)
357
+ end
358
+
359
+ def post(uri, json, open_timeout: 5*30, read_timeout: 5*30, fail_silent: false)
360
+ Request.new(Net::HTTP::Post.new(uri), json,
361
+ @options.merge({couch_url: @couch_url, open_timeout: open_timeout, read_timeout: read_timeout, fail_silent: fail_silent})
362
+ ).perform
363
+ end
364
+
365
+ def new_post(uri)
366
+ Request.new(Net::HTTP::Post.new(uri)).couch_url(@couch_url)
367
+ end
368
+
369
+ class Request
370
+ def initialize(req, json=nil, opts={open_timeout: 5*30, read_timeout: 5*30, fail_silent: false})
371
+ @req=req
372
+ @json = json
373
+ @options = opts
374
+ end
375
+
376
+ def json(json)
377
+ @json = json
378
+ self
379
+ end
380
+
381
+ def couch_url(couch_url)
382
+ @options.merge!({couch_url: couch_url})
383
+ self
384
+ end
385
+
386
+ def open_timeout(open_timeout)
387
+ @options.merge!({open_timeout: open_timeout})
388
+ self
389
+ end
390
+
391
+ def read_timeout(read_timeout)
392
+ @options.merge!({read_timeout: read_timeout})
393
+ self
394
+ end
395
+
396
+ def fail_silent(fail_silent)
397
+ @options.merge!({fail_silent: fail_silent})
398
+ self
399
+ end
400
+
401
+ def perform
402
+ @req.basic_auth @options[:name], @options[:password]
403
+
404
+ if @json
405
+ @req['Content-Type'] = 'application/json;charset=utf-8'
406
+ @req.body = @json
407
+ end
408
+
409
+ res = Net::HTTP.start(@options[:couch_url].host, @options[:couch_url].port,
410
+ :use_ssl => @options[:couch_url].scheme =='https') do |http|
411
+ http.open_timeout = @options[:open_timeout]
412
+ http.read_timeout = @options[:read_timeout]
413
+ http.request(@req)
414
+ end
415
+
416
+ unless @options[:fail_silent] or res.kind_of?(Net::HTTPSuccess)
417
+ # puts "CouchDb responsed with error code #{res.code}"
418
+ handle_error(@req, res)
419
+ end
420
+ res
421
+ end
422
+
423
+ def handle_error(req, res)
424
+ raise RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
425
+ end
426
+ end
427
+
358
428
  include BasicRequest
359
429
  include BasicRequest::Head
360
430
  include BasicRequest::Get
@@ -1,6 +1,6 @@
1
1
  module Couch
2
2
  # noinspection ALL
3
3
  module DB
4
- VERSION = '0.9.0'
4
+ VERSION = '0.9.1'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maarten Trompper
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-21 00:00:00.000000000 Z
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler