lib/leanback.rb in leanback-0.2.2 vs lib/leanback.rb in leanback-0.2.3

- old
+ new

@@ -1,9 +1,17 @@ require 'rest_client' require 'yajl' require 'erb' +class CouchdbException < RuntimeError + attr :error + def initialize(error) + @error = error.values[0] + end +end + + module Document #create a document def self.create( doc) db_name = doc[:database] @@ -14,10 +22,11 @@ begin response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id),json_data, {:content_type => :json, :accept => :json} hash = Yajl::Parser.parse(response.to_str) rescue => e hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end #edit a document def self.edit(doc) @@ -29,10 +38,11 @@ begin response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id), json_data, {:content_type => :json, :accept => :json} hash = Yajl::Parser.parse(response.to_str) rescue => e hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end #delete a doc def self.delete(doc) @@ -43,10 +53,11 @@ begin response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id) + '?rev=' + rev, {:content_type => :json} hash = Yajl::Parser.parse(response.to_str) rescue => e hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end class << self attr_accessor :address @@ -67,10 +78,11 @@ begin response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:content_type => :json} hash = Yajl::Parser.parse(response.to_str) rescue => e hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end #delete a database def self.delete(database_name) @@ -78,10 +90,11 @@ begin response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:content_type => :json} hash = Yajl::Parser.parse(response.to_str) rescue => e hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end #return a list of all databases def self.all @@ -102,10 +115,11 @@ begin response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/' + doc_id hash = Yajl::Parser.parse(response.to_str) rescue => e hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end #query a permanent view def self.find(doc,key=nil) @@ -128,10 +142,11 @@ end return rows rescue => e #puts e.inspect hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end #create a design document with views def self.create_design(doc) @@ -152,10 +167,11 @@ 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) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end #add a finder method to the database #this creates a find by key method @@ -179,10 +195,11 @@ begin response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:content_type => :json, :accept => :json} rescue => e hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end #find by key def self.find_by(options) @@ -220,9 +237,10 @@ count += 1 end return rows rescue => e hash = Yajl::Parser.parse(e.response.to_s) + raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - " + hash.values[1] end end class << self