danarchy_couchdb 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4687b1862b5abdbdd8dd8df2f7f1b7cd85a10ba4
4
- data.tar.gz: 3e09d75a8e101df1ef2e7e86482611d2ddd237f9
3
+ metadata.gz: aef075277dd642303ed801489bddcb5b91d29fc8
4
+ data.tar.gz: 40c45a2cb5ff0244bbd523cd3e48013d9629c822
5
5
  SHA512:
6
- metadata.gz: c1e6ba5e320941d63169191bccc1cd2ec43a076236a5aebb21b8c88b470010d0e6b56a17f503716c32821ee8e22f0c58d4880d681fa0606786320c694e5355b4
7
- data.tar.gz: 6f5820579b50ee6d4decc029ed8629183e68c481f00b25a80939466f52f901f1f9111064d224eeb69683b6309b7a6c1f7a90eddf63558b929af6935e6912bac3
6
+ metadata.gz: 61b9335b607a0e2984edc27f98a98c348c32a0b99fa2abde598f1edfeecbf5d9236e2f08fce350252a24c10f2e1330a428cdfe5ffbb8d326f1135c90a7cb5ff3
7
+ data.tar.gz: a879ed7b20299ef9e1a2d45b2743ff97e00f55a5738ee92f8be13d5e40621586ac770447b89ea69716c7128b1341680a66052cecb48a3cdd617aee71ed50f864
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danarchy_couchdb (0.1.0)
4
+ danarchy_couchdb (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,4 +1,5 @@
1
- require_relative 'danarchy_couchdb/version'
1
+ require 'danarchy_couchdb/version'
2
+ require 'danarchy_couchdb/config_manager'
2
3
  require 'json'
3
4
  require 'net/http'
4
5
 
@@ -6,9 +7,9 @@ module DanarchyCouchDB
6
7
  class Connection
7
8
  def initialize(connection)
8
9
  @connection = connection
9
- uri = 'http' if !@connection[:use_ssl]
10
- uri = 'https' if @connection[:use_ssl]
11
- @uri = "#{uri}://#{connection[:host]}:#{connection[:port]}"
10
+ uri = 'http' if !@connection[:ssl]
11
+ uri = 'https' if @connection[:ssl]
12
+ @uri = "#{uri}://#{connection[:hostname]}:#{connection[:port]}"
12
13
  end
13
14
 
14
15
  def get(*args)
@@ -20,6 +21,12 @@ module DanarchyCouchDB
20
21
  uri += "/#{document}" if document
21
22
  uri += "/?rev=#{revision}" if revision
22
23
 
24
+ uri = URI.parse(URI.encode(uri))
25
+ JSON.parse(request(Net::HTTP::Get.new(uri)), symbolize_names: true)
26
+ end
27
+
28
+ def get_attachment(database, document, attachment)
29
+ uri = "#{@uri}/#{database}/#{document}/#{attachment}"
23
30
  uri = URI.parse(URI.encode(uri))
24
31
  request(Net::HTTP::Get.new(uri))
25
32
  end
@@ -36,7 +43,23 @@ module DanarchyCouchDB
36
43
  req = Net::HTTP::Put.new(uri)
37
44
  req["content-type"] = "application/json"
38
45
  req.body = data.to_json if data
39
- request(req)
46
+ JSON.parse(request(req), symbolize_names: true)
47
+ end
48
+
49
+ def put_attachment(*args)
50
+ database = args.shift
51
+ document = args.shift
52
+ revision = args.shift
53
+ attachment = args.shift
54
+ data = args.shift
55
+ type = args.shift
56
+
57
+ uri = "#{@uri}/#{database}/#{document}/#{attachment}?rev=#{revision}"
58
+ uri = URI.parse(URI.encode(uri))
59
+ req = Net::HTTP::Put.new(uri)
60
+ req["content-type"] = "#{type}"
61
+ req.body = data if data
62
+ JSON.parse(request(req), symbolize_names: true)
40
63
  end
41
64
 
42
65
  def delete(*args)
@@ -45,29 +68,44 @@ module DanarchyCouchDB
45
68
  revision = args.shift
46
69
 
47
70
  uri = "#{@uri}/#{database}"
48
- uri += "/#{document}" if document
49
- uri += "/?rev=#{revision}" if revision
71
+ uri += "/#{document}" if document
72
+ uri += "/?rev=#{revision}" if revision
50
73
 
51
74
  uri = URI.parse(URI.encode(uri))
52
- request(Net::HTTP::Delete.new(uri))
75
+ JSON.parse(request(Net::HTTP::Delete.new(uri)), symbolize_names: true)
76
+ end
77
+
78
+ def delete_attachment(*args)
79
+ database = args.shift
80
+ document = args.shift
81
+ revision = args.shift
82
+ attachment = args.shift
83
+
84
+ uri = "#{@uri}/#{database}/#{document}/#{attachment}?rev=#{revision}"
85
+ uri = URI.parse(URI.encode(uri))
86
+ JSON.parse(request(Net::HTTP::Delete.new(uri)), symbolize_names: true)
53
87
  end
54
88
 
55
89
  private
56
90
  def request(uri)
57
91
  response = nil
58
92
 
59
- Net::HTTP.start(@connection[:host], @connection[:port], :use_ssl => @connection[:use_ssl]) do |http|
60
- uri.basic_auth @connection[:user], @connection[:pass]
93
+ begin
94
+ Net::HTTP.start(@connection[:hostname], @connection[:port], :use_ssl => @connection[:ssl]) do |http|
95
+ uri.basic_auth @connection[:username], @connection[:password]
61
96
 
62
- response = http.request(uri)
97
+ response = http.request(uri)
63
98
 
64
- unless response.kind_of?(Net::HTTPSuccess)
65
- handle_error(uri, response)
66
- break
99
+ unless response.kind_of?(Net::HTTPSuccess)
100
+ handle_error(uri, response)
101
+ break
102
+ end
67
103
  end
104
+ rescue
105
+ return "{\"error\":\"Connection Failed\",\"reason\":\"#{uri.uri} is unreachable.\"}\n"
68
106
  end
69
107
 
70
- JSON.parse(response.body, symbolize_names: true)
108
+ response.body
71
109
  end
72
110
 
73
111
  def handle_error(req, res)
@@ -0,0 +1,74 @@
1
+
2
+ module DanarchyCouchDB::ConfigManager
3
+ class CouchDB
4
+ def initialize(account)
5
+ @account = account
6
+ end
7
+
8
+ def new_cdb_connection
9
+ if @account[:couchdb]
10
+ print "A CouchDB connection already exists. Should we overwrite it? (Y/n): "
11
+ answer = gets.chomp
12
+ if answer =~ /^n(o)?$/i
13
+ puts "Keeping existing CouchDB Connection."
14
+ return @account
15
+ end
16
+ elsif answer =~ /^y(es)?$/i
17
+ puts "Removing existing CouchDB connection."
18
+ @account.delete(:couchdb)
19
+ end
20
+
21
+ print "CouchDB Hostname: "
22
+ hostname = gets.chomp
23
+ print 'CouchDB Username: '
24
+ username = gets.chomp
25
+ print 'CouchDB Password: '
26
+ password = gets.chomp
27
+ print 'CouchDB Database: '
28
+ database = gets.chomp
29
+ print 'Enable SSL? (Y/n): '
30
+ ssl = gets.chomp
31
+ port = nil
32
+
33
+ if ssl =~ /^y(es)?$/i || ssl.empty?
34
+ ssl = true
35
+ port = '6984'
36
+ else
37
+ ssl = false
38
+ port = '5984'
39
+ end
40
+
41
+ add_couchdb(@account, hostname, username, password, database, port, ssl)
42
+ @account
43
+ end
44
+
45
+ def verify_connection(couchdb)
46
+ cdb = DanarchyCouchDB::Connection.new(couchdb)
47
+ cdb.get('')
48
+ end
49
+
50
+ private
51
+ def add_couchdb(account, hostname, username, password, database, port, ssl)
52
+ couchdb = { hostname: hostname,
53
+ username: username,
54
+ password: password,
55
+ database: database,
56
+ port: port,
57
+ ssl: ssl }
58
+
59
+ verify = verify_connection(couchdb)
60
+
61
+ if verify[:error]
62
+ puts "Failed to connect to: #{couchdb[:hostname]}:#{couchdb[:port]}! Not adding this connection."
63
+ return false
64
+ elsif verify[:couchdb] == 'Welcome'
65
+ puts "Adding CouchDB connection to: #{couchdb[:hostname]}:#{couchdb[:port]}."
66
+ @account[:couchdb] = couchdb
67
+ end
68
+ end
69
+
70
+ def delete_couchdb
71
+ @account.delete(:couchdb)
72
+ end
73
+ end
74
+ end
@@ -1,3 +1,3 @@
1
1
  module DanarchyCouchDB
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danarchy_couchdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan James
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-02 00:00:00.000000000 Z
11
+ date: 2018-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,7 @@ files:
73
73
  - bin/setup
74
74
  - danarchy_couchdb.gemspec
75
75
  - lib/danarchy_couchdb.rb
76
+ - lib/danarchy_couchdb/config_manager.rb
76
77
  - lib/danarchy_couchdb/version.rb
77
78
  homepage: https://github.com/danarchy85/danarchy_couchdb
78
79
  licenses: