leanback 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.rdoc CHANGED
@@ -1,7 +1,23 @@
1
+ 0.4.2
2
+ November 16, 2013
3
+ * Optionally return documents and query results as hashmaps with symbolized keys
4
+ Example:
5
+ Couchdb.find_by({:database => 'contacts', :email => 'nancy@mail.com'} ,auth_session, symbolize_keys: true))
6
+ # => [{:_id =>"Nancy", :_rev =>"1-d15a83d2a23b495c19df2595b636ecc8", :firstname =>"Nancy", :lastname =>"Lee",
7
+ # :phone =>"347-808-3734", :email =>"nancy@mail.com", :gender =>"female"}]
8
+ And
9
+ Couchdb.find_by({:database => 'contacts', :email => 'nancy@mail.com'} , auth_session, symbolize_keys: true)
10
+
11
+ # => [{:_id=>"Nancy", :_rev=>"1-d15a83d2a23b495c19df2595b636ecc8", :firstname =>"Nancy", :lastname =>"Lee",
12
+ # :phone =>"347-808-3734", :email =>"nancy@mail.com", :gender =>"female"}]
13
+
14
+ This works for view document results and also works for all finders and queries. See documentation for details.
15
+
1
16
  0.4.1
2
17
  August 23,2013
3
18
  * Changed bind address to require prefix (http:// or https://)
4
- Example: Couchdb.address = "http://whisperservers.cloudant.com"
19
+ Example:
20
+ Couchdb.address = "http://whisperservers.cloudant.com"
5
21
  See documentation for details
6
22
 
7
23
  =0.4.0
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
data/leanback.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "leanback"
8
- s.version = "0.4.1"
8
+ s.version = "0.4.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Obi Akubue"]
12
- s.date = "2013-08-24"
12
+ s.date = "2013-11-16"
13
13
  s.description = "lightweight Ruby interface to CouchDB"
14
14
  s.email = "obioraakubue@yahoo.com"
15
15
  s.extra_rdoc_files = [
data/lib/leanback.rb CHANGED
@@ -238,13 +238,13 @@ module Couchdb
238
238
  end
239
239
 
240
240
  ##view a document
241
- def self.view(doc,auth_session = "")
241
+ def self.view(doc,auth_session = "", options = {})
242
242
  set_address
243
243
  db_name = doc[:database]
244
244
  doc_id = doc[:doc_id]
245
245
  begin
246
246
  response = RestClient.get "#{@address}:#{@port}/#{db_name}/#{doc_id}",{:cookies => {"AuthSession" => auth_session}}
247
- hash = Yajl::Parser.parse(response.to_str)
247
+ hash = Yajl::Parser.parse(response.to_str, symbolize_keys(options))
248
248
  rescue => e
249
249
  couch_error(e)
250
250
  end
@@ -298,14 +298,16 @@ module Couchdb
298
298
  key = URI.escape('?key="' + key + '"')
299
299
  response = RestClient.get "#{@address}:#{@port}/#{db_name}/_design/#{design_doc_name}/_view/#{view_name}#{key}&#{URI.escape(params)}" ,{:cookies => {"AuthSession" => auth_session}}
300
300
  end
301
- hash = Yajl::Parser.parse(response.to_str)
302
- rows = hash["rows"]
303
- count = 0
301
+ hash = Yajl::Parser.parse(response.to_str, symbolize_keys(options))
302
+ data = []
303
+ rows = hash["rows"] unless hash["rows"].nil?
304
+ rows = hash[:rows] unless hash[:rows].nil?
304
305
  rows.each do |row|
305
- rows[count] = row["value"]
306
- count += 1
306
+ value = row["value"] unless row["value"].nil?
307
+ value = row[:value] unless row[:value].nil?
308
+ data << value
307
309
  end
308
- return rows
310
+ return data
309
311
  rescue => e
310
312
  couch_error(e)
311
313
  end
@@ -340,20 +342,20 @@ module Couchdb
340
342
  design_doc = doc[:design_doc]
341
343
  view = doc[:view]
342
344
  json_doc = doc[:json_doc]
343
-
345
+ query_info = {:database => db_name, :design_doc => design_doc, :view => view}
344
346
  begin
345
347
  if( key == nil)
346
- docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key=nil,options)
348
+ docs = find(query_info,auth_session,key=nil,options)
347
349
  else
