leanback 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
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.3.2"
8
+ s.version = "0.3.3"
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 = "2012-12-02"
12
+ s.date = "2013-05-26"
13
13
  s.description = "lightweight Ruby interface to CouchDB"
14
14
  s.email = "obioraakubue@yahoo.com"
15
15
  s.extra_rdoc_files = [
@@ -33,8 +33,10 @@ Gem::Specification.new do |s|
33
33
  "spec/spec_base.rb",
34
34
  "test/helper.rb",
35
35
  "test/main.rb",
36
+ "test/my_view.json",
36
37
  "test/my_views.json",
37
- "test/test_leanback.rb"
38
+ "test/test_leanback.rb",
39
+ "test/view_age.json"
38
40
  ]
39
41
  s.homepage = "http://github.com/obi-a/leanback"
40
42
  s.licenses = ["MIT"]
data/lib/leanback.rb CHANGED
@@ -361,6 +361,53 @@ def self.add_finder(options,auth_session = "")
361
361
  end
362
362
  end
363
363
 
364
+ #add a counter method to the database
365
+ #this creates a count method that counts documents by key
366
+ def self.add_counter(options,auth_session = "")
367
+ set_address
368
+ db_name = options[:database]
369
+ key = options[:key]
370
+ design_doc_name = key + '_counter'
371
+
372
+ view ='{
373
+ "language" : "javascript",
374
+ "views" :{
375
+ "count_'+key+'" : {
376
+ "map" : "function(doc){ if(doc.'+key+') emit(doc.'+key+',null);}", "reduce": "_count"
377
+ }
378
+ }
379
+ }'
380
+
381
+ begin
382
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:cookies => {"AuthSession" => auth_session}}
383
+ rescue => e
384
+ hash = Yajl::Parser.parse(e.response.to_s)
385
+ raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1]
386
+ end
387
+ end
388
+
389
+ #count by key
390
+ def self.count(options,auth_session = "")
391
+ set_address
392
+ db_name = options[:database]
393
+ index = options.keys[1].to_s
394
+ search_term = options.values[1]
395
+ design_doc_name = index + '_counter'
396
+ view_name = 'count_' + index
397
+
398
+ begin
399
+ view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
400
+ docs = find view,auth_session,search_term
401
+ rescue CouchdbException => e
402
+ #add a counter index if one doesn't already exist in the database
403
+ #then count_by_key
404
+ add_counter({:database => db_name, :key => index},auth_session)
405
+ docs = find view,auth_session,search_term
406
+ end
407
+ count = docs[0]
408
+ return count.to_i
409
+ end
410
+
364
411
  #find by key
365
412
  def self.find_by(options,auth_session = "")
366
413
  set_address
@@ -28,14 +28,11 @@ it "should create a database add a finder method to it and then delete the datab
28
28
  end
29
29
 
30
30
 
31
- it "find items by key" do
32
- docs = Couchdb.find_by({:database => 'friends', :lastname => 'winner'})
33
- d = docs[0]
34
- d["lastname"].should == "winner"
35
- Couchdb.delete_doc({:database => 'friends', :doc_id => '_design/lastname_finder'})
36
- end
31
+
37
32
 
38
33
  it "should create and view document doc" do
