danarchy_couchdb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/danarchy_couchdb.gemspec +36 -0
- data/lib/danarchy_couchdb/version.rb +3 -0
- data/lib/danarchy_couchdb.rb +77 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c79285e5a743f8a96a416ee600eb7a9c2733b483
|
4
|
+
data.tar.gz: 16676412cf347a7e3ba82d7fe6967645b7e00739
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e9700a452ee99b02a55024db976abcdfa84508561bd2eec2b0a92ff4d9cf7cba1bfc1c58984779c8455c72874830e0147d1e2b2f28dc1e2af16ff7c2e4ae622b
|
7
|
+
data.tar.gz: 325e00876a2c1a4a20ac68beb5a16d41d07245b01b033d2fe35bcb90042d8b6fbb5725e745230df265a0571ee0e7ca6beb3dfca2cc3b09e8dd74816b8c6130e8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
danarchy_couchdb (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.7.0)
|
12
|
+
rspec-core (~> 3.7.0)
|
13
|
+
rspec-expectations (~> 3.7.0)
|
14
|
+
rspec-mocks (~> 3.7.0)
|
15
|
+
rspec-core (3.7.1)
|
16
|
+
rspec-support (~> 3.7.0)
|
17
|
+
rspec-expectations (3.7.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.7.0)
|
20
|
+
rspec-mocks (3.7.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.7.0)
|
23
|
+
rspec-support (3.7.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
danarchy_couchdb!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Dan Heneise
|
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/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# DanarchyCouchDB
|
2
|
+
|
3
|
+
A Ruby implementation of CouchDB.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'danarchy_couchdb'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install danarchy_couchdb
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
A new DanarchyCouchDB object is created using a hash of connection values:
|
24
|
+
|
25
|
+
With SSL:
|
26
|
+
```ruby
|
27
|
+
conn = { host: 'hostname/IPaddr', port: '6984', user: 'cdb_user', pass: 'cdb_passwd', use_ssl: true }
|
28
|
+
```
|
29
|
+
|
30
|
+
Without SSL:
|
31
|
+
```ruby
|
32
|
+
conn = { host: 'hostname/IPaddr', port: '5984', user: 'cdb_user', pass: 'cdb_passwd', use_ssl: false }
|
33
|
+
```
|
34
|
+
|
35
|
+
Create the object:
|
36
|
+
```ruby
|
37
|
+
cdb = DanarchyCouchDB::Connection.new(conn)
|
38
|
+
```
|
39
|
+
|
40
|
+
All calls to CouchDB comprise of at least a 'database' and 'document' argument, and PUT requires an additional hash, which it will turn into JSON formatting to send to CouchDB:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# View all databases:
|
44
|
+
cdb.get('_all_dbs', '')
|
45
|
+
=> ["_replicator", "_users", "test_suite_db", "test_suite_db2"]
|
46
|
+
|
47
|
+
database = 'test_suite_db'
|
48
|
+
|
49
|
+
# View all documents within a database:
|
50
|
+
cdb.get('test_suite_db', '_all_docs')
|
51
|
+
=> {:total_rows=>3, :offset=>0, :rows=>[{:id=>"3853a22cbc4520c65f81c8689706009a", :key=>"3853a22cbc4520c65f81c8689706009a", :value=>{:rev=>"1-027467bd0efec85f21c822a8eb537073"}}, {:id=>"3853a22cbc4520c65f81c86897060c60", :key=>"3853a22cbc4520c65f81c86897060c60", :value=>{:rev=>"1-3975759ccff3842adf690a5c10caee42"}}, {:id=>"3853a22cbc4520c65f81c86897060dc0", :key=>"3853a22cbc4520c65f81c86897060dc0", :value=>{:rev=>"1-23202479633c2b380f79507a776743d5"}}]}
|
52
|
+
|
53
|
+
# Load a document:
|
54
|
+
test_suite_db = cdb.get('test_suite_db', '3853a22cbc4520c65f81c86897060c60')
|
55
|
+
=> {:_id=>"3853a22cbc4520c65f81c86897060c60", :_rev=>"1-3975759ccff3842adf690a5c10caee42", :a=>2}
|
56
|
+
|
57
|
+
# Add a new key=>value to the document:
|
58
|
+
test_suite_db[:taxation] = 'is extortion'
|
59
|
+
p test_suite_db
|
60
|
+
=> {:_id=>"3853a22cbc4520c65f81c86897060c60", :_rev=>"1-3975759ccff3842adf690a5c10caee42", :a=>2, :taxation=>"is extortion"}
|
61
|
+
|
62
|
+
# Update the document within CouchDB:
|
63
|
+
cdb.put('test_suite_db', '3853a22cbc4520c65f81c86897060c60', test_suite_db)
|
64
|
+
=> {:ok=>true, :id=>"3853a22cbc4520c65f81c86897060c60", :rev=>"2-743ac80b9a990d2644bc92bafb452fe5"}
|
65
|
+
|
66
|
+
# We can verify the update with another cdb.get to update the hash to the latest document revision:
|
67
|
+
test_suite_db = cdb.get('test_suite_db', '3853a22cbc4520c65f81c86897060c60')
|
68
|
+
=> {:_id=>"3853a22cbc4520c65f81c86897060c60", :_rev=>"2-743ac80b9a990d2644bc92bafb452fe5", :a=>2, :taxation=>"is extortion"}
|
69
|
+
|
70
|
+
# cdb.get can also take an optional revision argument:
|
71
|
+
test_suite_db = cdb.get('test_suite_db', '3853a22cbc4520c65f81c86897060c60', '3-9de3ebabb6ea97fb4015cb698d228aec')
|
72
|
+
=> {:_id=>"3853a22cbc4520c65f81c86897060c60", :_rev=>"3-9de3ebabb6ea97fb4015cb698d228aec", :a=>2, :taxation=>"is extortion"}
|
73
|
+
|
74
|
+
# Delete a document with cdb.delete with a required revision:
|
75
|
+
cdb.delete('test_suite_db', '3853a22cbc4520c65f81c86897060c60', '3-9de3ebabb6ea97fb4015cb698d228aec')
|
76
|
+
=> {:ok=>true, :id=>"3853a22cbc4520c65f81c86897060c60", :rev=>"4-b35392a676cdbafebefaf425c6913670"}
|
77
|
+
|
78
|
+
```
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports are welcome on GitHub at https://github.com/danarchy85/danarchy_couchdb.
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "danarchy_couchdb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "danarchy_couchdb/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "danarchy_couchdb"
|
8
|
+
spec.version = DanarchyCouchDB::VERSION
|
9
|
+
spec.authors = ["Dan James"]
|
10
|
+
spec.email = ["dan@danarchy.me"]
|
11
|
+
|
12
|
+
spec.summary = %q{dAnarchy CouchDB connector.}
|
13
|
+
spec.description = %q{A CouchDB API connector for dAnarchy gems.}
|
14
|
+
spec.homepage = "https://github.com/danarchy85/danarchy_couchdb"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "bin"
|
30
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative 'danarchy_couchdb/version'
|
2
|
+
require 'json'
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
module DanarchyCouchDB
|
6
|
+
class Connection
|
7
|
+
def initialize(connection)
|
8
|
+
@connection = connection
|
9
|
+
uri = 'http' if !@connection[:use_ssl]
|
10
|
+
uri = 'https' if @connection[:use_ssl]
|
11
|
+
@uri = "#{uri}://#{connection[:host]}:#{connection[:port]}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(*args)
|
15
|
+
database = args.shift
|
16
|
+
document = args.shift
|
17
|
+
revision = args.shift
|
18
|
+
|
19
|
+
uri = "#{@uri}/#{database}"
|
20
|
+
uri += "/#{document}" if document
|
21
|
+
uri += "/?rev=#{revision}" if revision
|
22
|
+
|
23
|
+
uri = URI.parse(URI.encode(uri))
|
24
|
+
request(Net::HTTP::Get.new(uri))
|
25
|
+
end
|
26
|
+
|
27
|
+
def put(*args)
|
28
|
+
database = args.shift
|
29
|
+
document = args.shift
|
30
|
+
data = args.shift
|
31
|
+
|
32
|
+
uri = "#{@uri}/#{database}"
|
33
|
+
uri += "/#{document}" if document
|
34
|
+
|
35
|
+
uri = URI.parse(URI.encode("#{@uri}/#{database}/#{document}"))
|
36
|
+
req = Net::HTTP::Put.new(uri)
|
37
|
+
req["content-type"] = "application/json"
|
38
|
+
req.body = data.to_json if data
|
39
|
+
request(req)
|
40
|
+
end
|
41
|
+
|
42
|
+
def delete(*args)
|
43
|
+
database = args.shift
|
44
|
+
document = args.shift
|
45
|
+
revision = args.shift
|
46
|
+
|
47
|
+
uri = "#{@uri}/#{database}"
|
48
|
+
uri += "/#{document}" if document
|
49
|
+
uri += "/?rev=#{revision}" if revision
|
50
|
+
|
51
|
+
uri = URI.parse(URI.encode(uri))
|
52
|
+
request(Net::HTTP::Delete.new(uri))
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
def request(uri)
|
57
|
+
response = nil
|
58
|
+
|
59
|
+
Net::HTTP.start(@connection[:host], @connection[:port], :use_ssl => @connection[:use_ssl]) do |http|
|
60
|
+
uri.basic_auth @connection[:user], @connection[:pass]
|
61
|
+
|
62
|
+
response = http.request(uri)
|
63
|
+
|
64
|
+
unless response.kind_of?(Net::HTTPSuccess)
|
65
|
+
handle_error(uri, response)
|
66
|
+
break
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
JSON.parse(response.body, symbolize_names: true)
|
71
|
+
end
|
72
|
+
|
73
|
+
def handle_error(req, res)
|
74
|
+
puts RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: danarchy_couchdb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan James
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A CouchDB API connector for dAnarchy gems.
|
56
|
+
email:
|
57
|
+
- dan@danarchy.me
|
58
|
+
executables:
|
59
|
+
- console
|
60
|
+
- setup
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- danarchy_couchdb.gemspec
|
75
|
+
- lib/danarchy_couchdb.rb
|
76
|
+
- lib/danarchy_couchdb/version.rb
|
77
|
+
homepage: https://github.com/danarchy85/danarchy_couchdb
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata:
|
81
|
+
allowed_push_host: https://rubygems.org
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.6.14
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: dAnarchy CouchDB connector.
|
102
|
+
test_files: []
|