protoncouch 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +127 -0
- data/lib/protoncouch.rb +195 -0
- data/license +21 -0
- data/protoncouch.gemspec +16 -0
- metadata +69 -0
data/README.md
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
![ProtonCouch logo](http://i.8electrons.com/8protons/img/pc/protoncouch.png "ProtonCouch logo")
|
2
|
+
|
3
|
+
#ProtonCouch
|
4
|
+
|
5
|
+
It's a simple and efficient CouchDB client written on Ruby.
|
6
|
+
|
7
|
+
Current solutions like CouchRest are too heavy and have strange behaviour (like raising error when Resource not found). Also they can't manage many databases.
|
8
|
+
|
9
|
+
#How to use (real life examples from 8Protons' project)
|
10
|
+
|
11
|
+
It's simple. Create an object like this:
|
12
|
+
|
13
|
+
require 'protons8/couch'
|
14
|
+
@couch = Protons8::Couch.new({ :account => 'http://user:password@127.0.0.1:5984/accounts', :activations => 'http://user:password@127.0.0.1:5984/email_activations'})
|
15
|
+
|
16
|
+
Now you can query:
|
17
|
+
|
18
|
+
**GET**
|
19
|
+
|
20
|
+
@couch.get(:account, 'test@test.com')
|
21
|
+
|
22
|
+
**PUT (create document with id)**
|
23
|
+
|
24
|
+
@couch.save_with_id(db, id, document)
|
25
|
+
|
26
|
+
**POST (create document with autogenerated id**
|
27
|
+
|
28
|
+
@couch.save(db, document)
|
29
|
+
|
30
|
+
**Save or Update (Document exists? Update. No? Create a new one)**
|
31
|
+
|
32
|
+
@couch.save_or_update(db, id, document)
|
33
|
+
|
34
|
+
**Simple DELETE**
|
35
|
+
|
36
|
+
@couch.delete(db, id)
|
37
|
+
|
38
|
+
**DELETE with revision**
|
39
|
+
|
40
|
+
@couch.delete_rev(db, id, rev)
|
41
|
+
|
42
|
+
**VIEW Example**
|
43
|
+
Spec: view(db, view, params = {})
|
44
|
+
|
45
|
+
@couchdb.view(:accounts, 'accounts/login', { :key => ["login", "hashedpassword"] })
|
46
|
+
|
47
|
+
#Methods (Asking the Right Questions)
|
48
|
+
|
49
|
+
Every query wrapped by help class.
|
50
|
+
|
51
|
+
Base methods are:
|
52
|
+
|
53
|
+
- error?
|
54
|
+
- no_error?
|
55
|
+
- error_name
|
56
|
+
- reason
|
57
|
+
- not_found? = if CouchDB answered { "error": "not_found" }...
|
58
|
+
- unauth?
|
59
|
+
- conflict?
|
60
|
+
- raw = raw document (hash)
|
61
|
+
- attachments
|
62
|
+
|
63
|
+
**GET methods**
|
64
|
+
|
65
|
+
- id
|
66
|
+
- rev
|
67
|
+
- exists?
|
68
|
+
|
69
|
+
**VIEW methods**
|
70
|
+
|
71
|
+
- total_rows
|
72
|
+
- offset
|
73
|
+
- rows = get all rows
|
74
|
+
- any?
|
75
|
+
- empty? = no document found by criteria
|
76
|
+
|
77
|
+
**PUT-POST-DELETE methods**
|
78
|
+
|
79
|
+
- id = id of saved document
|
80
|
+
- rev
|
81
|
+
- ok? = is query okay?
|
82
|
+
|
83
|
+
#Real life examples
|
84
|
+
|
85
|
+
* Check unique of email
|
86
|
+
|
87
|
+
if @couch.view(:account, "accounts/list_emails", { :key => email }).empty?
|
88
|
+
|
89
|
+
* Save email activation code and check query
|
90
|
+
|
91
|
+
if @couch.save_or_update(:activation, email, { :code => @code }).ok?
|
92
|
+
|
93
|
+
* Check email ectivation code
|
94
|
+
|
95
|
+
if @couch.view(:activation, 'email_activations/list_codes', { :key => params['code'] }).any?
|
96
|
+
|
97
|
+
* User
|
98
|
+
|
99
|
+
user = @couch.save(:account, document)
|
100
|
+
if user.ok? and @couch.delete(:activation, document['email']).ok?
|
101
|
+
|
102
|
+
|
103
|
+
#Enjoy!
|
104
|
+
|
105
|
+
#License
|
106
|
+
|
107
|
+
The MIT License
|
108
|
+
|
109
|
+
Copyright (c) 2010 Anthony Sekatski <anthony@8protons.com>
|
110
|
+
|
111
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
112
|
+
of this software and associated documentation files (the "Software"), to deal
|
113
|
+
in the Software without restriction, including without limitation the rights
|
114
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
115
|
+
copies of the Software, and to permit persons to whom the Software is
|
116
|
+
furnished to do so, subject to the following conditions:
|
117
|
+
|
118
|
+
The above copyright notice and this permission notice shall be included in
|
119
|
+
all copies or substantial portions of the Software.
|
120
|
+
|
121
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
122
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
123
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
124
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
125
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
126
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
127
|
+
THE SOFTWARE.
|
data/lib/protoncouch.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
module Protons8
|
7
|
+
class ProtonCouch
|
8
|
+
#Initialization
|
9
|
+
def initialize(constrings)
|
10
|
+
@s = { }
|
11
|
+
#Construct our settings
|
12
|
+
constrings.each do |k,v|
|
13
|
+
#Get URI from value
|
14
|
+
uri = URI.parse(v)
|
15
|
+
#Fill a simple hash without Object (I've no idea why i do this :D)
|
16
|
+
@s[k] = { :host => uri.host, :port => uri.port, :path => uri.path, :user => uri.user, :password => uri.password }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def info(db)
|
21
|
+
CouchResponse::Get.new request( db, Net::HTTP::Get.new("#{@s[db][:path]}/") )
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(db, id, params = {})
|
25
|
+
CouchResponse::Get.new request( db, Net::HTTP::Get.new( "#{@s[db][:path]}/" + id + get_qs(params) ) )
|
26
|
+
end
|
27
|
+
|
28
|
+
def save(db, document)
|
29
|
+
req = Net::HTTP::Post.new( "#{@s[db][:path]}/" )
|
30
|
+
req["content-type"] = "application/json"
|
31
|
+
req.body = document.to_json
|
32
|
+
CouchResponse::PutPostDelete.new request( db, req )
|
33
|
+
end
|
34
|
+
|
35
|
+
def save_with_id(db, id, document)
|
36
|
+
req = Net::HTTP::Put.new( "#{@s[db][:path]}/" + id )
|
37
|
+
req["content-type"] = "application/json"
|
38
|
+
req.body = document.to_json
|
39
|
+
CouchResponse::PutPostDelete.new request( db, req )
|
40
|
+
end
|
41
|
+
|
42
|
+
def save_or_update(db, id, document)
|
43
|
+
response = self.get(db, id)
|
44
|
+
document['_rev'] = response.rev if response.exists?
|
45
|
+
req = Net::HTTP::Put.new( "#{@s[db][:path]}/" + id )
|
46
|
+
req["content-type"] = "application/json"
|
47
|
+
req.body = document.to_json
|
48
|
+
CouchResponse::PutPostDelete.new request( db, req )
|
49
|
+
end
|
50
|
+
|
51
|
+
def delete(db, id)
|
52
|
+
response = self.get(db, id)
|
53
|
+
rev = response.exists? ? response.rev : ''
|
54
|
+
CouchResponse::PutPostDelete.new request( db, Net::HTTP::Delete.new( "#{@s[db][:path]}/#{id}?rev=#{rev}") )
|
55
|
+
end
|
56
|
+
|
57
|
+
def delete_rev(db, id, rev)
|
58
|
+
CouchResponse::PutPostDelete.new request( db, Net::HTTP::Delete.new( "#{@s[db][:path]}/#{id}?rev=#{rev}") )
|
59
|
+
end
|
60
|
+
|
61
|
+
def view(db, view, params = {})
|
62
|
+
CouchResponse::View.new request( db, Net::HTTP::Get.new("#{@s[db][:path]}/_design/#{view.split('/')[0]}/_view/#{view.split('/')[1]}" + get_qs(params) ) )
|
63
|
+
end
|
64
|
+
|
65
|
+
# Private methods - they are shy
|
66
|
+
private
|
67
|
+
|
68
|
+
# Do some request now!
|
69
|
+
def request(db, req)
|
70
|
+
#Create server
|
71
|
+
http = Net::HTTP.new @s[db][:host], @s[db][:port]
|
72
|
+
#Set auth rules
|
73
|
+
req.basic_auth @s[db][:user], @s[db][:password]
|
74
|
+
#Request!
|
75
|
+
JSON.parse(http.request(req).body)
|
76
|
+
#...
|
77
|
+
#Profit!
|
78
|
+
end
|
79
|
+
|
80
|
+
#Get query string from hash
|
81
|
+
def get_qs(params)
|
82
|
+
if params and not params.empty?
|
83
|
+
qs = params.collect do |k,v|
|
84
|
+
v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
|
85
|
+
"#{k}=#{CGI.escape(v.to_s)}"
|
86
|
+
end.join("&")
|
87
|
+
"?#{qs}"
|
88
|
+
else
|
89
|
+
''
|
90
|
+
end
|
91
|
+
end #End of get_qs
|
92
|
+
|
93
|
+
#Several responses classes for convenience
|
94
|
+
class CouchResponse
|
95
|
+
|
96
|
+
#Add several methods to all type of response
|
97
|
+
class BaseResponse
|
98
|
+
def initialize(hash)
|
99
|
+
@hash = hash
|
100
|
+
end
|
101
|
+
|
102
|
+
def error?
|
103
|
+
not @hash['error'].nil?
|
104
|
+
end
|
105
|
+
|
106
|
+
def no_error?
|
107
|
+
@hash['error'].nil?
|
108
|
+
end
|
109
|
+
|
110
|
+
def error_name
|
111
|
+
@hash['error'] || 'No error'
|
112
|
+
end
|
113
|
+
|
114
|
+
def reason
|
115
|
+
@hash['error'] ? @hash['reason'] : 'No reason - everything ok'
|
116
|
+
end
|
117
|
+
|
118
|
+
def not_found?
|
119
|
+
@hash['error'] == 'not_found' ? true : false
|
120
|
+
end
|
121
|
+
|
122
|
+
def unauth?
|
123
|
+
@hash['error'] == 'unauthorized' ? true : false
|
124
|
+
end
|
125
|
+
|
126
|
+
def conflict?
|
127
|
+
@hash['error'] == 'conflict' ? true : false
|
128
|
+
end
|
129
|
+
|
130
|
+
def raw
|
131
|
+
@hash
|
132
|
+
end
|
133
|
+
|
134
|
+
def attachments
|
135
|
+
@hash['_attachments'] || nil
|
136
|
+
end
|
137
|
+
|
138
|
+
def attachment(id)
|
139
|
+
@hash['_attachments'] ? @hash['_attachments'][id] : nil
|
140
|
+
end
|
141
|
+
end #End of BaseResponse
|
142
|
+
|
143
|
+
class Get < BaseResponse
|
144
|
+
def id
|
145
|
+
@hash['_id']
|
146
|
+
end
|
147
|
+
|
148
|
+
def rev
|
149
|
+
@hash['_rev']
|
150
|
+
end
|
151
|
+
|
152
|
+
def exists?
|
153
|
+
@hash['_id'] || false
|
154
|
+
end
|
155
|
+
end #End of Get
|
156
|
+
|
157
|
+
class View < BaseResponse
|
158
|
+
def total_rows
|
159
|
+
@hash['total_rows'] || nil
|
160
|
+
end
|
161
|
+
|
162
|
+
def offset
|
163
|
+
@hash['offset'] || nil
|
164
|
+
end
|
165
|
+
|
166
|
+
def rows
|
167
|
+
@hash['rows'] || nil
|
168
|
+
end
|
169
|
+
|
170
|
+
def any?
|
171
|
+
@hash['rows'] ? (not @hash['rows'][0].nil?) : false
|
172
|
+
end
|
173
|
+
|
174
|
+
def empty?
|
175
|
+
@hash['rows'] ? @hash['rows'].empty? : false
|
176
|
+
end
|
177
|
+
end #End of View
|
178
|
+
|
179
|
+
class PutPostDelete < BaseResponse
|
180
|
+
def id
|
181
|
+
@hash['id']
|
182
|
+
end
|
183
|
+
|
184
|
+
def rev
|
185
|
+
@hash['rev']
|
186
|
+
end
|
187
|
+
|
188
|
+
def ok?
|
189
|
+
@hash['ok'] || false
|
190
|
+
end
|
191
|
+
end #End of PutPost
|
192
|
+
|
193
|
+
end #End of CouchResponse
|
194
|
+
end
|
195
|
+
end
|
data/license
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2010 Anthony Sekatski <anthony@8protons.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/protoncouch.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'protoncouch'
|
3
|
+
s.version = '1.0'
|
4
|
+
s.description = "It's a simple and efficient CouchDB client written on Ruby. Current solutions like CouchRest are too heavy and have strange behaviour (like raising error when Resource not found). Also they can't manage many databases."
|
5
|
+
s.summary = 'ProtonCouch is the very simple and efficient CouchDB client. Awesome relax!'
|
6
|
+
|
7
|
+
s.author = 'Anthony Sekatski'
|
8
|
+
s.email = 'anthony@8protons.com'
|
9
|
+
s.homepage = 'http://github.com/8protons/protoncouch'
|
10
|
+
|
11
|
+
s.files = Dir['lib/protoncouch.rb', 'protoncouch.gemspec', 'README.md', 'license']
|
12
|
+
|
13
|
+
s.add_dependency('json', '>= 1.4.6')
|
14
|
+
end
|
15
|
+
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: protoncouch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "1.0"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anthony Sekatski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-02 00:00:00 +03:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: json
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 1.4.6
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
description: It's a simple and efficient CouchDB client written on Ruby. Current solutions like CouchRest are too heavy and have strange behaviour (like raising error when Resource not found). Also they can't manage many databases.
|
28
|
+
email: anthony@8protons.com
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files: []
|
34
|
+
|
35
|
+
files:
|
36
|
+
- lib/protoncouch.rb
|
37
|
+
- protoncouch.gemspec
|
38
|
+
- README.md
|
39
|
+
- license
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/8protons/protoncouch
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.5.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: ProtonCouch is the very simple and efficient CouchDB client. Awesome relax!
|
68
|
+
test_files: []
|
69
|
+
|