leanback 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -55,9 +55,9 @@ Lets edit that document to change the email address, phone number, and add a gen
55
55
  Document.edit doc
56
56
  # => {"ok"=>true, "id"=>"Linda", "rev"=>"2-e813a0e902e3ac114400ff3959a2adde"}
57
57
 
58
- Retrieve the document by id
58
+ Retrieve/view the document
59
59
  doc = {:database => 'contacts', :doc_id => 'Linda'}
60
- Couchdb.find doc
60
+ Couchdb.view doc
61
61
  #=> {"_id"=>"Linda", "_rev"=>"2-e813a0e902e3ac114400ff3959a2adde", "firstname"=>"Linda",
62
62
  "lastname"=>"smith", "email"=>"linda@mail.com", "gender"=>"female", "phone"=>"718-245-5611"}
63
63
 
@@ -77,6 +77,48 @@ Retrieve all documents in a database
77
77
  puts d["phone"]
78
78
  end
79
79
 
80
+ ===Query a permanent view
81
+ view = { :database => "contacts", :design_doc => 'my_views', :view => 'get_female_contacts'}
82
+ Couchdb.find view
83
+ # => [{"_id"=>"Mary", "_rev"=>"5-bfcd67fd17dbb6a875af8f6dc497b15f", "firstname"=>"Mary", "lastname"=>"smith",
84
+ "phone"=>"212-234-1234", "email"=>"mary@mail.com", "gender"=>"female"},
85
+ {"_id"=>"Nancy", "_rev"=>"1-d15a83d2a23b495c19df2595b636ecc8", "firstname"=>"Nancy", "lastname"=>"Lee",
86
+ "phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female"}]
87
+
88
+ This is similar to sending a
89
+ GET http://127.0.0.1:5984/[database]/_design/[design_doc]/_view/[view_name]
90
+ For the above example
91
+ GET http://127.0.0.1:5984/contacts/_design/my_views/_view/get_female_contacts
92
+ Leanback parses the native JSON results to return only the data values.
93
+
94
+ ===Create a design document with permanent views:
95
+ First define the views in a JSON file
96
+ //my_views.json
97
+ {
98
+ "language" : "javascript",
99
+ "views" :{
100
+ "get_email" : {
101
+ "map" : "function(doc){
102
+ if(doc.lastname && doc.email)
103
+ emit(doc.id,{Firstname: doc.firstname, Lastname: doc.lastname, Email: doc.email});
104
+ }"
105
+ }
106
+ }
107
+ }
108
+
109
+ Now create the design document and add the json file
110
+ doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/path/to/my_views.json' }
111
+ hash = Couchdb.create_design doc
112
+ # => {"ok"=>true, "id"=>"_design/more_views", "rev"=>"1-d67ae97ff03a98f68ddc300bf9ae5048"}
113
+
114
+ To query the view
115
+ view = { :database => "contacts", :design_doc => 'more_views', :view => 'get_email'}
116
+ Couchdb.find view
117
+ # => [{"Firstname"=>"Nancy", "Lastname"=>"Lee", "Email"=>"nancy@mail.com"},
118
+ {"Firstname"=>"john", "Lastname"=>"smith", "Email"=>"john@mail.com"}]
119
+
120
+
121
+ ===Error handling
80
122
  Attempting to create a database that already exists
81
123
  Couchdb.create 'contacts'
82
124
  # => {"error"=>"file_exists", "reason"=>"The database could not be created, the file already exists."}
@@ -85,12 +127,13 @@ Attempting to delete a database that no longer exists
85
127
  Couchdb.delete 'more_books'
86
128
  # => {"error"=>"not_found", "reason"=>"missing"}
87
129
 
88
- Most methods return a hash value unless otherwise specified. When a database transaction goes through successfully most methods will return a hash value # => {"ok" => true ...} and when something goes wrong it returns a hash value # => {"error" => ...}
130
+ Most methods return a hash value unless otherwise specified. When a database transaction goes through successfully most methods will return a hash {"ok" => true ...} and when something goes wrong it returns a hash {"error" => ...}
89
131
 
90
132
  You can always check this value
91
133
  hash = Couchdb.delete 'schools'
92
134
  puts hash.inspect
93
135
 
136
+ ===Bind Address
94
137
  Leanback uses the default Couchdb bind address http://127.0.0.1:5984. To use a different bind address;
95
138
  Couchdb.address = '192.168.2.16'
96
139
  Couchdb.port = '6000'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.7
1
+ 0.1.8
data/leanback.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{leanback}
8
- s.version = "0.1.7"
8
+ s.version = "0.1.8"
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 = %q{2011-06-19}
12
+ s.date = %q{2011-07-04}
13
13
  s.description = %q{lightweight Ruby interface to CouchDB}
