leanback 0.1.6

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ source "http://gemcutter.org"
3
+ # Add dependencies required to use your gem here.
4
+ # Example:
5
+ # gem "activesupport", ">= 2.3.5"
6
+ gem "rest-client"
7
+ gem "yajl-ruby"
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "shoulda", ">= 0"
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.5.2"
15
+ gem "rcov", ">= 0"
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ remote: http://gemcutter.org/
4
+ specs:
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ mime-types (1.16)
11
+ rake (0.8.7)
12
+ rcov (0.9.9)
13
+ rest-client (1.6.1)
14
+ mime-types (>= 1.16)
15
+ shoulda (2.11.3)
16
+ yajl-ruby (0.8.2)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ bundler (~> 1.0.0)
23
+ jeweler (~> 1.5.2)
24
+ rcov
25
+ rest-client
26
+ shoulda
27
+ yajl-ruby
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Obi Akubue, obi-akubue.org
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,111 @@
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
+ ==Install
14
+
15
+ gem install leanback
16
+
17
+
18
+ ==Usage
19
+
20
+ Require the leanback Gem
21
+ require 'leanback'
22
+
23
+ Create a CouchDB database
24
+ Couchdb.create 'contacts'
25
+ # => {"ok"=>true}
26
+
27
+ Delete a database
28
+ Couchdb.delete 'contacts'
29
+ # => {"ok"=>true}
30
+
31
+ Return a list of all Databases
32
+ Couchdb.all
33
+ # => ["maps", "inventory", "monitors", "contacts", "books"]
34
+
35
+ Create a new document
36
+ data = {:firstname => 'Linda',
37
+ :lastname =>'smith',
38
+ :phone => '212-234-1234',
39
+ :email =>'john@mail.com'}
40
+
41
+ doc = {:database => 'contacts', :doc_id => 'Linda', :data => data}
42
+ Document.create doc
43
+ # => {"ok"=>true, "id"=>"Linda", "rev"=>"1-6f16274513f51e922ff1f745452a92b6"}
44
+ This will create a new document in the 'contacts' database, with document id 'Linda'.
45
+
46
+ Lets edit that document to change the email address, phone number, and add a gender
47
+ data = {:firstname => 'Linda',
48
+ :lastname =>'smith',
49
+ :email => 'linda@mail.com',
50
+ :gender=>'female',
51
+ :phone =>'718-245-5611',
52
+ :_rev=>'1-6f16274513f51e922ff1f745452a92b6'}
53
+
54
+ doc = {:database => 'contacts', :doc_id => 'Linda', :data => data}
55
+ Document.edit doc
56
+ # => {"ok"=>true, "id"=>"Linda", "rev"=>"2-e813a0e902e3ac114400ff3959a2adde"}
57
+
58
+ Delete the document
59
+ doc = {:database => 'contacts', :doc_id => 'Linda', :rev => '2-e813a0e902e3ac114400ff3959a2adde'}
60
+ Document.delete doc
61
+ # => {"ok"=>true, "id"=>"Linda", "rev"=>"3-48580d1806983b32cb03f114efb064e3"}
62
+
63
+ Retrieve all documents in a database
64
+ docs = Couchdb.docs_from 'contacts'
65
+ docs.each do |d|
66
+ puts d["_rev"]
67
+ puts d["_id"]
68
+ puts d["firstname"]
69
+ puts d["lastname"]
70
+ puts d["email"]
71
+ puts d["phone"]
72
+ end
73
+
74
+ Attempting to create a database that already exists
75
+ Couchdb.create 'contacts'
76
+ # => {"error"=>"file_exists", "reason"=>"The database could not be created, the file already exists."}
77
+
78
+ Attempting to delete a database that no longer exists
79
+ Couchdb.delete 'more_books'
80
+ # => {"error"=>"not_found", "reason"=>"missing"}
81
+
82
+ 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" => ...}
83
+
84
+ You can always check this value
85
+ hash = Couchdb.delete 'schools'
86
+ puts hash.inspect
87
+
88
+ Leanback uses the default Couchdb bind address http://127.0.0.1:5984. To use a different bind address;
89
+ Couchdb.address = '192.168.2.16'
90
+ Couchdb.port = '6000'
91
+ Couchdb.all
92
+ ...
93
+ Document.address = '192.168.2.16'
94
+ Document.port = '6000'
95
+ docs = Couchdb.docs_from 'contacts'
96
+
97
+ To change it back to default bind address at anytime, simply set the values to nil
98
+ Couchdb.address = nil
99
+ Couchdb.port = nil
100
+ Couchdb.all
101
+ ...
102
+ Document.address = nil
103
+ Document.port = nil
104
+ docs = Couchdb.docs_from 'contacts'
105
+
106
+
107
+ == Copyright
108
+
109
+ Copyright (c) 2011 Obi Akubue. See LICENSE.txt for
110
+ further details.
111
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "leanback"
16
+ gem.homepage = "http://github.com/obi-a/leanback"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{lightweight Ruby interface to CouchDB}
19
+ gem.description = %Q{lightweight Ruby interface to CouchDB}
20
+ gem.email = "obioraakubue@yahoo.com"
21
+ gem.authors = ["Obi Akubue"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "leanback #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.6
data/lib/leanback.rb ADDED
@@ -0,0 +1,127 @@
1
+ require 'rest_client'
2
+ require 'yajl'
3
+
4
+ module Document
5
+
6
+ #create a document
7
+ def self.create( doc)
8
+ db_name = doc[:database]
9
+ doc_id = doc[:doc_id]
10
+ data = doc[:data]
11
+ json_data = Yajl::Encoder.encode(data)
12
+ set_address
13
+ begin
14
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id),json_data, {:content_type => :json, :accept => :json}
15
+ hash = Yajl::Parser.parse(response.to_str)
16
+ rescue => e
17
+ hash = Yajl::Parser.parse(e.response.to_s)
18
+ end
19
+ end
20
+
21
+ #edit a document
22
+ def self.edit(doc)
23
+ db_name = doc[:database]
24
+ doc_id = doc[:doc_id]
25
+ data = doc[:data]
26
+ json_data = Yajl::Encoder.encode(data)
27
+ set_address
28
+ begin
29
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id), json_data, {:content_type => :json, :accept => :json}
30
+ hash = Yajl::Parser.parse(response.to_str)
31
+ rescue => e
32
+ hash = Yajl::Parser.parse(e.response.to_s)
33
+ end
34
+ end
35
+
36
+ #delete a doc
37
+ def self.delete(doc)
38
+ db_name = doc[:database]
39
+ doc_id = doc[:doc_id]
40
+ rev = doc[:rev]
41
+ set_address
42
+ begin
43
+ response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id) + '?rev=' + rev, {:content_type => :json}
44
+ hash = Yajl::Parser.parse(response.to_str)
45
+ rescue => e
46
+ hash = Yajl::Parser.parse(e.response.to_s)
47
+ end
48
+ end
49
+
50
+ class << self
51
+ attr_accessor :address
52
+ attr_accessor :port
53
+ def set_address
54
+ if @address == nil && port == nil
55
+ @address = '127.0.0.1'
56
+ @port = '5984'
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ module Couchdb
63
+ #create a database if one with the same name doesn't already exist
64
+ def self.create(database_name)
65
+ set_address
66
+ begin
67
+ response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:content_type => :json}
68
+ hash = Yajl::Parser.parse(response.to_str)
69
+ rescue => e
70
+ hash = Yajl::Parser.parse(e.response.to_s)
71
+ end
72
+ end
73
+
74
+
75
+ #delete a database
76
+ def self.delete(database_name)
77
+ set_address
78
+ begin
79
+ response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:content_type => :json}
80
+ hash = Yajl::Parser.parse(response.to_str)
81
+ rescue => e
82
+ hash = Yajl::Parser.parse(e.response.to_s)
83
+ end
84
+ end
85
+
86
+ #return a list of all databases
87
+ def self.all
88
+ set_address
89
+ begin
90
+ response = RestClient.get 'http://' + @address + ':' + @port + '/_all_dbs', {:content_type => :json}
91
+ hash = Yajl::Parser.parse(response.to_str)
92
+ rescue => e
93
+ raise e
94
+ end
95
+ end
96
+
97
+ #return a list of all docs in the database
98
+ def self.docs_from(database_name)
99
+ set_address
100
+ begin
101
+ response = RestClient.get 'http://' + @address + ':' + @port + '/' + URI.escape(database_name) + '/_all_docs?include_docs=true', {:content_type => :json}
102
+ hash = Yajl::Parser.parse(response.to_str)
103
+ rows = hash["rows"]
104
+ only_rows = []
105
+ count = 0
106
+ rows.each do |row|
107
+ only_rows[count] = row["doc"]
108
+ count = count + 1
109
+ end
110
+ return only_rows
111
+ rescue => e
112
+ hash = Yajl::Parser.parse(e.response.to_s)
113
+ end
114
+ end
115
+
116
+
117
+ class << self
118
+ attr_accessor :address
119
+ attr_accessor :port
120
+ def set_address()
121
+ if @address == nil && port == nil
122
+ @address = '127.0.0.1'
123
+ @port = '5984'
124
+ end
125
+ end
126
+ end
127
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'leanback'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,85 @@
1
+ path = File.expand_path(File.dirname(__FILE__))
2
+
3
+ require path + "/helper.rb"
4
+
5
+ class TestLeanback < Test::Unit::TestCase
6
+ #should "probably rename this file and start testing for real" do
7
+ # flunk "hey buddy, you should probably rename this file and start testing for real"
8
+ #end
9
+
10
+
11
+ should "create a database if it doesn't already exist" do
12
+ hash = Couchdb.create 'staff'
13
+ puts hash.inspect
14
+ end
15
+
16
+ should "create a database if it doesn't already exist" do
17
+ hash = Couchdb.create 'contacts'
18
+ puts hash.inspect
19
+ end
20
+
21
+ #should "return error message illegal database name" do
22
+ # hash = Couchdb.create 'more books'
23
+ # puts hash.inspect
24
+ # end
25
+
26
+ should "return a display a list of all databases" do
27
+ databases = Couchdb.all
28
+ databases.each do |db_name|
29
+ puts db_name
30
+ end
31
+ end
32
+
33
+ should "delete a database" do
34
+ hash = Couchdb.delete 'staff'
35
+ puts hash.inspect
36
+ end
37
+
38
+ should "create a document" do
39
+ data = {:firstname => 'Mary', :lastname =>'smith', :phone => '212-234-1234',:email =>'john@mail.com'}
40
+ doc = {:database => 'contacts', :doc_id => 'Mary', :data => data}
41
+ hash = Document.create doc
42
+ puts hash.inspect
43
+ end
44
+
45
+ should "edit a document" do
46
+ data = {:firstname => 'john', :lastname =>'smith', :email => 'john@mail.com',:gender=>'male', :_rev=>'2-e813a0e902e3ac114400ff3959a2adde'}
47
+ doc = {:database => 'contacts', :doc_id => 'john', :data => data}
48
+ hash = Document.edit doc
49
+ puts hash.inspect
50
+ end
51
+
52
+ should "delete a document" do
53
+ doc = {:database => 'contacts', :doc_id => 'john', :rev => '3-be02e80490f8e9e610d9a9e33d752316'}
54
+ hash = Document.delete doc
55
+ puts hash.inspect
56
+ end
57
+
58
+ should "display all documents in the database" do
59
+ docs = Couchdb.docs_from 'monitors'
60
+
61
+ docs.each do |d|
62
+ puts d["_rev"]
63
+ puts d["_id"]
64
+ puts d["every"]
65
+ puts d["monitor"]
66
+ puts d["url"]
67
+ puts d["test"]
68
+ puts d["contact"]
69
+ puts d["via"]
70
+ puts d["notify_interval"]
71
+ end
72
+ end
73
+
74
+ should " switch to default bind address" do
75
+ Couchdb.address = nil
76
+ Couchdb.port = nil
77
+ Couchdb.all
78
+ end
79
+
80
+ end
81
+
82
+
83
+
84
+
85
+
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leanback
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 6
9
+ version: 0.1.6
10
+ platform: ruby
11
+ authors:
12
+ - Obi Akubue
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-03 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rest-client
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ prerelease: false
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: yajl-ruby
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: bundler
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 1
68
+ - 0
69
+ - 0
70
+ version: 1.0.0
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: *id004
74
+ - !ruby/object:Gem::Dependency
75
+ name: jeweler
76
+ requirement: &id005 !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 1
83
+ - 5
84
+ - 2
85
+ version: 1.5.2
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: *id005
89
+ - !ruby/object:Gem::Dependency
90
+ name: rcov
91
+ requirement: &id006 !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *id006
102
+ description: lightweight Ruby interface to CouchDB
103
+ email: obioraakubue@yahoo.com
104
+ executables: []
105
+
106
+ extensions: []
107
+
108
+ extra_rdoc_files:
109
+ - LICENSE.txt
110
+ - README.rdoc
111
+ files:
112
+ - .document
113
+ - Gemfile
114
+ - Gemfile.lock
115
+ - LICENSE.txt
116
+ - README.rdoc
117
+ - Rakefile
118
+ - VERSION
119
+ - lib/leanback.rb
120
+ - test/helper.rb
121
+ - test/test_leanback.rb
122
+ has_rdoc: true
123
+ homepage: http://github.com/obi-a/leanback
124
+ licenses:
125
+ - MIT
126
+ post_install_message:
127
+ rdoc_options: []
128
+
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 327802877
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ requirements: []
149
+
150
+ rubyforge_project:
151
+ rubygems_version: 1.3.7
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: lightweight Ruby interface to CouchDB
155
+ test_files:
156
+ - test/helper.rb
157
+ - test/test_leanback.rb