couchdb-utils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in couchdb-utils.gemspec
4
+ gemspec
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "couchdb-utils/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "couchdb-utils"
7
+ s.version = Couchdb::Utils::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Aaron Hamid & Michal Kuklis"]
10
+ s.email = ["aaron@incandescentsoftware.com, michal@incandescentsoftware.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{couchdb utils}
13
+ s.description = %q{couchdb utils for new database provisioning}
14
+
15
+ s.rubyforge_project = "couchdb-utils"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,90 @@
1
+ require "rubygems"
2
+
3
+ require 'base64'
4
+ require 'uuid'
5
+ require 'digest/sha1'
6
+ require 'json'
7
+ require 'uri'
8
+ require 'rest-client'
9
+
10
+ # couchdb utils for database provisioning
11
+ module Couchdb
12
+ class Utils
13
+ def initialize(host, port, user = nil, pass = nil, protocol = 'http')
14
+ if !user.nil? && !pass.nil?
15
+ @uri = "#{protocol}://#{user}:#{pass}@#{host}:#{port}"
16
+ else
17
+ @uri = "#{protocol}://#{host}:#{port}"
18
+ end
19
+ @headers = {:content_type => :json, :accept => :json}
20
+ end
21
+
22
+ # tests if user exists
23
+ def user_exists?(username)
24
+ #TODO
25
+ end
26
+
27
+ # creates all
28
+ def create_all(username, password)
29
+ resp1 = create_db(username)
30
+ resp2 = create_user(username, password)
31
+ resp3 = create_db_readers(username)
32
+ yield resp1, resp2, resp3 if block_given?
33
+ end
34
+
35
+ # creates new database for given name
36
+ def create_db(name)
37
+ RestClient.put "#{@uri}/#{name}", '/', @headers
38
+ end
39
+
40
+ # create db readers
41
+ # http://blog.couchone.com/post/1027100082/whats-new-in-couchdb-1-0-part-4-securityn-stuff
42
+ def create_db_readers(name)
43
+ readers = {:readers => {:names => ["#{name}"]}}
44
+ RestClient.put "#{@uri}/#{name}/_security",
45
+ readers.to_json, @headers
46
+ end
47
+
48
+ # adds new user to _users table
49
+ def create_user(username, password)
50
+ user_doc = prepare_user_doc({:name => username}, password);
51
+ RestClient.put "#{@uri}/_users/#{encode_doc_id(user_doc[:_id])}",
52
+ user_doc.to_json, @headers
53
+ end
54
+
55
+ #replicates document
56
+ def replicate_doc(from_db, to_db, doc_id)
57
+ replication = {:source => "#{@uri}/#{from_db}", :target => "#{@uri}/#{to_db}", :doc_ids => [doc_id]}
58
+ RestClient.post "#{@uri}/_replicate", replication.to_json, @headers
59
+ end
60
+
61
+ private
62
+
63
+ # encodes uri
64
+ def encode_uri(uri)
65
+ URI.escape(uri, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
66
+ end
67
+
68
+ # encondes doc id
69
+ def encode_doc_id(doc_id)
70
+ parts = doc_id.split("/")
71
+ if parts[0] == "_design"
72
+ parts.shift
73
+ return "_design/" + encode_uri(parts.join('/'));
74
+ end
75
+ encode_uri(doc_id);
76
+ end
77
+
78
+ # prepares user document
79
+ def prepare_user_doc(user_doc, user_password)
80
+ user_doc[:_id] = "org.couchdb.user:" + user_doc[:name]
81
+ if !user_password.nil?
82
+ user_doc[:salt] = UUID.generate
83
+ user_doc[:password_sha] = Digest::SHA1.hexdigest(user_password + user_doc[:salt])
84
+ end
85
+ user_doc[:type] = "user"
86
+ user_doc[:roles] = []
87
+ return user_doc;
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,5 @@
1
+ module Couchdb
2
+ module Utils
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: couchdb-utils
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Aaron Hamid & Michal Kuklis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-04 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: couchdb utils for new database provisioning
23
+ email:
24
+ - aaron@incandescentsoftware.com, michal@incandescentsoftware.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - couchdb-utils.gemspec
36
+ - lib/couchdb-utils.rb
37
+ - lib/couchdb-utils/version.rb
38
+ has_rdoc: true
39
+ homepage: ""
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: couchdb-utils
68
+ rubygems_version: 1.3.7
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: couchdb utils
72
+ test_files: []
73
+