armchair 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Daniel Kirsch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = Armchair
2
+
3
+ Armchair is a <em>very</em> minimal interface to CouchDB. It can only store documents and iterate over all of them.
4
+
5
+ == Usage
6
+
7
+ armchair = Armchair.new 'http://url.to.couch:5984/databasename'
8
+ armchair << { 'foo' => 'bar' } << { 'foo' => 'baz' }
9
+ armchair.each do |doc|
10
+ do_something_useful_with doc
11
+ end
12
+
13
+ Armchair is Enumerable
14
+
15
+ armchair.map { |doc| doc['foo'] }.join # => "barbaz"
16
+ armchair.detect { |doc| doc['foo'] == 'bar' } # => { 'foo' => 'bar' }
17
+
18
+ == Note on Patches/Pull Requests
19
+
20
+ * Fork the project.
21
+ * Make your feature addition or bug fix.
22
+ * Add tests for it. This is important so I don't break it in a
23
+ future version unintentionally.
24
+ * Commit, do not mess with rakefile, version, or history.
25
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
26
+ * Send me a pull request. Bonus points for topic branches.
27
+
28
+ == Copyright
29
+
30
+ Copyright (c) 2010 Daniel Kirsch. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "armchair"
8
+ gem.version = '0.0.1'
9
+ gem.summary = %Q{Minimal CouchDB interface}
10
+ gem.description = %Q{A minimal CouchDB interface that can do nothing but add documents and iterate over documents.}
11
+ gem.email = "danishkirel@gmail.com"
12
+ gem.homepage = "http://github.com/kirel/armchair"
13
+ gem.authors = ["Daniel Kirsch"]
14
+ gem.add_dependency "rest-client", ">= 1.4.1"
15
+ gem.add_dependency "json"
16
+ gem.add_development_dependency "rspec", ">= 1.2.9"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ task :spec => :check_dependencies
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
37
+
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = "armchair #{version}"
40
+ rdoc.rdoc_files.include('README*')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ end
data/armchair.gemspec ADDED
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{armchair}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Daniel Kirsch"]
12
+ s.date = %q{2010-03-11}
13
+ s.description = %q{A minimal CouchDB interface that can do nothing but add documents and iterate over documents.}
14
+ s.email = %q{danishkirel@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "armchair.gemspec",
26
+ "lib/armchair.rb",
27
+ "spec/armchair_spec.rb",
28
+ "spec/spec.opts",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/kirel/armchair}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.6}
35
+ s.summary = %q{Minimal CouchDB interface}
36
+ s.test_files = [
37
+ "spec/armchair_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<rest-client>, [">= 1.4.1"])
47
+ s.add_runtime_dependency(%q<json>, [">= 0"])
48
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
49
+ else
50
+ s.add_dependency(%q<rest-client>, [">= 1.4.1"])
51
+ s.add_dependency(%q<json>, [">= 0"])
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rest-client>, [">= 1.4.1"])
56
+ s.add_dependency(%q<json>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ end
59
+ end
60
+
data/lib/armchair.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'rest_client'
2
+ require 'json'
3
+
4
+ # An Armchair is <em>very</em> a minimal interface to a CouchDB database. It is Enumarable.
5
+ class Armchair
6
+ include Enumerable
7
+
8
+ # Pass in the database url and optionally
9
+ # a <tt>batch_size</tt> which is used when iterating over the armchair in Armchair#each
10
+ # couch = Armchair.new 'http://127.0.0.1:5984/mycouch'
11
+ #
12
+ def initialize dburl, batch_size = 100
13
+ @dburl = dburl
14
+ @batch_size = batch_size
15
+ end
16
+
17
+ # Create the CouchDB database at <tt>dburl</tt> (see Armchair#new) if it does not exist yet
18
+ def create!
19
+ RestClient.get @dburl, :accept => :json do |response|
20
+ case response.code
21
+ when 404
22
+ RestClient.put @dburl, nil, :content_type => :json, :accept => :json
23
+ else
24
+ response.return!
25
+ end
26
+ end
27
+ end
28
+
29
+ # Shift a document into the Armchair. <tt>doc</tt> should be a Hash.
30
+ # armchair << { 'a' => 'document' } << { 'another' => 'document' }
31
+ def << doc
32
+ RestClient.post @dburl, JSON(doc), :content_type => :json, :accept => :json do |response|
33
+ response.return! unless response.code == 201
34
+ end
35
+ self
36
+ end
37
+
38
+ # Returns the size of the Armchair (the number of documents stored).
39
+ def size
40
+ RestClient.get(@dburl + '_all_docs?limit=0', :accept => :json) do |r|
41
+ case r.code
42
+ when 200
43
+ JSON(r.body)['total_rows']
44
+ else
45
+ r.return!
46
+ end
47
+ end
48
+ end
49
+
50
+ # yields each document
51
+ def each
52
+ # iterate in batches of @batch_size
53
+ # initial query
54
+ res = RestClient.get(@dburl + "_all_docs?limit=#{@batch_size+1}&include_docs=true", :accept => :json) do |r|
55
+ case r.code
56
+ when 200
57
+ JSON(r.body)
58
+ else
59
+ r.return!
60
+ end
61
+ end
62
+ rows = res['rows']
63
+ last = rows.size > @batch_size ? rows.pop : nil
64
+ rows.each { |row| doc = row['doc']; yield doc }
65
+ # subsequent queries
66
+ while last
67
+ startkey = last['key']
68
+ res = RestClient.get(@dburl+"_all_docs?startkey=%22#{startkey}%22&limit=#{@batch_size+1}&include_docs=true", :accept => :json) do |r|
69
+ case r.code
70
+ when 200
71
+ JSON(r.body)
72
+ else
73
+ r.return!
74
+ end
75
+ end
76
+ rows = res['rows']
77
+ last = rows.size > @batch_size ? rows.pop : nil
78
+ rows.each { |row| yield row['doc'] }
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ require 'armchair'
4
+
5
+ describe Armchair do
6
+
7
+ before(:each) do
8
+ @dburl = 'http://localhost:5984/test_armchair/'
9
+ @couch = Armchair.new @dburl
10
+ @doc = {'some' => 'doc'}
11
+ end
12
+
13
+ after(:each) do
14
+ begin
15
+ RestClient.delete(@dburl)
16
+ rescue RestClient::ResourceNotFound
17
+ end
18
+ end
19
+
20
+ it "should create the database" do
21
+ @couch.create!
22
+ lambda { RestClient.get(@dburl) }.should_not raise_error(RestClient::ResourceNotFound)
23
+ end
24
+
25
+ it "should insert documents" do
26
+ # just checking if CouchDB API is correctly used
27
+ RestClient.should_receive(:post).with(@dburl, JSON(@doc), :content_type => :json, :accept => :json)
28
+ @couch << @doc
29
+ end
30
+
31
+ describe "with some documents in it" do
32
+
33
+ before(:each) do
34
+ @num = 42
35
+ @couch.create!
36
+ @docs = (1..@num).map { |i| { 'number' => i } }
37
+ @docs.each { |doc| @couch << doc }
38
+ end
39
+
40
+ it "should know the numbers" do
41
+ @couch.size.should == @num
42
+ end
43
+
44
+ it "should iterate over each document" do
45
+ docs = @docs.dup
46
+ @couch.each do |doc|
47
+ docs.delete(docs.detect { |d| d['number'] == doc['number']})
48
+ end
49
+ docs.should be_empty
50
+ end
51
+
52
+ end
53
+
54
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ require 'armchair'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: armchair
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Daniel Kirsch
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-11 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rest-client
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 4
30
+ - 1
31
+ version: 1.4.1
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: json
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 2
56
+ - 9
57
+ version: 1.2.9
58
+ type: :development
59
+ version_requirements: *id003
60
+ description: A minimal CouchDB interface that can do nothing but add documents and iterate over documents.
61
+ email: danishkirel@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .gitignore
72
+ - LICENSE
73
+ - README.rdoc
74
+ - Rakefile
75
+ - armchair.gemspec
76
+ - lib/armchair.rb
77
+ - spec/armchair_spec.rb
78
+ - spec/spec.opts
79
+ - spec/spec_helper.rb
80
+ has_rdoc: true
81
+ homepage: http://github.com/kirel/armchair
82
+ licenses: []
83
+
84
+ post_install_message:
85
+ rdoc_options:
86
+ - --charset=UTF-8
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.3.6
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Minimal CouchDB interface
110
+ test_files:
111
+ - spec/armchair_spec.rb
112
+ - spec/spec_helper.rb