34
+ Couchdb.create('friends')
35
+
39
36
  data = {:firstname => 'John',
40
37
  :lastname =>'smith',
41
38
  :phone => '202-234-1234',
@@ -50,12 +47,29 @@ it "should create and view document doc" do
50
47
  hash["_id"].should == 'john'
51
48
  end
52
49
 
50
+ it "should count the lastnames named smith" do
51
+ count = Couchdb.count({:database => 'friends', :lastname => 'smith'})
52
+ count.should == 1
53
+ end
54
+
55
+ it "should count lastnames named brown" do
56
+ count = Couchdb.count({:database => 'friends', :lastname => 'brown'})
57
+ count.should == 0
58
+ end
59
+
60
+ it "find items by key" do
61
+ docs = Couchdb.find_by({:database => 'friends', :lastname => 'smith'})
62
+ d = docs[0]
63
+ d["lastname"].should == "smith"
64
+ Couchdb.delete_doc({:database => 'friends', :doc_id => '_design/lastname_finder'})
65
+ end
66
+
53
67
  it "should query a permanent view that doesn't exist and handle exception" do
54
68
  begin
55
69
  view = { :database => "friends", :design_doc => 'more_views', :view => 'get_user_email'}
56
70
  Couchdb.find view
57
71
  rescue CouchdbException => e
58
- e.to_s.should == "CouchDB: Error - not_found. Reason - deleted"
72
+ e.to_s.should == "CouchDB: Error - not_found. Reason - missing"
59
73
  e.error.should == "not_found"
60
74
  end
61
75
  end
@@ -64,7 +78,7 @@ it "should query a permanent view and create the view on the fly, if it doesn't
64
78
  view = {:database => 'friends',
65
79
  :design_doc => 'my_views',
66
80
  :view => 'get_emails',
67
- :json_doc => '/home/obi/bin/my_views.json'}
81
+ :json_doc => '/home/obi/bin/leanback/test/my_view.json'}
68
82
 
69
83
  docs = Couchdb.find_on_fly(view)
70
84
  docs[0].include?("Email").should == true
@@ -77,21 +91,21 @@ it "should query a permanent view and create the view on the fly, if it doesn't
77
91
  end
78
92
 
79
93
  it "should query a permanent view by key and create the view on the fly, if it doesn't already exist" do
80
- view = { :database => 'contacts',
94
+ view = { :database => 'friends',
81
95
  :design_doc => 'the_view',
82
96
  :view => 'age',
83
- :json_doc => '/home/obi/bin/view_age.json'}
97
+ :json_doc => '/home/obi/bin/leanback/test/view_age.json'}
84
98
 
85
- age = '36'
99
+ age = '34'
86
100
  docs = Couchdb.find_on_fly(view,"",key = age)
87
101
  docs[0].include?("age").should == true
88
102
  d = docs[0]
89
- d["age"].should == '36'
103
+ d["age"].should == '34'
90
104
  #verify that the view was created
91
- doc = {:database => 'contacts', :doc_id => '_design/the_view'}
105
+ doc = {:database => 'friends', :doc_id => '_design/the_view'}
92
106
  hash = Couchdb.view doc
93
107
  hash["_id"].should == '_design/the_view'
94
- Couchdb.delete_doc({:database => 'contacts', :doc_id => '_design/the_view'})
108
+ Couchdb.delete_doc({:database => 'friends', :doc_id => '_design/the_view'})
95
109
  end
96
110
 
97
111
  it "should create a design doc/permanent view and query it" do
@@ -184,7 +198,8 @@ it "should set a config section, retrieve it and delete it" do
184
198
 
185
199
  Couchdb.get_config(data).should == "sample_value"
186
200
 
187
- Couchdb.delete_config(data).should == "sample_value"
201
+ Couchdb.delete_config(data).should == "sample_value"
202
+ Couchdb.delete 'friends'
188
203
 
189
204
  lambda {Couchdb.get_config(data)}.should raise_error(CouchdbException,"CouchDB: Error - not_found. Reason - unknown_config_value")
190
205
  end
@@ -36,14 +36,8 @@ it "should create a database add a finder method to it and then delete the datab
36
36
  Couchdb.delete 'wiseguys',@@auth_session
37
37
  end
38
38
 
39
- it "find items by key" do
40
- docs = Couchdb.find_by({:database => 'contacts', :lastname => 'winner'},@@auth_session)
41
- d = docs[0]
42
- d["lastname"].should == "winner"
43
- Couchdb.delete_doc({:database => 'contacts', :doc_id => '_design/lastname_finder'},@@auth_session)
44
- end
45
-
46
39
  it "should create and view document doc" do