14
14
  s.email = %q{obioraakubue@yahoo.com}
15
15
  s.extra_rdoc_files = [
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "leanback.gemspec",
28
28
  "lib/leanback.rb",
29
29
  "test/helper.rb",
30
+ "test/my_views.json",
30
31
  "test/test_leanback.rb"
31
32
  ]
32
33
  s.homepage = %q{http://github.com/obi-a/leanback}
data/lib/leanback.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rest_client'
2
2
  require 'yajl'
3
+ require 'erb'
3
4
 
4
5
  module Document
5
6
 
@@ -94,21 +95,64 @@ module Couchdb
94
95
  end
95
96
  end
96
97
 
97
- ##find a document by _id
98
- def self.find(doc)
98
+ ##view a document
99
+ def self.view(doc)
99
100
  set_address
100
101
  db_name = doc[:database]
101
102
  doc_id = doc[:doc_id]
102
103
  begin
103
104
  response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/' + doc_id
104
105
  hash = Yajl::Parser.parse(response.to_str)
105
- #puts hash.inspect
106
106
  rescue => e
107
107
  hash = Yajl::Parser.parse(e.response.to_s)
108
- #puts hash.inspect
109
108
  end
110
109
  end
111
110
 
111
+ #query a permanent view
112
+ def self.find(doc)
113
+ set_address
114
+ db_name = doc[:database]
115
+ design_doc_name = doc[:design_doc]
116
+ view_name = doc[:view]
117
+ begin
118
+ response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name
119
+ hash = Yajl::Parser.parse(response.to_str)
120
+ rows = hash["rows"]
121
+ count = 0
122
+ rows.each do |row|
123
+ rows[count] = row["value"]
124
+ count += 1
125
+ end
126
+ return rows
127
+ rescue => e
128
+ hash = Yajl::Parser.parse(e.response.to_s)
129
+ end
130
+ end
131
+
132
+ #create a design document with views
133
+ def self.create_design(doc)
134
+ set_address
135
+ db_name = doc[:database]
136
+ design_doc_name = doc[:design_doc]
137
+ json_doc_name = doc[:json_doc]
138
+
139
+ begin
140
+ #bind json doc to string
141
+ message_template = ERB.new File.new(json_doc_name).read
142
+ str = message_template.result(binding)
143
+ rescue => e
144
+ raise e
145
+ end
146
+
147
+ begin
148
+
149
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, str, {:content_type => :json, :accept => :json}
150
+ hash = Yajl::Parser.parse(response.to_str)
151
+ rescue => e
152
+ hash = Yajl::Parser.parse(e.response.to_s)
153
+ end
154
+ end
155
+
112
156
  #return a list of all docs in the database
113
157
  def self.docs_from(database_name)
114
158
  set_address
@@ -116,13 +160,12 @@ def self.docs_from(database_name)
116
160
  response = RestClient.get 'http://' + @address + ':' + @port + '/' + URI.escape(database_name) + '/_all_docs?include_docs=true', {:content_type => :json}
117
161
  hash = Yajl::Parser.parse(response.to_str)
118
162
  rows = hash["rows"]
119
- only_rows = []
120
163
  count = 0
121
164
  rows.each do |row|
122
- only_rows[count] = row["doc"]
123
- count = count + 1
165
+ rows[count] = row["doc"]
166
+ count += 1
124
167
  end
125
- return only_rows
168
+ return rows
126
169
  rescue => e
127
170
  hash = Yajl::Parser.parse(e.response.to_s)
128
171
  end
@@ -0,0 +1,11 @@
1
+ {
2
+ "language" : "javascript",
3
+ "views" :{
4
+ "get_email" : {
5
+ "map" : "function(doc){
6
+ if(doc.lastname && doc.email)
7
+ emit(doc.id,{Firstname: doc.firstname, Lastname: doc.lastname, Email: doc.email});
8
+ }"
9
+ }
10
+ }
11
+ }
@@ -9,72 +9,81 @@ class TestLeanback < Test::Unit::TestCase
9
9
 
10
10
 
11
11
  should "create a database if it doesn't already exist" do
12
- hash = Couchdb.create 'staff'
13
- puts hash.inspect
12
+ hash = Couchdb.create 'staff'
13
+ #puts hash.inspect
14
14
  end
15
15
 
16
16
  should "create a database if it doesn't already exist" do
17
17
  hash = Couchdb.create 'contacts'
18
- puts hash.inspect
18
+ #puts hash.inspect
19
19
  end
20
20
 
21
- #should "return error message illegal database name" do
22
- # hash = Couchdb.create 'more books'
23
- # puts hash.inspect
24
- # end
25
21
 
26
- should "return a document by ID" do
22
+ should "view document doc" do
27
23
 
28
- doc = {:database => 'monitors', :doc_id => 'ee6f4f65-2b5b-4452-a9c4-fd9d860ec17d'}
29
- hash = Couchdb.find doc
30
- puts hash.inspect
24
+ doc = {:database => 'monitors', :doc_id => '3-d71c8ee21d6753896f2d08f57a985e94'}
25
+ hash = Couchdb.view doc
26
+ #puts hash.inspect
27
+ end
28
+
29
+ should "Query a permanent view" do
30
+ view = { :database => "contacts", :design_doc => 'more_views', :view => 'get_email'}
31
+ puts 'viewing design doc...'
32
+ hash = Couchdb.find view
33
+ puts hash.inspect
34
+ end
35
+
36
+ should "Create a design doc/permanent view" do
37
+ doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/home/obi/bin/leanback/test/my_views.json' }
38
+ hash = Couchdb.create_design doc
39
+ #puts hash.inspect
31
40
  end
32
41
 
33
42
  should "return a display a list of all databases" do
34
43
  databases = Couchdb.all
35
- databases.each do |db_name|
36
- puts db_name
44
+ databases.each do |db_name|
45
+ #puts db_name
37
46
  end
38
47
  end
39
48
 
40
49
  should "delete a database" do
41
50
  hash = Couchdb.delete 'staff'
42
- puts hash.inspect
51
+ #puts hash.inspect
43
52
  end
44
53
 
45
54
  should "create a document" do
46
- data = {:firstname => 'Mary', :lastname =>'smith', :phone => '212-234-1234',:email =>'john@mail.com'}
47
- doc = {:database => 'contacts', :doc_id => 'Mary', :data => data}
55
+ data = {:firstname => 'Nancy', :lastname =>'Lee', :phone => '347-808-3734',:email =>'nancy@mail.com',:gender => 'female'}
56
+ doc = {:database => 'contacts', :doc_id => 'Nancy', :data => data}
48
57
  hash = Document.create doc
49
- puts hash.inspect
58
+ #puts hash.inspect
50
59
  end
51
60
 
52
61
  should "edit a document" do
53
62
  data = {:firstname => 'john', :lastname =>'smith', :email => 'john@mail.com',:gender=>'male', :_rev=>'2-e813a0e902e3ac114400ff3959a2adde'}
54
63
  doc = {:database => 'contacts', :doc_id => 'john', :data => data}
55
64
  hash = Document.edit doc
56
- puts hash.inspect
65
+ #puts hash.inspect
57
66
  end
58
67
 
59
68
  should "delete a document" do
60
69
  doc = {:database => 'contacts', :doc_id => 'john', :rev => '3-be02e80490f8e9e610d9a9e33d752316'}
61
70
  hash = Document.delete doc
62
- puts hash.inspect
71
+ #puts hash.inspect
63
72
  end
64
73
 
65
74
  should "display all documents in the database" do
66
75
  docs = Couchdb.docs_from 'monitors'
67
-
76
+ puts 'docs = Couchdb.docs_from monitors'
68
77
  docs.each do |d|
69
- puts d["_rev"]
70
- puts d["_id"]
71
- puts d["every"]
72
- puts d["monitor"]
73
- puts d["url"]
74
- puts d["test"]
75
- puts d["contact"]
76
- puts d["via"]
77
- puts d["notify_interval"]
78
+ # puts "_rev: " + d["_rev"]
79
+ #puts "_id: " + d["_id"]
80
+ #puts "every: " + d["every"]
81
+ #puts "monitor: " + d["monitor"]
82
+ #puts "url: " + d["url"]
83
+ #puts "test: " + d["test"]
84
+ #puts "contact: " + d["contact"]
85
+ #puts "via: " + d["via"]
86
+ #puts "notify_interval: " + d["notify_interval"]
78
87
  end
79
88
  end
80
89
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 7
9
- version: 0.1.7
8
+ - 8
9
+ version: 0.1.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - Obi Akubue
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-19 00:00:00 -04:00
17
+ date: 2011-07-04 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -119,6 +119,7 @@ files:
119
119
  - leanback.gemspec
120
120
  - lib/leanback.rb
121
121
  - test/helper.rb
122
+ - test/my_views.json
122
123
  - test/test_leanback.rb
123
124
  has_rdoc: true
124
125
  homepage: http://github.com/obi-a/leanback
@@ -134,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
135
  requirements:
135
136
  - - ">="
136
137
  - !ruby/object:Gem::Version
137
- hash: 609197857
138
+ hash: 116332943
138
139
  segments:
139
140
  - 0
140
141
  version: "0"