348
- docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key,options)
350
+ docs = find(query_info,auth_session,key,options)
349
351
  end
350
352
  rescue CouchdbException => e
351
353
  document = { :database => db_name, :design_doc => design_doc, :json_doc => json_doc}
352
354
  create_design document,auth_session
353
355
  if( key == nil)
354
- docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key=nil,options)
356
+ docs = find(query_info,auth_session,key=nil,options)
355
357
  else
356
- docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key,options)
358
+ docs = find(query_info,auth_session,key,options)
357
359
  end
358
360
  end
359
361
  return docs
@@ -588,7 +590,13 @@ module Couchdb
588
590
  couch_error(e)
589
591
  end
590
592
  end
593
+
594
+ def self.symbolize_keys(options)
595
+ return {symbolize_keys: true} if options[:symbolize_keys]
596
+ return {}
597
+ end
591
598
 
599
+ private_class_method :symbolize_keys
592
600
 
593
601
  class << self
594
602
  attr_accessor :address
@@ -37,14 +37,19 @@ it "should create and view document doc" do
37
37
  :lastname =>'smith',
38
38
  :phone => '202-234-1234',
39
39
  :email =>'james@mail.com',
40
- :age =>'34',
41
- :gender =>'male'}
40
+ :age =>'34',
41
+ :gender =>'male'}
42
42
  doc = {:database => 'friends', :doc_id => 'john', :data => data}
43
43
  Couchdb.create_doc doc
44
44
 
45
45
  doc = {:database => 'friends', :doc_id => 'john'}
46
46
  hash = Couchdb.view doc
47
47
  hash["_id"].should == 'john'
48
+
49
+ #view doc and return symbolized keys
50
+ doc = {:database => 'friends', :doc_id => 'john'}
51
+ hash = Couchdb.view(doc,'', {symbolize_keys: true})
52
+ hash.should include(data)
48
53
  end
49
54
 
50
55
  it "should count the lastnames named smith" do
@@ -227,8 +232,8 @@ it "should test finder options" do
227
232
  doc = {:database => 'fishes', :doc_id => 'aaron', :data => data}
228
233
  Couchdb.create_doc doc
229
234
 
230
- data = {:firstname => 'john', :gender =>'male', :age => '28', :salary => '60000'}
231
- doc = {:database => 'fishes', :doc_id => 'john', :data => data}
235
+ data_c = {:firstname => 'john', :gender =>'male', :age => '28', :salary => '60000'}
236
+ doc = {:database => 'fishes', :doc_id => 'john', :data => data_c}
232
237
  Couchdb.create_doc doc
233
238
 
234
239
  data = {:firstname => 'peter', :gender =>'male', :age => '45', :salary => '78000'}
@@ -245,6 +250,10 @@ it "should test finder options" do
245
250
  h = hash[0]
246
251
  h["firstname"].should == "john"
247
252
  hash.length.should == 2
253
+
254
+ #return hash in symbolized keys for find_by_keys
255
+ hash = Couchdb.find_by_keys({:database => 'fishes', :keys => keys},'', options = {:limit => 2, :skip => 1, :symbolize_keys => true})
256
+ hash.first.should include(data_c)
248
257
 
249
258
  #create the design doc to be queryed in the test
250
259
  Couchdb.find_by({:database => 'fishes', :gender => 'male'})
@@ -258,13 +267,24 @@ it "should test finder options" do
258
267
  h = hash[0]
259
268
  h["firstname"].should == "john"
260
269
  hash.length.should == 2
270
+
271
+ #return results in symbolized keys
272
+ hash = Couchdb.find view,'',key=nil, options = {:limit => 2, :skip => 1, :symbolize_keys => true}
273
+ hash.first.should include(data_c)
274
+
275
+ #it should not return symbolized keys
276
+ hash = Couchdb.find view,'',key=nil, options = {:limit => 2, :skip => 1, :symbolize_keys => false}
277
+ hash.first.should_not include(data_c)
278
+ hash.first["firstname"].should == "john"
261
279
 
262
280
  Couchdb.find_by({:database => 'fishes', :gender => 'male'},"",options = {:limit => 2, :skip => 1})
263
281
  h = hash[0]
264
282
  h["firstname"].should == "john"
265
283
  hash.length.should == 2
266
284
 
267
-
285
+ #return symbolized results
286
+ hash = Couchdb.find_by({:database => 'fishes', :gender => 'male'},'',options = {:limit => 2, :skip => 1, :symbolize_keys => true})
287
+ hash.first.should include(data_c)
268
288
 
269
289
  hash = Couchdb.find view,"",key='male', options = {:descending => true}
270
290
  h = hash[0]
@@ -338,6 +358,12 @@ it "should test finder options" do
338
358
  h0["firstname"].should == "john"
339
359
  h1["firstname"].should == "sam"
340
360
  hash.length.should == 2
361
+
362
+
363
+ #return results as symbolized keys
364
+ options = {:startkey => ["28","male"], :endkey => ["28","male"], :skip => 1,:symbolize_keys => true}
365
+ hash = Couchdb.find_on_fly(view,'',key=nil, options)
366
+ hash.first.should include(data_c)
341
367
 
342
368
 
343
369
  options = {:startkey => ["28","male"], :endkey => ["28","male"]}
@@ -39,17 +39,23 @@ it "should create a database add a finder method to it and then delete the datab
39
39
  it "should create and view document doc" do
40
40
  Couchdb.create('contacts',@@auth_session)
41
41
  data = {:firstname => 'John',
42
- :lastname =>'smith',
43
- :phone => '202-234-1234',
44
- :email =>'james@mail.com',
45
- :age =>'34',
46
- :gender =>'male'}
42
+ :lastname =>'smith',
43
+ :phone => '202-234-1234',
44
+ :email =>'james@mail.com',
45
+ :age =>'34',
46
+ :gender =>'male'}
47
+
47
48
  doc = {:database => 'contacts', :doc_id => 'john', :data => data}
48
49
  Couchdb.create_doc doc,@@auth_session
49
50
 
50
51
  doc = {:database => 'contacts', :doc_id => 'john'}
51
52
  hash = Couchdb.view doc,@@auth_session
52
53
  hash["_id"].should == 'john'
54
+
55
+ #view doc and return symbolized keys
56
+ doc = {:database => 'contacts', :doc_id => 'john'}
57
+ hash = Couchdb.view(doc,@@auth_session, {symbolize_keys: true})
58
+ hash.should include(data)
53
59
  end
54
60
 
55
61
  it "should count the lastnames named smith" do
@@ -233,8 +239,8 @@ it "should test finder options" do
233
239
  doc = {:database => 'fishes', :doc_id => 'aaron', :data => data}
234
240
  Couchdb.create_doc doc,@@auth_session
235
241
 
236
- data = {:firstname => 'john', :gender =>'male', :age => '28', :salary => '60000'}
237
- doc = {:database => 'fishes', :doc_id => 'john', :data => data}
242
+ data_c = {:firstname => 'john', :gender =>'male', :age => '28', :salary => '60000'}
243
+ doc = {:database => 'fishes', :doc_id => 'john', :data => data_c}
238
244
  Couchdb.create_doc doc,@@auth_session
239
245
 
240
246
  data = {:firstname => 'peter', :gender =>'male', :age => '45', :salary => '78000'}
@@ -248,9 +254,12 @@ it "should test finder options" do
248
254
 
249
255
  keys = {:age =>'28', :gender => 'male'}
250
256
  hash = Couchdb.find_by_keys({:database => 'fishes', :keys => keys},@@auth_session, options = {:limit => 2, :skip => 1})
251
- h = hash[0]
252
- h["firstname"].should == "john"
257
+ hash.first["firstname"].should == "john"
253
258
  hash.length.should == 2
259
+
260
+ #return hash in symbolized keys for find_by_keys
261
+ hash = Couchdb.find_by_keys({:database => 'fishes', :keys => keys},@@auth_session, options = {:limit => 2, :skip => 1, :symbolize_keys => true})
262
+ hash.first.should include(data_c)
254
263
 
255
264
  #create the design doc to be queryed in the test
256
265
  Couchdb.find_by({:database => 'fishes', :gender => 'male'},@@auth_session)
@@ -261,16 +270,25 @@ it "should test finder options" do
261
270
  :view => 'find_by_gender'}