40
+ Couchdb.create('contacts',@@auth_session)
47
41
  data = {:firstname => 'John',
48
42
  :lastname =>'smith',
49
43
  :phone => '202-234-1234',
@@ -58,12 +52,33 @@ it "should create and view document doc" do
58
52
  hash["_id"].should == 'john'
59
53
  end
60
54
 
55
+ it "should count the lastnames named smith" do
56
+ count = Couchdb.count({:database => 'contacts', :lastname => 'smith'},@@auth_session)
57
+ count.should == 1
58
+ end
59
+
60
+ it "should count lastnames named brown" do
61
+ count = Couchdb.count({:database => 'contacts', :lastname => 'brown'},@@auth_session)
62
+ count.should == 0
63
+ end
64
+
65
+
66
+ it "find items by key" do
67
+ docs = Couchdb.find_by({:database => 'contacts', :lastname => 'smith'},@@auth_session)
68
+ d = docs[0]
69
+ d["lastname"].should == "smith"
70
+ Couchdb.delete_doc({:database => 'contacts', :doc_id => '_design/lastname_finder'},@@auth_session)
71
+ end
72
+
73
+
74
+
75
+
61
76
  it "should query a permanent view that doesn't exist and handle exception" do
62
77
  begin
63
78
  view = { :database => "contacts", :design_doc => 'more_views', :view => 'get_user_email'}
64
79
  Couchdb.find view,@@auth_session
65
80
  rescue CouchdbException => e
66
- e.to_s.should == "CouchDB: Error - not_found. Reason - deleted"
81
+ e.to_s.should == "CouchDB: Error - not_found. Reason - missing"
67
82
  e.error.should == "not_found"
68
83
  end
69
84
  end
@@ -72,7 +87,7 @@ it "should query a permanent view and create the view on the fly, if it doesn't
72
87
  view = {:database => 'contacts',
73
88
  :design_doc => 'my_views',
74
89
  :view => 'get_emails',
75
- :json_doc => '/home/obi/bin/my_views.json'}
90
+ :json_doc => '/home/obi/bin/leanback/test/my_view.json'}
76
91
 
77
92
  docs = Couchdb.find_on_fly(view,@@auth_session)
78
93
  docs[0].include?("Email").should == true
@@ -88,13 +103,13 @@ it "should query a permanent view by key and create the view on the fly, if it d
88
103
  view = { :database => 'contacts',
89
104
  :design_doc => 'the_view',
90
105
  :view => 'age',
91
- :json_doc => '/home/obi/bin/view_age.json'}
106
+ :json_doc => '/home/obi/bin/leanback/test/view_age.json'}
92
107
 
93
- age = '36'
108
+ age = '34'
94
109
  docs = Couchdb.find_on_fly(view,@@auth_session,key = age)
95
110
  docs[0].include?("age").should == true
96
111
  d = docs[0]
97
- d["age"].should == '36'
112
+ d["age"].should == '34'
98
113
  #verify that the view was created
99
114
  doc = {:database => 'contacts', :doc_id => '_design/the_view'}
100
115
  hash = Couchdb.view doc,@@auth_session
@@ -272,11 +287,16 @@ it "should non-admin user password, verify new password" do
272
287
 
273
288
  end
274
289
 
290
+ it "should delete the database" do
291
+ Couchdb.delete 'contacts',@@auth_session
292
+ end
293
+
275
294
  it "should switch to default bind address" do
276
295
  data = {:section => "httpd",
277
296
  :key => "port",
278
297
  :value => "5984" }
279
- Couchdb.set_config(data,@@auth_session)
298
+ Couchdb.set_config(data,@@auth_session)
299
+
280
300
  #Couchdb.address = nil
281
301
  #Couchdb.port = nil
282
302
  #Couchdb.all @@auth_session
@@ -9,7 +9,9 @@ hash = Couchdb.login(username = 'obi',password ='trusted')
9
9
 
10
10
  #specs to ensure non-admin users function properly
11
11
  describe "non admin user" do
