ProtonCouch 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/protoncouch.rb +195 -0
- metadata +66 -0
data/lib/protoncouch.rb
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Protons8
|
6
|
+
class Couch
|
7
|
+
#Initialization
|
8
|
+
def initialize(constrings)
|
9
|
+
@s = { }
|
10
|
+
#Construct our settings
|
11
|
+
constrings.each do |k,v|
|
12
|
+
#Get URI from value
|
13
|
+
uri = URI.parse(v)
|
14
|
+
#Fill a simple hash without Object (I've no idea why i do this :D)
|
15
|
+
@s[k] = { :host => uri.host, :port => uri.port, :path => uri.path, :user => uri.user, :password => uri.password }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def info(db)
|
20
|
+
CouchResponse::Get.new request( db, Net::HTTP::Get.new("#{@s[db][:path]}/") )
|
21
|
+
end
|
22
|
+
|
23
|
+
def get(db, id, params = {})
|
24
|
+
CouchResponse::Get.new request( db, Net::HTTP::Get.new( "#{@s[db][:path]}/" + id + get_qs(params) ) )
|
25
|
+
end
|
26
|
+
|
27
|
+
def save(db, document)
|
28
|
+
req = Net::HTTP::Post.new( "#{@s[db][:path]}/" )
|
29
|
+
req["content-type"] = "application/json"
|
30
|
+
req.body = document.to_json
|
31
|
+
CouchResponse::PutPostDelete.new request( db, req )
|
32
|
+
end
|
33
|
+
|
34
|
+
def save_with_id(db, id, document)
|
35
|
+
req = Net::HTTP::Put.new( "#{@s[db][:path]}/" + id )
|
36
|
+
req["content-type"] = "application/json"
|
37
|
+
req.body = document.to_json
|
38
|
+
CouchResponse::PutPostDelete.new request( db, req )
|
39
|
+
end
|
40
|
+
|
41
|
+
def save_or_update(db, id, document)
|
42
|
+
response = self.get(db, id)
|
43
|
+
document['_rev'] = response.rev if response.exists?
|
44
|
+
req = Net::HTTP::Put.new( "#{@s[db][:path]}/" + id )
|
45
|
+
req["content-type"] = "application/json"
|
46
|
+
req.body = document.to_json
|
47
|
+
CouchResponse::PutPostDelete.new request( db, req )
|
48
|
+
end
|
49
|
+
|
50
|
+
def delete(db, id)
|
51
|
+
response = self.get(db, id)
|
52
|
+
rev = response.exists? ? response.rev : ''
|
53
|
+
CouchResponse::PutPostDelete.new request( db, Net::HTTP::Delete.new( "#{@s[db][:path]}/#{id}?rev=#{rev}") )
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete_rev(db, id, rev)
|
57
|
+
CouchResponse::PutPostDelete.new request( db, Net::HTTP::Delete.new( "#{@s[db][:path]}/#{id}?rev=#{rev}") )
|
58
|
+
end
|
59
|
+
|
60
|
+
def view(db, view, params = {})
|
61
|
+
CouchResponse::View.new request( db, Net::HTTP::Get.new("#{@s[db][:path]}/_design/#{view.split('/')[0]}/_view/#{view.split('/')[1]}" + get_qs(params) ) )
|
62
|
+
end
|
63
|
+
|
64
|
+
# Private methods - they are shy
|
65
|
+
private
|
66
|
+
|
67
|
+
# Do some request now!
|
68
|
+
def request(db, req)
|
69
|
+
#Create server
|
70
|
+
http = Net::HTTP.new @s[db][:host], @s[db][:port]
|
71
|
+
#Set auth rules
|
72
|
+
req.basic_auth @s[db][:user], @s[db][:password]
|
73
|
+
#Request!
|
74
|
+
JSON.parse(http.request(req).body)
|
75
|
+
#...
|
76
|
+
#Profit!
|
77
|
+
end
|
78
|
+
|
79
|
+
#Get query string from hash
|
80
|
+
def get_qs(params)
|
81
|
+
if params and not params.empty?
|
82
|
+
qs = params.collect do |k,v|
|
83
|
+
v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
|
84
|
+
"#{k}=#{CGI.escape(v.to_s)}"
|
85
|
+
end.join("&")
|
86
|
+
"?#{qs}"
|
87
|
+
else
|
88
|
+
''
|
89
|
+
end
|
90
|
+
end #End of get_qs
|
91
|
+
|
92
|
+
#Several responses classes for convenience
|
93
|
+
class CouchResponse
|
94
|
+
|
95
|
+
#Add several methods to all type of response
|
96
|
+
class BaseResponse
|
97
|
+
def initialize(hash)
|
98
|
+
@hash = hash
|
99
|
+
end
|
100
|
+
|
101
|
+
def error?
|
102
|
+
not @hash['error'].nil?
|
103
|
+
end
|
104
|
+
|
105
|
+
def no_error?
|
106
|
+
@hash['error'].nil?
|
107
|
+
end
|
108
|
+
|
109
|
+
def error_name
|
110
|
+
@hash['error'] || 'No error'
|
111
|
+
end
|
112
|
+
|
113
|
+
def reason
|
114
|
+
@hash['error'] ? @hash['reason'] : 'No reason - everything ok'
|
115
|
+
end
|
116
|
+
|
117
|
+
def not_found?
|
118
|
+
@hash['error'] == 'not_found' ? true : false
|
119
|
+
end
|
120
|
+
|
121
|
+
def unauth?
|
122
|
+
@hash['error'] == 'unauthorized' ? true : false
|
123
|
+
end
|
124
|
+
|
125
|
+
def conflict?
|
126
|
+
@hash['error'] == 'conflict' ? true : false
|
127
|
+
end
|
128
|
+
|
129
|
+
def raw
|
130
|
+
@hash
|
131
|
+
end
|
132
|
+
|
133
|
+
def attachments
|
134
|
+
@hash['_attachments'] || nil
|
135
|
+
end
|
136
|
+
|
137
|
+
def attachment(id)
|
138
|
+
@hash['_attachments'] ? @hash['_attachments'][id] : nil
|
139
|
+
end
|
140
|
+
end #End of BaseResponse
|
141
|
+
|
142
|
+
class Get < BaseResponse
|
143
|
+
def id
|
144
|
+
@hash['_id']
|
145
|
+
end
|
146
|
+
|
147
|
+
def rev
|
148
|
+
@hash['_rev']
|
149
|
+
end
|
150
|
+
|
151
|
+
def exists?
|
152
|
+
@hash['_id'] || false
|
153
|
+
end
|
154
|
+
end #End of Get
|
155
|
+
|
156
|
+
class View < BaseResponse
|
157
|
+
def total_rows
|
158
|
+
@hash['total_rows'] || nil
|
159
|
+
end
|
160
|
+
|
161
|
+
def offset
|
162
|
+
@hash['offset'] || nil
|
163
|
+
end
|
164
|
+
|
165
|
+
def rows
|
166
|
+
@hash['rows'] || nil
|
167
|
+
end
|
168
|
+
|
169
|
+
def any?
|
170
|
+
@hash['rows'] ? (not @hash['rows'][0].nil?) : false
|
171
|
+
end
|
172
|
+
|
173
|
+
def empty?
|
174
|
+
@hash['rows'] ? @hash['rows'].empty? : false
|
175
|
+
end
|
176
|
+
end #End of View
|
177
|
+
|
178
|
+
class PutPostDelete < BaseResponse
|
179
|
+
def id
|
180
|
+
@hash['id']
|
181
|
+
end
|
182
|
+
|
183
|
+
def rev
|
184
|
+
@hash['rev']
|
185
|
+
end
|
186
|
+
|
187
|
+
def ok?
|
188
|
+
@hash['ok'] || false
|
189
|
+
end
|
190
|
+
end #End of PutPost
|
191
|
+
|
192
|
+
end #End of CouchResponse
|
193
|
+
|
194
|
+
end
|
195
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
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
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/8protons/protoncouch
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.5.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: ProtonCouch is very simple and efficient CouchDB client. Awesome relax!
|
65
|
+
test_files: []
|
66
|
+
|