flazz-couchdb 0.0.0
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/Rakefile +10 -0
- data/couchdb.gemspec +11 -0
- data/lib/couchdb.rb +106 -0
- data/lib/rest.rb +51 -0
- metadata +64 -0
data/Rakefile
ADDED
data/couchdb.gemspec
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "couchdb"
|
3
|
+
spec.version = '0.0.0'
|
4
|
+
spec.summary = "Simple Interface to CouchDB"
|
5
|
+
spec.email = "flazzarino@gmail.com"
|
6
|
+
spec.homepage = "http://github.com/flazz/couchdb"
|
7
|
+
spec.authors = ["Francesco (franco) Lazzarino"]
|
8
|
+
spec.files = ['Rakefile', 'couchdb.gemspec', "lib/couchdb.rb", "lib/rest.rb"]
|
9
|
+
spec.add_dependency("json", ">= 1.1.3")
|
10
|
+
spec.autorequire = 'couchdb'
|
11
|
+
end
|
data/lib/couchdb.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'rest'
|
4
|
+
|
5
|
+
module CouchDB
|
6
|
+
|
7
|
+
class Server
|
8
|
+
|
9
|
+
include Rest::Resource
|
10
|
+
|
11
|
+
def initialize(host="localhost", port=5984)
|
12
|
+
@host = host
|
13
|
+
@port = port
|
14
|
+
end
|
15
|
+
|
16
|
+
def databases
|
17
|
+
response = self.get "/_all_dbs"
|
18
|
+
JSON.parse(response.body).map { |name| Database.new(self, name) }
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
# CouchDB database
|
24
|
+
class Database
|
25
|
+
|
26
|
+
def initialize(server, name)
|
27
|
+
@server = server
|
28
|
+
@name = name
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_db
|
32
|
+
@server.put "/#{@name}/"
|
33
|
+
end
|
34
|
+
|
35
|
+
def delete_db
|
36
|
+
@server.delete "/#{@name}/"
|
37
|
+
end
|
38
|
+
|
39
|
+
def documents
|
40
|
+
response = @server.get "/#{@name}/_all_docs"
|
41
|
+
JSON.parse(response.body)
|
42
|
+
end
|
43
|
+
|
44
|
+
def info
|
45
|
+
response = @server.get "/#{@name}/"
|
46
|
+
JSON.parse(response.body)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Pass restfull calls to the server
|
50
|
+
def method_missing(method, *args)
|
51
|
+
|
52
|
+
case method
|
53
|
+
when :get, :put, :delete
|
54
|
+
args[0] = "/#{@name}/#{args[0]}"
|
55
|
+
@server.send method, *args
|
56
|
+
when :post
|
57
|
+
@server.send method, "/#{@name}/", *args
|
58
|
+
else
|
59
|
+
super
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
# CRUD interface to CouchDB Document
|
67
|
+
class Document
|
68
|
+
attr_reader :id, :revision, :data
|
69
|
+
attr_writer :data, :id
|
70
|
+
|
71
|
+
def initialize(database)
|
72
|
+
@database = database
|
73
|
+
end
|
74
|
+
|
75
|
+
def create
|
76
|
+
json = JSON.generate @data
|
77
|
+
response = @database.post json
|
78
|
+
data = JSON.parse response.body
|
79
|
+
@id = data["id"]
|
80
|
+
@revision = data["rev"]
|
81
|
+
end
|
82
|
+
|
83
|
+
def read
|
84
|
+
response = @database.get @id
|
85
|
+
data = JSON.parse response.body
|
86
|
+
@data = data
|
87
|
+
@revision = data["_rev"]
|
88
|
+
end
|
89
|
+
|
90
|
+
def update
|
91
|
+
@data['_rev'] = @revision
|
92
|
+
json = JSON.generate @data
|
93
|
+
response = @database.put @id, json
|
94
|
+
data = JSON.parse response.body
|
95
|
+
@revision = data["rev"]
|
96
|
+
end
|
97
|
+
|
98
|
+
def delete
|
99
|
+
response = @database.delete "#{@id}?rev=#{@revision}"
|
100
|
+
data = JSON.parse response.body
|
101
|
+
@revision = data["rev"]
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
data/lib/rest.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
# requires the existence of @host and @port
|
4
|
+
module Rest
|
5
|
+
|
6
|
+
module Resource
|
7
|
+
|
8
|
+
# Delete a resource
|
9
|
+
def delete(uri)
|
10
|
+
request Net::HTTP::Delete.new(uri)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Get a resource
|
14
|
+
def get(uri)
|
15
|
+
request Net::HTTP::Get.new(uri)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Put a document
|
19
|
+
def put(uri, body=nil, content_type="application/json")
|
20
|
+
req = Net::HTTP::Put.new uri
|
21
|
+
req["content-type"] = content_type
|
22
|
+
req.body = body
|
23
|
+
request req
|
24
|
+
end
|
25
|
+
|
26
|
+
# Post a document to a uri
|
27
|
+
def post(uri, body, content_type="application/json")
|
28
|
+
req = Net::HTTP::Post.new uri
|
29
|
+
req["content-type"] = content_type
|
30
|
+
req.body = body
|
31
|
+
request req
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
def request(req)
|
37
|
+
|
38
|
+
res = Net::HTTP.start(@host, @port) do |http|
|
39
|
+
http.request req
|
40
|
+
end
|
41
|
+
|
42
|
+
unless res.kind_of? Net::HTTPSuccess
|
43
|
+
raise "#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}"
|
44
|
+
end
|
45
|
+
|
46
|
+
res
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flazz-couchdb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco (franco) Lazzarino
|
8
|
+
autorequire: couchdb
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-10-27 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.3
|
23
|
+
version:
|
24
|
+
description:
|
25
|
+
email: flazzarino@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- Rakefile
|
34
|
+
- couchdb.gemspec
|
35
|
+
- lib/couchdb.rb
|
36
|
+
- lib/rest.rb
|
37
|
+
has_rdoc: false
|
38
|
+
homepage: http://github.com/flazz/couchdb
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.2.0
|
60
|
+
signing_key:
|
61
|
+
specification_version: 2
|
62
|
+
summary: Simple Interface to CouchDB
|
63
|
+
test_files: []
|
64
|
+
|