gnouch 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/gnouch.rb +11 -0
- data/lib/gnouch/attachment.rb +18 -0
- data/lib/gnouch/database.rb +126 -0
- data/lib/gnouch/design.rb +29 -0
- data/lib/gnouch/error.rb +17 -0
- data/lib/gnouch/requestor.rb +37 -0
- data/lib/gnouch/server.rb +54 -0
- metadata +86 -0
data/lib/gnouch.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module Gnouch
|
4
|
+
|
5
|
+
def self.add_inline_attachment(doc, name, mime, data)
|
6
|
+
attachment = {
|
7
|
+
:content_type => mime,
|
8
|
+
:data => Base64.urlsafe_encode64(data)
|
9
|
+
}
|
10
|
+
attachments = doc["_attachments"]
|
11
|
+
unless attachments
|
12
|
+
attachments = {}
|
13
|
+
doc["_attachments"] = attachments
|
14
|
+
end
|
15
|
+
attachments[name] = attachment
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
require 'gnouch/requestor.rb'
|
4
|
+
|
5
|
+
module Gnouch
|
6
|
+
|
7
|
+
class Database < Requestor
|
8
|
+
|
9
|
+
def initialize(url)
|
10
|
+
@url = url
|
11
|
+
end
|
12
|
+
|
13
|
+
def reset()
|
14
|
+
request [:delete, url, {}]
|
15
|
+
request [:put, url, {}, ""]
|
16
|
+
end
|
17
|
+
|
18
|
+
def info()
|
19
|
+
request [:get, url, {}]
|
20
|
+
end
|
21
|
+
|
22
|
+
def save(id, doc=nil)
|
23
|
+
unless doc
|
24
|
+
doc = id
|
25
|
+
id = doc["_id"]
|
26
|
+
end
|
27
|
+
json = doc.to_json
|
28
|
+
req = id ? [:put, url(id), {}, json] : [:post, url, {}, json]
|
29
|
+
if block_given?
|
30
|
+
yield *block_request(req)
|
31
|
+
else
|
32
|
+
request req
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def get(id)
|
37
|
+
req = [:get, url(id), {}]
|
38
|
+
if block_given?
|
39
|
+
yield *block_request(req)
|
40
|
+
else
|
41
|
+
request req
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete(id, rev=nil)
|
46
|
+
unless rev
|
47
|
+
doc = request [:get, url(id), {}]
|
48
|
+
rev = doc["_rev"]
|
49
|
+
end
|
50
|
+
request [:delete, [url(id), {:rev => rev}], {}]
|
51
|
+
end
|
52
|
+
|
53
|
+
def <<(doc)
|
54
|
+
id = doc["_id"]
|
55
|
+
json = doc.to_json
|
56
|
+
req = id ? [:put, url(id), {}, json] : [:post, url, {}, json]
|
57
|
+
request req
|
58
|
+
end
|
59
|
+
|
60
|
+
def [](id)
|
61
|
+
request [:get, url(id), {}]
|
62
|
+
end
|
63
|
+
|
64
|
+
def []=(id, doc)
|
65
|
+
request [:put, url(id), {}, doc.to_json]
|
66
|
+
end
|
67
|
+
|
68
|
+
def all_docs()
|
69
|
+
self[:_all_docs]
|
70
|
+
end
|
71
|
+
|
72
|
+
def security()
|
73
|
+
self[:_security]
|
74
|
+
end
|
75
|
+
|
76
|
+
def view(design, view, opts=nil)
|
77
|
+
url = "#{@url}/_design/#{design}/_view/#{view}"
|
78
|
+
if opts
|
79
|
+
[:key, :startkey, :endkey].each do |key|
|
80
|
+
opts[key] = opts[key].to_json if opts.has_key? key
|
81
|
+
end
|
82
|
+
url = [url, opts]
|
83
|
+
end
|
84
|
+
request [:get, url, {}]
|
85
|
+
end
|
86
|
+
|
87
|
+
def design_info(design)
|
88
|
+
request [:get, url("_design/#{design}/_info"), {}]
|
89
|
+
end
|
90
|
+
|
91
|
+
def find(design, view, opts=nil)
|
92
|
+
raw = view(design, view, opts)
|
93
|
+
raw["rows"].collect do |row|
|
94
|
+
row["value"]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def find_first(design, view, opts=nil)
|
99
|
+
raw = view(design, view, opts)
|
100
|
+
unless raw["rows"].empty?
|
101
|
+
raw["rows"].first["value"]
|
102
|
+
else
|
103
|
+
nil
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def find_docs(design, view, opts={})
|
108
|
+
opts[:include_docs] = true
|
109
|
+
raw = view(design, view, opts)
|
110
|
+
raw["rows"].collect do |row|
|
111
|
+
row["doc"]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def find_first_doc(design, view, opts={})
|
116
|
+
opts[:include_docs] = true
|
117
|
+
raw = view(design, view, opts)
|
118
|
+
unless raw["rows"].empty?
|
119
|
+
raw["rows"].first["doc"]
|
120
|
+
else
|
121
|
+
nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
module Gnouch
|
3
|
+
|
4
|
+
module Design
|
5
|
+
|
6
|
+
def self.json(doc)
|
7
|
+
js = %Q{
|
8
|
+
var funToStr = function(obj) {
|
9
|
+
for (var key in obj) {
|
10
|
+
var value = obj[key];
|
11
|
+
if (typeof value == "object") {
|
12
|
+
funToStr(value);
|
13
|
+
} else if (typeof value == "function") {
|
14
|
+
obj[key] = value.toString();
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
var doc = #{doc};
|
19
|
+
funToStr(doc);
|
20
|
+
print(JSON.stringify(doc));
|
21
|
+
}
|
22
|
+
|
23
|
+
json = `js -f #{File.dirname(__FILE__)}/json2.js -e "#{js.gsub('"', '\\"')}"`
|
24
|
+
json
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/gnouch/error.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
require 'gnouch/error.rb'
|
4
|
+
|
5
|
+
module Gnouch
|
6
|
+
|
7
|
+
class Requestor
|
8
|
+
|
9
|
+
def request(request)
|
10
|
+
response = http request
|
11
|
+
status, headers, body = response
|
12
|
+
if (200..204).cover? status
|
13
|
+
JSON.parse body
|
14
|
+
else
|
15
|
+
raise Error.new(response)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def block_request(request)
|
20
|
+
response = http request
|
21
|
+
status, headers, body = response
|
22
|
+
if (200..204).cover? status
|
23
|
+
[JSON.parse(body)]
|
24
|
+
else
|
25
|
+
[JSON.parse(body), response]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def url(id=nil)
|
30
|
+
url = @url
|
31
|
+
url += "/#{id}" if id
|
32
|
+
url
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'gnouch/requestor.rb'
|
2
|
+
|
3
|
+
module Gnouch
|
4
|
+
|
5
|
+
class Server < Requestor
|
6
|
+
|
7
|
+
def initialize(url)
|
8
|
+
@url = url
|
9
|
+
end
|
10
|
+
|
11
|
+
def info()
|
12
|
+
request [:get, url, {}]
|
13
|
+
end
|
14
|
+
|
15
|
+
def config()
|
16
|
+
request [:get, url(:_config), {}]
|
17
|
+
end
|
18
|
+
|
19
|
+
def database(name)
|
20
|
+
Database.new url(name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_database(name)
|
24
|
+
request [:put, url(name), {}, ""]
|
25
|
+
database name
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete(name)
|
29
|
+
request [:delete, url(name), {}]
|
30
|
+
end
|
31
|
+
|
32
|
+
def all_dbs()
|
33
|
+
request [:get, url(:_all_dbs), {}]
|
34
|
+
end
|
35
|
+
|
36
|
+
def [](name)
|
37
|
+
database name
|
38
|
+
end
|
39
|
+
|
40
|
+
def <<(name)
|
41
|
+
add_database name
|
42
|
+
end
|
43
|
+
|
44
|
+
def replicate(source, target)
|
45
|
+
doc = {
|
46
|
+
:source => source,
|
47
|
+
:target => target
|
48
|
+
}
|
49
|
+
request [:post, url(:_replicate), {}, doc.to_json]
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gnouch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kim Dalsgaard
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-23 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: json
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: A CouchDB client on top of the Gnarly HTTP Client abstraction.
|
36
|
+
email: kim@kimdalsgaard.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/gnouch/attachment.rb
|
45
|
+
- lib/gnouch/database.rb
|
46
|
+
- lib/gnouch/design.rb
|
47
|
+
- lib/gnouch/error.rb
|
48
|
+
- lib/gnouch/requestor.rb
|
49
|
+
- lib/gnouch/server.rb
|
50
|
+
- lib/gnouch.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage:
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.7
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: A CouchDB client
|
85
|
+
test_files: []
|
86
|
+
|