em-synchrony-couchdb 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/em-synchrony/couchdb.rb +100 -0
- data/test/couchdb_test.rb +26 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de5aace765f991aa7d41bb6ffed9734ce9076537
|
4
|
+
data.tar.gz: db944981a01b282da7c3c5f5cd6faf0387e5f4d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a969309722fced2b0fbd5aeb33bcc09db02c2e1b5cbcffcee6c73a37fb446ef0cdaaa17d55e119678bc8002a8ed231707bab9be1bb8c6ca708d3dcce3c129eb7
|
7
|
+
data.tar.gz: d1c71b3babe8cee75925c1a8a5f30fed915b732606c37b74ee6f550016ae525ed0ed9422e2385fda5ea7b6d1978ff0c7940c44d36c69c806eabfaf58ce6afca7
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'em-http'
|
3
|
+
require 'em-synchrony'
|
4
|
+
require 'em-synchrony/em-http'
|
5
|
+
require "json"
|
6
|
+
require "uri"
|
7
|
+
|
8
|
+
module EventMachine
|
9
|
+
module Synchrony
|
10
|
+
class CouchDB
|
11
|
+
def self.connect(connection_params={})
|
12
|
+
puts "*****Connecting" if $debug
|
13
|
+
self.new(connection_params)
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(connection_params={})
|
17
|
+
@host = connection_params[:host] || '127.0.0.1'
|
18
|
+
@port = connection_params[:port] || 5984
|
19
|
+
@timeout = connection_params[:timeout] || 10
|
20
|
+
end
|
21
|
+
|
22
|
+
# DB API
|
23
|
+
|
24
|
+
def get_all_dbs
|
25
|
+
get_request "/_all_dbs/", :timeout => @timeout
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_db(db_name)
|
29
|
+
put_request "/#{db_name}/"
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_db(db_name)
|
33
|
+
get_request "/#{db_name}/", :timeout => @timeout
|
34
|
+
end
|
35
|
+
|
36
|
+
def delete_db(db_name)
|
37
|
+
delete_request "/#{db_name}/"
|
38
|
+
end
|
39
|
+
|
40
|
+
def compact(db_name)
|
41
|
+
post_request "/#{db_name}/_compact"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Document API
|
45
|
+
|
46
|
+
def get(db_name, id)
|
47
|
+
get_request "/#{db_name}/#{id}", :timeout => @timeout
|
48
|
+
end
|
49
|
+
|
50
|
+
def save(db_name, doc)
|
51
|
+
post_request "/#{db_name}/", :body => JSON.dump(doc)
|
52
|
+
end
|
53
|
+
|
54
|
+
def update(db_name, old_doc, new_doc)
|
55
|
+
id, rev = get_id_and_revision(old_doc)
|
56
|
+
new_doc["_rev"] = rev
|
57
|
+
new_doc["_id"] = id
|
58
|
+
put_request "/#{db_name}/#{id}", :body => JSON.dump(new_doc)
|
59
|
+
end
|
60
|
+
|
61
|
+
def delete(db_name, doc)
|
62
|
+
doc_id, doc_revision = get_id_and_revision(doc)
|
63
|
+
delete_request "/#{db_name}/#{doc_id}?rev=#{doc_revision}"
|
64
|
+
end
|
65
|
+
|
66
|
+
def get_id_and_revision(doc)
|
67
|
+
if doc.has_key? "_id"
|
68
|
+
return doc["_id"], doc["_rev"]
|
69
|
+
else
|
70
|
+
return doc["id"], doc["rev"]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def get_request(path, options={})
|
77
|
+
request :get, path, options
|
78
|
+
end
|
79
|
+
|
80
|
+
def post_request(path, options={})
|
81
|
+
request :post, path, options
|
82
|
+
end
|
83
|
+
|
84
|
+
def put_request(path, options={})
|
85
|
+
request :put, path, options
|
86
|
+
end
|
87
|
+
|
88
|
+
def delete_request(path, options={})
|
89
|
+
request :delete, path, options
|
90
|
+
end
|
91
|
+
|
92
|
+
def request(type, path, options)
|
93
|
+
uri = URI.parse("http://#{@host}:#{@port}#{path}")
|
94
|
+
options = options.merge(:head => {"content-type" => "application/json"})
|
95
|
+
conn = EventMachine::HttpRequest.new(uri).send type, options
|
96
|
+
JSON.load conn.response
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'em-synchrony/couchdb'
|
4
|
+
|
5
|
+
module EventMachine
|
6
|
+
module Synchrony
|
7
|
+
describe CouchDB do
|
8
|
+
it 'should work' do
|
9
|
+
EM.synchrony do
|
10
|
+
db_name = 'em-synchrony-couchdb'
|
11
|
+
conn = CouchDB.connect
|
12
|
+
|
13
|
+
conn.create_db db_name
|
14
|
+
conn.get_all_dbs.must_include db_name
|
15
|
+
|
16
|
+
id = conn.save(db_name, {'meaning' => 42})['id']
|
17
|
+
conn.get(db_name, id)['meaning'].must_equal 42
|
18
|
+
|
19
|
+
conn.delete_db db_name
|
20
|
+
conn.get_all_dbs.wont_include db_name
|
21
|
+
EM.stop
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-synchrony-couchdb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tinomen (Jake Mallory)
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.4.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: em-http-request
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: em-synchrony
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.0.3
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.3
|
55
|
+
description: |2
|
56
|
+
em-synchrony-couchdb is a simple, convenient, and non-blocking client for CouchDB
|
57
|
+
implemented using the em-synchrony version of EventMachine::HttpRequest and based
|
58
|
+
completely on the em-couchdb gem. With em-synchrony-couchdb, you can easily save,
|
59
|
+
query, delete documents, databases to/from a CouchDB database in your favourite
|
60
|
+
language - Ruby.
|
61
|
+
email: tinomen@gmail.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- lib/em-synchrony/couchdb.rb
|
67
|
+
- test/couchdb_test.rb
|
68
|
+
homepage: http://github.com/tinomen/em-synchrony-couchdb
|
69
|
+
licenses: []
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements:
|
86
|
+
- CouchDB 0.8.0 and upwards
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.0.3
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: A non-blocking client protocol for CouchDB using em-synchrony
|
92
|
+
test_files:
|
93
|
+
- test/couchdb_test.rb
|