leanback 0.2.8 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Changelog.rdoc +12 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +10 -0
- data/README.md +39 -0
- data/VERSION +1 -1
- data/leanback.gemspec +21 -9
- data/lib/leanback.rb +156 -54
- data/spec/admin_party/database_spec.rb +190 -0
- data/spec/no_admin_party/database_spec.rb +268 -0
- data/spec/no_admin_party/non_admin_user_spec.rb +52 -0
- data/spec/spec_base.rb +2 -0
- data/test/main.rb +154 -0
- data/test/test_leanback.rb +63 -19
- metadata +28 -31
- data/README.rdoc +0 -393
    
        data/Changelog.rdoc
    CHANGED
    
    | @@ -1,3 +1,15 @@ | |
| 1 | 
            +
            =Leanback 0.3.0
         | 
| 2 | 
            +
            Nvember 20,2011
         | 
| 3 | 
            +
            * Added CouchDB Security features: working with admin and non-admin users, authentication, adding security objects to databases. See documentation for details.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            November 9,2011:-
         | 
| 6 | 
            +
            * Added support for couchDB configuration API. It's now possible to change/modify/delete any couchDB configuration settings with Leanback. README will soon be updated with details.
         | 
| 7 | 
            +
              
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            November 3, 2011:-
         | 
| 10 | 
            +
            * Changed all the syntax for working with documents (creating, editing, updating and deleting documents now use a new syntax) See README for the change. Document::Module has been removed, it's methods are now part of Couchdb::Module. 
         | 
| 11 | 
            +
                
         | 
| 12 | 
            +
             | 
| 1 13 | 
             
            =Leanback 0.2.7
         | 
| 2 14 | 
             
            August 4, 2011:-
         | 
| 3 15 | 
             
            * Added ability to Query a permanent view and create it on the fly from a json file, if it doesn't already exist and then return the values
         | 
    
        data/Gemfile
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -2,6 +2,7 @@ GEM | |
| 2 2 | 
             
              remote: http://rubygems.org/
         | 
| 3 3 | 
             
              remote: http://gemcutter.org/
         | 
| 4 4 | 
             
              specs:
         | 
| 5 | 
            +
                diff-lcs (1.1.2)
         | 
| 5 6 | 
             
                git (1.2.5)
         | 
| 6 7 | 
             
                jeweler (1.5.2)
         | 
| 7 8 | 
             
                  bundler (~> 1.0.0)
         | 
| @@ -12,6 +13,14 @@ GEM | |
| 12 13 | 
             
                rcov (0.9.9)
         | 
| 13 14 | 
             
                rest-client (1.6.1)
         | 
| 14 15 | 
             
                  mime-types (>= 1.16)
         | 
| 16 | 
            +
                rspec (2.6.0)
         | 
| 17 | 
            +
                  rspec-core (~> 2.6.0)
         | 
| 18 | 
            +
                  rspec-expectations (~> 2.6.0)
         | 
| 19 | 
            +
                  rspec-mocks (~> 2.6.0)
         | 
| 20 | 
            +
                rspec-core (2.6.4)
         | 
| 21 | 
            +
                rspec-expectations (2.6.0)
         | 
| 22 | 
            +
                  diff-lcs (~> 1.1.2)
         | 
| 23 | 
            +
                rspec-mocks (2.6.0)
         | 
| 15 24 | 
             
                shoulda (2.11.3)
         | 
| 16 25 | 
             
                yajl-ruby (0.8.2)
         | 
| 17 26 |  | 
| @@ -23,5 +32,6 @@ DEPENDENCIES | |
| 23 32 | 
             
              jeweler (~> 1.5.2)
         | 
| 24 33 | 
             
              rcov
         | 
| 25 34 | 
             
              rest-client
         | 
| 35 | 
            +
              rspec
         | 
| 26 36 | 
             
              shoulda
         | 
| 27 37 | 
             
              yajl-ruby
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,39 @@ | |
| 1 | 
            +
            ###Leanback
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Simple Ruby Interface to CouchDB. 
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            This project is still under development. Not complete by any means. I made this Gem to use in my projects. I will expose more CouchDB features soon.
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            ##Goals
         | 
| 8 | 
            +
            * To create a simple Ruby Interface to CouchDB
         | 
| 9 | 
            +
            * Expose the features of CouchDB to the Ruby Lang., 
         | 
| 10 | 
            +
            * Use a minimalist Ruby DSL to access CouchDB
         | 
| 11 | 
            +
            * provide a very easy way to persist and retrieve data
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            ##Usage
         | 
| 14 | 
            +
             | 
| 15 | 
            +
             | 