12
- it "should create a document, view, update and delete it" do
12
+ it "should create a document, view, update it" do
13
+ Couchdb.create('contacts',@@admin_auth_session)
14
+
13
15
  data = {:firstname => 'Nancy', :lastname =>'Lee', :phone => '347-808-3734',:email =>'nancy@mail.com',:gender => 'female'}
14
16
  doc = {:database => 'contacts', :doc_id => 'eeek', :data => data}
15
17
  hash = Couchdb.create_doc doc,@@auth_session
@@ -23,16 +25,10 @@ describe "non admin user" do
23
25
  hash = Couchdb.update_doc doc,@@auth_session
24
26
  hash["id"].should == 'eeek'
25
27
  hash["ok"].should == true
26
-
27
- Couchdb.delete_doc({:database => 'contacts', :doc_id => 'eeek'},@@auth_session)
28
-
29
- hash["id"].should == 'eeek'
30
- hash["ok"].should == true
31
- doc = { :database => 'contacts', :doc_id => 'eeek'}
32
- lambda {Couchdb.view(doc,@@auth_session)}.should raise_error(CouchdbException,"CouchDB: Error - not_found. Reason - deleted")
33
28
  end
34
29
 
35
30
  it"should query a view" do
31
+
36
32
  doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/home/obi/bin/leanback/test/my_views.json' }
37
33
  hash = Couchdb.create_design doc,@@admin_auth_session
38
34
 
@@ -42,11 +38,25 @@ it"should query a view" do
42
38
 
43
39
  view = { :database => "contacts", :design_doc => 'more_views', :view => 'get_email'}
44
40
  hash = Couchdb.find view,@@auth_session
45
-
46
41
  hash[0].has_key?("Firstname").should == true
47
42
  hash[0].has_key?("Lastname").should == true
48
43
  hash[0].has_key?("Email").should == true
49
44
  Couchdb.delete_doc({:database => 'contacts', :doc_id => '_design/more_views'},@@admin_auth_session)
45
+
46
+
47
+ end
48
+
49
+
50
+ it "should delete document" do
51
+ hash = Couchdb.delete_doc({:database => 'contacts', :doc_id => 'eeek'},@@auth_session)
52
+ hash["id"].should == 'eeek'
53
+ hash["ok"].should == true
54
+ doc = { :database => 'contacts', :doc_id => 'eeek'}
55
+ lambda {Couchdb.view(doc,@@auth_session)}.should raise_error(CouchdbException,"CouchDB: Error - not_found. Reason - deleted")
56
+ end
57
+
58
+ it "should delete the database" do
59
+ Couchdb.delete 'contacts',@@admin_auth_session
50
60
  end
51
61
 
52
62
  end
data/test/my_view.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "language" : "javascript",
3
+ "views" :{
4
+ "get_emails" : {
5
+ "map" : "function(doc){ if(doc.firstname && doc.email) emit(doc.id,{Name: doc.firstname, Email: doc.email}); }"
6
+ }
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "language" : "javascript",
3
+ "views" :{
4
+ "age" : {
5
+ "map" : "function(doc){ if(doc.age) emit(doc.age,doc); }"
6
+ }
7
+ }
8
+ }
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.3.2
4
+ version: 0.3.3
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: 2012-12-02 00:00:00.000000000 Z
12
+ date: 2013-05-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -147,8 +147,10 @@ files:
147
147
  - spec/spec_base.rb
148
148
  - test/helper.rb
149
149
  - test/main.rb
150
+ - test/my_view.json
150
151
  - test/my_views.json
151
152
  - test/test_leanback.rb
153
+ - test/view_age.json
152
154
  homepage: http://github.com/obi-a/leanback
153
155
  licenses:
154
156
  - MIT
@@ -164,7 +166,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
164
166
  version: '0'
165
167
  segments:
166
168
  - 0
167
- hash: 1267076556477492408
169
+ hash: -746348248553049715
168
170
  required_rubygems_version: !ruby/object:Gem::Requirement
169
171
  none: false
170
172
  requirements: