lib/leanback.rb in leanback-0.1.7 vs lib/leanback.rb in leanback-0.1.8
- old
+ new
@@ -1,7 +1,8 @@
require 'rest_client'
require 'yajl'
+require 'erb'
module Document
#create a document
def self.create( doc)
@@ -92,38 +93,80 @@
rescue => e
raise e
end
end
-##find a document by _id
-def self.find(doc)
+##view a document
+def self.view(doc)
set_address
db_name = doc[:database]
doc_id = doc[:doc_id]
begin
response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/' + doc_id
hash = Yajl::Parser.parse(response.to_str)
- #puts hash.inspect
rescue => e
hash = Yajl::Parser.parse(e.response.to_s)
- #puts hash.inspect
end
end
+#query a permanent view
+def self.find(doc)
+ set_address
+ db_name = doc[:database]
+ design_doc_name = doc[:design_doc]
+ view_name = doc[:view]
+ begin
+ response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name
+ hash = Yajl::Parser.parse(response.to_str)
+ rows = hash["rows"]
+ count = 0
+ rows.each do |row|
+ rows[count] = row["value"]
+ count += 1
+ end
+ return rows
+ rescue => e
+ hash = Yajl::Parser.parse(e.response.to_s)
+ end
+end
+
+#create a design document with views
+def self.create_design(doc)
+ set_address
+ db_name = doc[:database]
+ design_doc_name = doc[:design_doc]
+ json_doc_name = doc[:json_doc]
+
+ begin
+ #bind json doc to string
+ message_template = ERB.new File.new(json_doc_name).read
+ str = message_template.result(binding)
+ rescue => e
+ raise e
+ end
+
+ begin
+
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, str, {:content_type => :json, :accept => :json}
+ hash = Yajl::Parser.parse(response.to_str)
+ rescue => e
+ hash = Yajl::Parser.parse(e.response.to_s)
+ end
+end
+
#return a list of all docs in the database
def self.docs_from(database_name)
set_address
begin
response = RestClient.get 'http://' + @address + ':' + @port + '/' + URI.escape(database_name) + '/_all_docs?include_docs=true', {:content_type => :json}
hash = Yajl::Parser.parse(response.to_str)
rows = hash["rows"]
- only_rows = []
count = 0
rows.each do |row|
- only_rows[count] = row["doc"]
- count = count + 1
+ rows[count] = row["doc"]
+ count += 1
end
- return only_rows
+ return rows
rescue => e
hash = Yajl::Parser.parse(e.response.to_s)
end
end