| 16 | 
            +
            * [Leanback](http://www.whisperservers.com/leanback/leanback/)
         | 
| 17 | 
            +
             | 
| 18 | 
            +
               + [Installation](http://www.whisperservers.com/leanback/leanback/installation/) 
         | 
| 19 | 
            +
             
         | 
| 20 | 
            +
               + [Basic CouchDB Operations](http://www.whisperservers.com/leanback/basic-couchdb-operations/)
         | 
| 21 | 
            +
             | 
| 22 | 
            +
               + Working with [CouchDB Views](http://www.whisperservers.com/leanback/design-documents-and-permanent-views/)
         | 
| 23 | 
            +
             | 
| 24 | 
            +
               + [Error Handling](http://www.whisperservers.com/ragios/ragios-saint-ruby/status-reports/)
         | 
| 25 | 
            +
               
         | 
| 26 | 
            +
               + [CouchDB Configuration](http://www.whisperservers.com/leanback/couchdb-configuration/) 
         | 
| 27 | 
            +
               
         | 
| 28 | 
            +
               + [CouchDB Security](http://www.whisperservers.com/leanback/couchdb-security/) 
         | 
| 29 | 
            +
               
         | 
| 30 | 
            +
               + [CouchDB Bind_Address & Port](http://www.whisperservers.com/leanback/setting-the-bind_address-port/) 
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            ##License
         | 
| 33 | 
            +
            MIT License.
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            ##Copyright
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            Copyright (c) 2011 Obi Akubue. 
         | 
| 38 | 
            +
             | 
| 39 | 
            +
             | 
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            0. | 
| 1 | 
            +
            0.3.0
         | 
    
        data/leanback.gemspec
    CHANGED
    
    | @@ -5,16 +5,16 @@ | |
| 5 5 |  | 
| 6 6 | 
             
            Gem::Specification.new do |s|
         | 
| 7 7 | 
             
              s.name = %q{leanback}
         | 
| 8 | 
            -
              s.version = "0. | 
| 8 | 
            +
              s.version = "0.3.0"
         | 
| 9 9 |  | 
| 10 10 | 
             
              s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
         | 
| 11 | 
            -
              s.authors = [ | 
| 12 | 
            -
              s.date = %q{2011- | 
| 11 | 
            +
              s.authors = [%q{Obi Akubue}]
         | 
| 12 | 
            +
              s.date = %q{2011-11-21}
         | 
| 13 13 | 
             
              s.description = %q{lightweight Ruby interface to CouchDB}
         | 
| 14 14 | 
             
              s.email = %q{obioraakubue@yahoo.com}
         | 
| 15 15 | 
             
              s.extra_rdoc_files = [
         | 
| 16 16 | 
             
                "LICENSE.txt",
         | 
| 17 | 
            -
                "README. | 
| 17 | 
            +
                "README.md"
         | 
| 18 18 | 
             
              ]
         | 
| 19 19 | 
             
              s.files = [
         | 
| 20 20 | 
             
                ".document",
         | 
| @@ -22,27 +22,36 @@ Gem::Specification.new do |s| | |
| 22 22 | 
             
                "Gemfile",
         | 
| 23 23 | 
             
                "Gemfile.lock",
         | 
| 24 24 | 
             
                "LICENSE.txt",
         | 
| 25 | 
            -
                "README. | 
| 25 | 
            +
                "README.md",
         | 
| 26 26 | 
             
                "Rakefile",
         | 
| 27 27 | 
             
                "VERSION",
         | 
| 28 28 | 
             
                "leanback.gemspec",
         | 
| 29 29 | 
             
                "lib/leanback.rb",
         | 
| 30 | 
            +
                "spec/admin_party/database_spec.rb",
         | 
| 31 | 
            +
                "spec/no_admin_party/database_spec.rb",
         | 
| 32 | 
            +
                "spec/no_admin_party/non_admin_user_spec.rb",
         | 
| 33 | 
            +
                "spec/spec_base.rb",
         | 
| 30 34 | 
             
                "test/helper.rb",
         | 
| 35 | 
            +
                "test/main.rb",
         | 
| 31 36 | 
             
                "test/my_views.json",
         | 
| 32 37 | 
             
                "test/test_leanback.rb"
         | 
| 33 38 | 
             
              ]
         | 
| 34 39 | 
             
              s.homepage = %q{http://github.com/obi-a/leanback}
         | 
| 35 | 
            -
              s.licenses = [ | 
| 36 | 
            -
              s.require_paths = [ | 
| 37 | 
            -
              s.rubygems_version = %q{1. | 
| 40 | 
            +
              s.licenses = [%q{MIT}]
         | 
| 41 | 
            +
              s.require_paths = [%q{lib}]
         | 
| 42 | 
            +
              s.rubygems_version = %q{1.8.5}
         | 
| 38 43 | 
             
              s.summary = %q{lightweight Ruby interface to CouchDB}
         | 
| 39 44 | 
             
              s.test_files = [
         | 
| 45 | 
            +
                "spec/admin_party/database_spec.rb",
         | 
| 46 | 
            +
                "spec/no_admin_party/database_spec.rb",
         | 
| 47 | 
            +
                "spec/no_admin_party/non_admin_user_spec.rb",
         | 
| 48 | 
            +
                "spec/spec_base.rb",
         | 
| 40 49 | 
             
                "test/helper.rb",
         | 
| 50 | 
            +
                "test/main.rb",
         | 
| 41 51 | 
             
                "test/test_leanback.rb"
         | 
| 42 52 | 
             
              ]
         | 
| 43 53 |  | 
| 44 54 | 
             
              if s.respond_to? :specification_version then
         | 
| 45 | 
            -
                current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
         | 
| 46 55 | 
             
                s.specification_version = 3
         | 
| 47 56 |  | 
| 48 57 | 
             
                if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
         | 
| @@ -52,6 +61,7 @@ Gem::Specification.new do |s| | |
| 52 61 | 
             
                  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 53 62 | 
             
                  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
         | 
| 54 63 | 
             
                  s.add_development_dependency(%q<rcov>, [">= 0"])
         | 
| 64 | 
            +
                  s.add_development_dependency(%q<rspec>, [">= 0"])
         | 
| 55 65 | 
             
                else
         | 
| 56 66 | 
             
                  s.add_dependency(%q<rest-client>, [">= 0"])
         | 
| 57 67 | 
             
                  s.add_dependency(%q<yajl-ruby>, [">= 0"])
         | 
| @@ -59,6 +69,7 @@ Gem::Specification.new do |s| | |
| 59 69 | 
             
                  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 60 70 | 
             
                  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
         | 
| 61 71 | 
             
                  s.add_dependency(%q<rcov>, [">= 0"])
         | 
| 72 | 
            +
                  s.add_dependency(%q<rspec>, [">= 0"])
         | 
| 62 73 | 
             
                end
         | 
| 63 74 | 
             
              else
         | 
| 64 75 | 
             
                s.add_dependency(%q<rest-client>, [">= 0"])
         | 
| @@ -67,6 +78,7 @@ Gem::Specification.new do |s| | |
| 67 78 | 
             
                s.add_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 68 79 | 
             
                s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
         | 
| 69 80 | 
             
                s.add_dependency(%q<rcov>, [">= 0"])
         | 
| 81 | 
            +
                s.add_dependency(%q<rspec>, [">= 0"])
         | 
| 70 82 | 
             
              end
         | 
| 71 83 | 
             
            end
         | 
| 72 84 |  | 
    
        data/lib/leanback.rb
    CHANGED
    
    | @@ -9,18 +9,129 @@ class CouchdbException < RuntimeError | |
| 9 9 | 
             
              end
         | 
| 10 10 | 
             
            end
         | 
| 11 11 |  | 
| 12 | 
            +
            module Couchdb
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            #add a new user 
         | 
| 15 | 
            +
            def self.add_user(user, auth_session="")
         | 
| 16 | 
            +
              o =  [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten;  
         | 
| 17 | 
            +
              salt  =  (0..50).map{ o[rand(o.length)]  }.join; 
         | 
| 18 | 
            +
              new_user = {:username => user[:username], :password => user[:password], :roles => user[:roles], :salt => salt}
         | 
| 19 | 
            +
              create_user(new_user,auth_session)
         | 
| 20 | 
            +
            end
         | 
| 12 21 |  | 
| 13 | 
            -
             | 
| 22 | 
            +
            #create a new user 
         | 
| 23 | 
            +
            def self.create_user(user,auth_session= "")
         | 
| 24 | 
            +
              password_sha = Digest::SHA1.hexdigest(user[:password] + user[:salt])              
         | 
| 14 25 |  | 
| 15 | 
            -
               | 
| 16 | 
            -
             | 
| 26 | 
            +
              user_hash = { :type => "user",
         | 
| 27 | 
            +
                               :name => user[:username],
         | 
| 28 | 
            +
                               :password_sha => password_sha,
         | 
| 29 | 
            +
                               :salt => user[:salt],
         | 
| 30 | 
            +
                               :roles => user[:roles]
         | 
| 31 | 
            +
                              }
         | 
| 32 | 
            +
               
         | 
| 33 | 
            +
               str = Yajl::Encoder.encode(user_hash)
         | 
| 34 | 
            +
               set_address
         | 
| 35 | 
            +
               begin
         | 
| 36 | 
            +
                response = RestClient.put 'http://' + @address + ':' + @port + '/_users/org.couchdb.user:' + URI.escape(user[:username]), str,{:cookies => {"AuthSession" => auth_session}}
         | 
| 37 | 
            +
                hash = Yajl::Parser.parse(response.to_str)
         | 
| 38 | 
            +
              rescue => e
         | 
| 39 | 
            +
                hash = Yajl::Parser.parse(e.response.to_s)
         | 
| 40 | 
            +
                raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
         | 
| 41 | 
            +
              end 
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            #add security object
         | 
| 46 | 
            +
            def self.set_security(db_name, data,auth_session="")
         | 
| 47 | 
            +
              security_data = Yajl::Encoder.encode(data)
         | 
| 48 | 
            +
              set_address
         | 
| 49 | 
            +
              begin
         | 
| 50 | 
            +
              response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/_security/',security_data, {:cookies => {"AuthSession" => auth_session}}
         | 
| 51 | 
            +
              hash = Yajl::Parser.parse(response.to_str)
         | 
| 52 | 
            +
              rescue => e
         | 
| 53 | 
            +
               hash = Yajl::Parser.parse(e.response.to_s)
         | 
| 54 | 
            +
               raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
         | 
| 55 | 
            +
              end 
         | 
| 56 | 
            +
            end
         | 
| 57 | 
            +
            #get security object
         | 
| 58 | 
            +
            def self.get_security(db_name, auth_session="")
         | 
| 59 | 
            +
             set_address
         | 
| 60 | 
            +
             begin
         | 
| 61 | 
            +
              response = RestClient.get 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/_security/', {:cookies => {"AuthSession" => auth_session}}
         | 
| 62 | 
            +
              hash = Yajl::Parser.parse(response.to_str)
         | 
| 63 | 
            +
              rescue => e
         | 
| 64 | 
            +
              hash = Yajl::Parser.parse(e.response.to_s)
         | 
| 65 | 
            +
              raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
         | 
| 66 | 
            +
             end 
         | 
| 67 | 
            +
            end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
             | 
| 70 | 
            +
            #login to couchdb
         | 
| 71 | 
            +
            def self.login(username, password)
         | 
| 72 | 
            +
              set_address
         | 
| 73 | 
            +
              data = 'name=' + username + '&password=' + password
         | 
| 74 | 
            +
              begin
         | 
| 75 | 
            +
               response = RestClient.post 'http://' + @address + ':' + @port + '/_session/', data, {:content_type => 'application/x-www-form-urlencoded'}
         | 
| 76 | 
            +
               response.cookies
         | 
| 77 | 
            +
              rescue => e
         | 
| 78 | 
            +
               hash = Yajl::Parser.parse(e.response.to_s)
         | 
| 79 | 
            +
               raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
         | 
| 80 | 
            +
             end
         | 
| 81 | 
            +
            end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
            #couchdb configuration api
         | 
| 84 | 
            +
            def self.set_config(data,auth_session = "") 
         | 
| 85 | 
            +
              section = data[:section]
         | 
| 86 | 
            +
              key = data[:key] 
         | 
| 87 | 
            +
              value = data[:value]
         | 
| 88 | 
            +
              json_data = Yajl::Encoder.encode(value)
         | 
| 89 | 
            +
              set_address
         | 
| 90 | 
            +
              begin
         | 
| 91 | 
            +
               response = RestClient.put 'http://' + @address + ':' + @port + '/_config/' + URI.escape(section) + '/' + URI.escape(key),json_data, {:cookies => {"AuthSession" => auth_session}}
         | 
| 92 | 
            +
               hash = Yajl::Parser.parse(response.to_str)
         | 
| 93 | 
            +
              rescue => e
         | 
| 94 | 
            +
               hash = Yajl::Parser.parse(e.response.to_s)
         | 
| 95 | 
            +
               raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
         | 
| 96 | 
            +
             end
         | 
| 97 | 
            +
            end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
            def self.delete_config(data,auth_session = "") 
         | 
| 100 | 
            +
              section = data[:section]
         | 
| 101 | 
            +
              key = data[:key] 
         | 
| 102 | 
            +
              set_address
         | 
| 103 | 
            +
              begin
         | 
| 104 | 
            +
               response = RestClient.delete 'http://' + @address + ':' + @port + '/_config/' + URI.escape(section) + '/' + URI.escape(key), {:cookies => {"AuthSession" => auth_session}}
         | 
| 105 | 
            +
               hash = Yajl::Parser.parse(response.to_str)
         | 
| 106 | 
            +
              rescue => e
         | 
| 107 | 
            +
               hash = Yajl::Parser.parse(e.response.to_s)
         | 
| 108 | 
            +
               raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
         | 
| 109 | 
            +
             end
         | 
| 110 | 
            +
            end
         | 
| 111 | 
            +
             | 
| 112 | 
            +
             | 
| 113 | 
            +
            def self.get_config(data,auth_session = "") 
         | 
| 114 | 
            +
              section = data[:section]
         | 
| 115 | 
            +
              key = data[:key] 
         | 
| 116 | 
            +
              set_address
         | 
| 117 | 
            +
              begin
         | 
| 118 | 
            +
               response = RestClient.get 'http://' + @address + ':' + @port + '/_config/' + URI.escape(section) + '/' + URI.escape(key), {:cookies => {"AuthSession" => auth_session}}
         | 
| 119 | 
            +
               hash = Yajl::Parser.parse(response.to_str)
         | 
| 120 | 
            +
              rescue => e
         | 
| 121 | 
            +
               hash = Yajl::Parser.parse(e.response.to_s)
         | 
| 122 | 
            +
               raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
         | 
| 123 | 
            +
             end
         | 
| 124 | 
            +
            end
         | 
| 125 | 
            +
             | 
| 126 | 
            +
            #create a document 
         | 
| 127 | 
            +
              def self.create_doc( doc,auth_session = "")  
         | 
| 17 128 | 
             
                  db_name =  doc[:database]
         | 
| 18 129 | 
             
                  doc_id = doc[:doc_id]
         | 
| 19 130 | 
             
                  data = doc[:data]
         | 
| 20 131 | 
             
                  json_data = Yajl::Encoder.encode(data)
         | 
| 21 132 | 
             
                  set_address
         | 
| 22 133 | 
             
                  begin
         | 
| 23 | 
            -
                     response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id),json_data, {: | 
| 134 | 
            +
                     response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id),json_data, {:cookies => {"AuthSession" => auth_session}}
         | 
| 24 135 | 
             
                     hash = Yajl::Parser.parse(response.to_str)
         | 
| 25 136 | 
             
                   rescue => e
         | 
| 26 137 | 
             
                     hash = Yajl::Parser.parse(e.response.to_s)
         | 
| @@ -29,14 +140,14 @@ module Document | |
| 29 140 | 
             
              end
         | 
| 30 141 |  | 
| 31 142 | 
             
              #edit a document
         | 
| 32 | 
            -
              def self. | 
| 143 | 
            +
              def self.edit_doc(doc,auth_session = "")
         | 
| 33 144 | 
             
                  db_name = doc[:database]
         | 
| 34 145 | 
             
                  doc_id = doc[:doc_id]
         | 
| 35 146 | 
             
                  data = doc[:data]
         | 
| 36 147 | 
             
                  json_data = Yajl::Encoder.encode(data)
         | 
| 37 148 | 
             
                  set_address
         | 
| 38 149 | 
             
                  begin
         | 
| 39 | 
            -
                    response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id), json_data, {: | 
| 150 | 
            +
                    response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id), json_data, {:cookies => {"AuthSession" => auth_session}}
         | 
| 40 151 | 
             
                     hash = Yajl::Parser.parse(response.to_str)
         | 
| 41 152 | 
             
                   rescue => e
         | 
| 42 153 | 
             
                     hash = Yajl::Parser.parse(e.response.to_s)
         | 
| @@ -45,36 +156,36 @@ module Document | |
| 45 156 | 
             
              end
         | 
| 46 157 |  | 
| 47 158 | 
             
             #update a doc
         | 
| 48 | 
            -
             def self. | 
| 159 | 
            +
             def self.update_doc(doc,auth_session = "")
         | 
| 49 160 | 
             
                  db_name = doc[:database]
         | 
| 50 161 | 
             
                  doc_id = doc[:doc_id]
         | 
| 51 162 | 
             
                  data = doc[:data]
         | 
| 52 163 | 
             
                  doc = {:database => db_name, :doc_id => doc_id}
         | 
| 53 | 
            -
                  options = Couchdb.view doc 
         | 
| 164 | 
            +
                  options = Couchdb.view doc,auth_session 
         | 
| 54 165 | 
             
                  options = options.merge(data)
         | 
| 55 166 | 
             
                  doc = {:database => db_name, :doc_id => doc_id, :data => options}
         | 
| 56 | 
            -
                   | 
| 167 | 
            +
                  edit_doc doc,auth_session
         | 
| 57 168 | 
             
             end
         | 
| 58 169 |  | 
| 59 170 | 
             
            #delete document
         | 
| 60 | 
            -
             def self. | 
| 171 | 
            +
             def self.delete_doc(doc,auth_session = "")  
         | 
| 61 172 | 
             
               db_name = doc[:database]
         | 
| 62 173 | 
             
               doc_id = doc[:doc_id]
         | 
| 63 174 | 
             
               doc = {:database => db_name, :doc_id => doc_id}
         | 
| 64 | 
            -
               hash = Couchdb.view doc
         | 
| 175 | 
            +
               hash = Couchdb.view doc,auth_session
         | 
| 65 176 | 
             
               doc = {:database => db_name, :doc_id => doc_id, :rev => hash["_rev"]}
         | 
| 66 | 
            -
               delete_rev | 
| 177 | 
            +
               delete_rev(doc,auth_session)
         | 
| 67 178 | 
             
             end
         | 
| 68 179 |  | 
| 69 180 |  | 
| 70 181 | 
             
             #delete a doc by rev#
         | 
| 71 | 
            -
             def self.delete_rev(doc)
         | 
| 182 | 
            +
             def self.delete_rev(doc,auth_session = "")
         | 
| 72 183 | 
             
               db_name = doc[:database]
         | 
| 73 184 | 
             
               doc_id = doc[:doc_id]
         | 
| 74 185 | 
             
               rev = doc[:rev]
         | 
| 75 186 | 
             
               set_address
         | 
| 76 187 | 
             
               begin 
         | 
| 77 | 
            -
                response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(db_name)  + '/' + URI.escape(doc_id) + '?rev=' + rev, {: | 
| 188 | 
            +
                response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(db_name)  + '/' + URI.escape(doc_id) + '?rev=' + rev, {:cookies => {"AuthSession" => auth_session}}
         | 
| 78 189 | 
             
                  hash = Yajl::Parser.parse(response.to_str)
         | 
| 79 190 | 
             
                rescue => e
         | 
| 80 191 | 
             
                 hash = Yajl::Parser.parse(e.response.to_s)
         | 
| @@ -82,24 +193,12 @@ module Document | |
| 82 193 | 
             
                end
         | 
| 83 194 | 
             
             end
         | 
| 84 195 |  | 
| 85 | 
            -
               class << self
         | 
| 86 | 
            -
                   attr_accessor :address 
         | 
| 87 | 
            -
                   attr_accessor :port 
         | 
| 88 | 
            -
                 def set_address
         | 
| 89 | 
            -
                  if @address == nil && port == nil
         | 
| 90 | 
            -
                     @address = '127.0.0.1'
         | 
| 91 | 
            -
                     @port = '5984'
         | 
| 92 | 
            -
                  end 
         | 
| 93 | 
            -
                 end
         | 
| 94 | 
            -
              end
         | 
| 95 | 
            -
            end
         | 
| 96 196 |  | 
| 97 | 
            -
            module Couchdb
         | 
| 98 197 | 
             
              #create a database if one with the same name doesn't already exist
         | 
| 99 | 
            -
              def self.create(database_name)
         | 
| 198 | 
            +
              def self.create(database_name,auth_session = "")
         | 
| 100 199 | 
             
                   set_address
         | 
| 101 200 | 
             
                   begin
         | 
| 102 | 
            -
                     response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:content_type => :json}
         | 
| 201 | 
            +
                     response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:content_type => :json},{:cookies => {"AuthSession" => auth_session}}
         | 
| 103 202 | 
             
                     hash = Yajl::Parser.parse(response.to_str)
         | 
| 104 203 | 
             
                   rescue => e
         | 
| 105 204 | 
             
                     hash = Yajl::Parser.parse(e.response.to_s)
         | 
| @@ -108,10 +207,10 @@ module Couchdb | |
| 108 207 | 
             
              end
         | 
| 109 208 |  | 
| 110 209 | 
             
             #delete a database
         | 
| 111 | 
            -
             def self.delete(database_name)
         | 
| 210 | 
            +
             def self.delete(database_name,auth_session = "")
         | 
| 112 211 | 
             
                  set_address
         | 
| 113 212 | 
             
                   begin
         | 
| 114 | 
            -
                     response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {: | 
| 213 | 
            +
                     response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:cookies => {"AuthSession" => auth_session}}
         | 
| 115 214 | 
             
                     hash = Yajl::Parser.parse(response.to_str)
         | 
| 116 215 | 
             
                   rescue => e
         | 
| 117 216 | 
             
                     hash = Yajl::Parser.parse(e.response.to_s)
         | 
| @@ -120,10 +219,10 @@ module Couchdb | |
| 120 219 | 
             
             end
         | 
| 121 220 |  | 
| 122 221 | 
             
             #return a list of all databases
         | 
| 123 | 
            -
             def self.all
         | 
| 222 | 
            +
             def self.all(auth_session = "")
         | 
| 124 223 | 
             
                  set_address
         | 
| 125 224 | 
             
                   begin
         | 
| 126 | 
            -
                     response = RestClient.get 'http://' + @address + ':' + @port + '/_all_dbs', {: | 
| 225 | 
            +
                     response = RestClient.get 'http://' + @address + ':' + @port + '/_all_dbs', {:cookies => {"AuthSession" => auth_session}}
         | 
| 127 226 | 
             
                     hash = Yajl::Parser.parse(response.to_str)
         | 
| 128 227 | 
             
                   rescue => e
         | 
| 129 228 | 
             
                       raise e
         | 
| @@ -131,12 +230,12 @@ module Couchdb | |
| 131 230 | 
             
             end
         | 
| 132 231 |  | 
| 133 232 | 
             
            ##view a document 
         | 
| 134 | 
            -
            def self.view(doc)
         | 
| 233 | 
            +
            def self.view(doc,auth_session = "")
         | 
| 135 234 | 
             
             set_address
         | 
| 136 235 | 
             
             db_name = doc[:database]
         | 
| 137 236 | 
             
             doc_id = doc[:doc_id]
         | 
| 138 237 | 
             
              begin
         | 
| 139 | 
            -
               response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/' + doc_id
         | 
| 238 | 
            +
               response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/' + doc_id,{:cookies => {"AuthSession" => auth_session}}
         | 
| 140 239 | 
             
               hash = Yajl::Parser.parse(response.to_str)
         | 
| 141 240 | 
             
              rescue => e
         | 
| 142 241 | 
             
               hash = Yajl::Parser.parse(e.response.to_s)
         | 
| @@ -145,16 +244,16 @@ def self.view(doc) | |
| 145 244 | 
             
            end
         | 
| 146 245 |  | 
| 147 246 | 
             
            #query a permanent view
         | 
| 148 | 
            -
            def self.find(doc,key=nil)
         | 
| 247 | 
            +
            def self.find(doc,auth_session = "",key=nil)
         | 
| 149 248 | 
             
             set_address
         | 
| 150 249 | 
             
             db_name = doc[:database]
         | 
| 151 250 | 
             
             design_doc_name = doc[:design_doc]
         | 
| 152 251 | 
             
             view_name = doc[:view]
         | 
| 153 252 | 
             
               begin
         | 
| 154 253 | 
             
                if key == nil
         | 
| 155 | 
            -
                 response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name
         | 
| 254 | 
            +
                 response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name,{:cookies => {"AuthSession" => auth_session}}
         | 
| 156 255 | 
             
                else
         | 
| 157 | 
            -
                 response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name + URI.escape('?key="' + key + '"')
         | 
| 256 | 
            +
                 response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name + URI.escape('?key="' + key + '"'),{:cookies => {"AuthSession" => auth_session}}
         | 
| 158 257 | 
             
                end
         | 
| 159 258 | 
             
                 hash = Yajl::Parser.parse(response.to_str)
         | 
| 160 259 | 
             
                 rows = hash["rows"]
         | 
| @@ -172,7 +271,7 @@ def self.find(doc,key=nil) | |
| 172 271 | 
             
            end
         | 
| 173 272 |  | 
| 174 273 | 
             
            #create a design document with views
         | 
| 175 | 
            -
            def self.create_design(doc)
         | 
| 274 | 
            +
            def self.create_design(doc,auth_session = "")
         | 
| 176 275 | 
             
             set_address
         | 
| 177 276 | 
             
             db_name = doc[:database]
         | 
| 178 277 | 
             
             design_doc_name = doc[:design_doc]
         | 
| @@ -188,7 +287,7 @@ def self.create_design(doc) | |
| 188 287 |  | 
| 189 288 | 
             
              begin
         | 
| 190 289 |  | 
| 191 | 
            -
               response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, str, {: | 
| 290 | 
            +
               response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, str, {:cookies => {"AuthSession" => auth_session}}
         | 
| 192 291 | 
             
                hash = Yajl::Parser.parse(response.to_str)
         | 
| 193 292 | 
             
              rescue => e
         | 
| 194 293 | 
             
                hash = Yajl::Parser.parse(e.response.to_s)
         | 
| @@ -197,7 +296,7 @@ def self.create_design(doc) | |
| 197 296 | 
             
            end
         | 
| 198 297 |  | 
| 199 298 | 
             
            #Query view, create view on fly if it dosen't already exist
         | 
| 200 | 
            -
            def self.find_on_fly(doc, key = nil)  
         | 
| 299 | 
            +
            def self.find_on_fly(doc,auth_session = "",key = nil)  
         | 
| 201 300 | 
             
               db_name = doc[:database]
         | 
| 202 301 | 
             
               design_doc = doc[:design_doc]
         | 
| 203 302 | 
             
               view = doc[:view]
         | 
| @@ -205,17 +304,17 @@ def self.find_on_fly(doc, key = nil) | |
| 205 304 |  | 
| 206 305 | 
             
               begin 
         | 
| 207 306 | 
             
                  if( key == nil)
         | 
| 208 | 
            -
                   docs = find(:database => db_name, :design_doc => design_doc, :view => view) 
         | 
| 307 | 
            +
                   docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session) 
         | 
| 209 308 | 
             
                  else
         | 
| 210 | 
            -
                   docs = find({:database => db_name, :design_doc => design_doc, :view => view},key) 
         | 
| 309 | 
            +
                   docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key) 
         | 
| 211 310 | 
             
                  end
         | 
| 212 311 | 
             
                 rescue CouchdbException => e
         | 
| 213 312 | 
             
                    document = { :database => db_name, :design_doc => design_doc, :json_doc => json_doc}
         | 
| 214 | 
            -
                    create_design document | 
| 313 | 
            +
                    create_design document,auth_session
         | 
| 215 314 | 
             
                    if( key == nil)
         | 
| 216 | 
            -
                      docs = find(:database => db_name, :design_doc => design_doc, :view => view) 
         | 
| 315 | 
            +
                      docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session) 
         | 
| 217 316 | 
             
                    else
         | 
| 218 | 
            -
                      docs = find({:database => db_name, :design_doc => design_doc, :view => view},key) 
         | 
| 317 | 
            +
                      docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key) 
         | 
| 219 318 | 
             
                    end
         | 
| 220 319 | 
             
                  end
         | 
| 221 320 | 
             
                return docs
         | 
| @@ -224,7 +323,7 @@ def self.find_on_fly(doc, key = nil) | |
| 224 323 |  | 
| 225 324 | 
             
            #add a finder method to the database
         | 
| 226 325 | 
             
            #this creates a find by key method
         | 
| 227 | 
            -
            def self.add_finder(options)
         | 
| 326 | 
            +
            def self.add_finder(options,auth_session = "")
         | 
| 228 327 | 
             
             set_address 
         | 
| 229 328 | 
             
             db_name = options[:database]
         | 
| 230 329 | 
             
             key = options[:key] 
         | 
| @@ -243,7 +342,7 @@ def self.add_finder(options) | |
| 243 342 | 
             
            }'
         | 
| 244 343 |  | 
| 245 344 | 
             
             begin  
         | 
| 246 | 
            -
              response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {: | 
| 345 | 
            +
              response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:cookies => {"AuthSession" => auth_session}}
         | 
| 247 346 | 
             
             rescue => e
         | 
| 248 347 | 
             
                hash = Yajl::Parser.parse(e.response.to_s)
         | 
| 249 348 | 
             
                raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
         | 
| @@ -251,7 +350,7 @@ def self.add_finder(options) | |
| 251 350 | 
             
            end
         | 
| 252 351 |  | 
| 253 352 | 
             
            #find by key 
         | 
| 254 | 
            -
            def self.find_by(options)
         | 
| 353 | 
            +
            def self.find_by(options,auth_session = "")
         | 
| 255 354 | 
             
             set_address 
         | 
| 256 355 | 
             
             db_name = options[:database]
         | 
| 257 356 | 
             
             index =  options.keys[1].to_s
         | 
| @@ -261,21 +360,21 @@ def self.find_by(options) | |
| 261 360 |  | 
| 262 361 | 
             
             begin 
         | 
| 263 362 | 
             
              view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
         | 
| 264 | 
            -
              docs = find view,search_term
         | 
| 363 | 
            +
              docs = find view,auth_session,search_term
         | 
| 265 364 | 
             
             rescue CouchdbException => e
         | 
| 266 365 | 
             
                #add a finder/index if one doesn't already exist in the database
         | 
| 267 366 | 
             
                #then find_by_key
         | 
| 268 | 
            -
                add_finder(:database => db_name, :key => index)
         | 
| 269 | 
            -
                docs = find view,search_term
         | 
| 367 | 
            +
                add_finder({:database => db_name, :key => index},auth_session)
         | 
| 368 | 
            +
                docs = find view,auth_session,search_term
         | 
| 270 369 | 
             
             end
         | 
| 271 370 | 
             
             return docs
         | 
| 272 371 | 
             
            end
         | 
| 273 372 |  | 
| 274 373 | 
             
             #return a list of all docs in the database
         | 
| 275 | 
            -
            def self.docs_from(database_name)
         | 
| 374 | 
            +
            def self.docs_from(database_name,auth_session = "")
         | 
| 276 375 | 
             
              set_address
         | 
| 277 376 | 
             
              begin
         | 
| 278 | 
            -
                     response = RestClient.get 'http://' + @address + ':' + @port + '/' + URI.escape(database_name) + '/_all_docs?include_docs=true', | 
| 377 | 
            +
                     response = RestClient.get 'http://' + @address + ':' + @port + '/' + URI.escape(database_name) + '/_all_docs?include_docs=true',{:cookies => {"AuthSession" => auth_session}}
         | 
| 279 378 | 
             
                     hash = Yajl::Parser.parse(response.to_str)
         | 
| 280 379 | 
             
                     rows = hash["rows"]
         | 
| 281 380 | 
             
                     count = 0 
         | 
| @@ -294,9 +393,12 @@ end | |
| 294 393 | 
             
             class << self
         | 
| 295 394 | 
             
                   attr_accessor :address 
         | 
| 296 395 | 
             
                   attr_accessor :port 
         | 
| 396 | 
            +
             | 
| 297 397 | 
             
                 def set_address()
         | 
| 298 | 
            -
                  if @address == nil  | 
| 398 | 
            +
                  if @address == nil 
         | 
| 299 399 | 
             
                     @address = '127.0.0.1'
         | 
| 400 | 
            +
                  end
         | 
| 401 | 
            +
                  if @port == nil
         | 
| 300 402 | 
             
                     @port = '5984'
         | 
| 301 403 | 
             
                  end 
         | 
| 302 404 | 
             
                 end
         |