262
271
 
263
272
  hash = Couchdb.find view,@@auth_session,key=nil, options = {:limit => 2, :skip => 1}
264
- h = hash[0]
265
- h["firstname"].should == "john"
273
+ hash.first["firstname"].should == "john"
266
274
  hash.length.should == 2
275
+
276
+ #return results in symbolized keys
277
+ hash = Couchdb.find view,@@auth_session,key=nil, options = {:limit => 2, :skip => 1, :symbolize_keys => true}
278
+ hash.first.should include(data_c)
279
+
280
+ #it should not return symbolized keys
281
+ hash = Couchdb.find view,@@auth_session,key=nil, options = {:limit => 2, :skip => 1, :symbolize_keys => false}
282
+ hash.first.should_not include(data_c)
283
+ hash.first["firstname"].should == "john"
267
284
 
268
- Couchdb.find_by({:database => 'fishes', :gender => 'male'},@@auth_session,options = {:limit => 2, :skip => 1})
269
- h = hash[0]
270
- h["firstname"].should == "john"
285
+ hash = Couchdb.find_by({:database => 'fishes', :gender => 'male'},@@auth_session,options = {:limit => 2, :skip => 1})
286
+ hash.first["firstname"].should == "john"
271
287
  hash.length.should == 2
272
288
 
273
-
289
+ #return symbolized results
290
+ hash = Couchdb.find_by({:database => 'fishes', :gender => 'male'},@@auth_session,options = {:limit => 2, :skip => 1, :symbolize_keys => true})
291
+ hash.first.should include(data_c)
274
292
 
275
293
  hash = Couchdb.find view,@@auth_session,key='male', options = {:descending => true}
276
294
  h = hash[0]
@@ -344,7 +362,11 @@ it "should test finder options" do
344
362
  h0["firstname"].should == "john"
345
363
  h1["firstname"].should == "sam"
346
364
  hash.length.should == 2
347
-
365
+
366
+ #return results as symbolized keys
367
+ options = {:startkey => ["28","male"], :endkey => ["28","male"], :skip => 1,:symbolize_keys => true}
368
+ hash = Couchdb.find_on_fly(view,@@auth_session,key=nil, options)
369
+ hash.first.should include(data_c)
348
370
 
349
371
  options = {:startkey => ["28","male"], :endkey => ["28","male"]}
350
372
 
@@ -1,12 +1,15 @@
1
1
  require 'spec_base.rb'
2
2
 
3
+ hash = Couchdb.login(username = 'obi',password ='trusted')
4
+ @@admin_auth_session = hash["AuthSession"]
5
+
6
+ user = { :username => "david", :password => "trusted", :roles => []}
7
+ Couchdb.add_user(user, @@admin_auth_session)
8
+
3
9
  #a day in the life of the non-admin user
4
10
  hash = Couchdb.login(username = 'david',password ='trusted')
5
11
  @@auth_session = hash["AuthSession"]
6
12
 
7
- hash = Couchdb.login(username = 'obi',password ='trusted')
8
- @@admin_auth_session = hash["AuthSession"]
9
-
10
13
  #specs to ensure non-admin users function properly
11
14
  describe "non admin user" do
12
15
  it "should create a document, view, update it" do
@@ -55,7 +58,9 @@ it "should delete document" do
55
58
  lambda {Couchdb.view(doc,@@auth_session)}.should raise_error(CouchdbException,"CouchDB: Error - not_found. Reason - deleted")
56
59
  end
57
60
 
58
- it "should delete the database" do
61
+ it "should delete the database and user" do
62
+ doc = {:database => '_users', :doc_id => 'org.couchdb.user:david'}
63
+ Couchdb.delete_doc doc,@@admin_auth_session
59
64
  Couchdb.delete 'contacts',@@admin_auth_session
60
65
  end
61
66
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leanback
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-24 00:00:00.000000000 Z
12
+ date: 2013-11-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -208,7 +208,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
208
208
  version: '0'
209
209
  segments:
210
210
  - 0
211
- hash: 3337391598339047950
211
+ hash: 3937638598034080075
212
212
  required_rubygems_version: !ruby/object:Gem::Requirement
213
213
  none: false
214
214
  requirements: