cloudant 0.1.2 → 0.1.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 323e21dbdef047f55e8659ab87b86849773f33bd
4
- data.tar.gz: de9591fe10e75a28b093a6650ab3e82f2c6c854f
3
+ metadata.gz: ad5b5cd110b37dca3817af668014ebb8744b1562
4
+ data.tar.gz: 79fc72e582dc8bf1e087b2e0e79e757e95a4015e
5
5
  SHA512:
6
- metadata.gz: d7965e9c118e21a7dc576b6bdee230ef47503b76fe434eb9b90068f8c9aa00bb756fbc6828874a5c507ade1ca86f276852a00aebf990c105f55248b7c4dc8857
7
- data.tar.gz: 8306676a225c5d36ffff93a7c230d80f78f2425fe2e1f2de80c09908b3c7aa7aec32459f0dd33714827e1ee2c3814f64392b35ad77d21a876b5fa753f1b4a8ec
6
+ metadata.gz: 2cd186a5327fc5c86a79cc983babbbb14d6c4e4a101888ba9119acbab8574c1c3e574e6a0f04800a0322f6048543b6d553fd4cc1338b4ef89f36a448f133ce7e
7
+ data.tar.gz: 120d73762c1aed47893600a6241f3708a4f478adeef3de15fe7fc87522651f523dcc0fbe2a9b9abab4fcb894e995b6f16cf2c125c8f4d75a678c703c29528f7b
data/README.md CHANGED
@@ -119,7 +119,7 @@ ddoc = {'language' => 'javascript', 'views' => {} }
119
119
  client.create_design_doc('test',ddoc)
120
120
  ```
121
121
 
122
- **[Querying](https://docs.cloudant.com/cloudant_query.html#finding-documents-using-an-index)**
122
+ **[Querying](https://docs.cloudant.com/cloudant_query.html#finding-documents-using-an-index)** a database
123
123
  ```ruby
124
124
  # Perform a single query
125
125
  q = {'selector': {'test_field': {'$exists': true}},'fields': ['_id', '_rev'],'limit': 1,'skip': 0}
@@ -132,19 +132,19 @@ client.bookmark_query(q) do |docs|
132
132
  end
133
133
  ```
134
134
 
135
- **[Views](https://docs.cloudant.com/creating_views.html#using-views)**
135
+ **[Views (MapReduce)](https://docs.cloudant.com/creating_views.html#using-views)**
136
136
  ```ruby
137
137
  # Creating a view
138
- client.create_view('test',{"current"=>{"reduce"=>"_count","map"=>"function (doc) {\n if (doc.status === \"name\") {\n emit(doc._id,1);\n }\n}"}})
138
+ client.create_view('test',{"current"=>{"reduce"=>"_count","map"=>"function (doc) {\n if (doc.field_type === \"name\") {\n emit(doc._id,1);\n }\n}"}})
139
139
 
140
140
  # Querying an existing view
141
141
  database = 'test'
142
- view_to_query = 'current'
142
+ view_name = 'current'
143
143
 
144
- client.view(database,view_to_query) # If a reduce function is given will return a 'rows' array with a single record, the value of the reduce
144
+ client.view(database,view_name) # If a reduce function is given will return a 'rows' array with a single record, the value of the reduce
145
145
  # => {"rows"=>[{"key"=>nil, "value"=>2}]}
146
146
 
147
- client.view(database,view_to_query, :reduce => false, :include_docs => true)
147
+ client.view(database,view_name, :reduce => false, :include_docs => true)
148
148
  # => {"total_rows"=>2, "offset"=>0, "rows"=>[{"id"=>"5d8e6c99198dfdde8accd8e019ba052", "key"=>"5d8e6c99198dfdde8accd8e019ba052", "value"=>1, "doc"=>{"_id"=>"5d8e6c99198dfdde8accd8e019ba052", "_rev"=>"1-7ebdb5b82e1cc4eaf2e27a711e9857c6", "a"=>10, "b"=>92, "c"=>31}}, {"id"=>"5d8e6c99898dcdd08accd8e019badab", "key"=>"5d8e6c99898dcdd0daccd8e019badab", "value"=>1, "doc"=>{"_id"=>"5d8e6c99898dcdd8daccd8e019badab", "_rev"=>"1-d36298f4391da575df61e170af2efa34", "b"=>12, "c"=>33}}]}
149
149
  ```
150
150
 
@@ -160,15 +160,39 @@ client.roles
160
160
  client.create_api_keys
161
161
 
162
162
  # Create a new user and assign it one or more permissions
163
- client.new_user(["reader", "writer"])
163
+ # Will return the credentials of the newly created user as well
164
+ client.new_user(["reader", "writer"])
165
+ # => {"password" => "str", "key" => "str", "ok" => true, "roles": ["_reader","_writer"]}
166
+
167
+ # Delete an existing user
168
+ username = "test_user"
169
+ client.delete_user(username)
170
+ ```
171
+
172
+ **Database Replication and Sync)**
173
+ ```ruby
174
+ # View Active tasks (including replications)
175
+ client.active_tasks
176
+
177
+ # Replicate a database
178
+ # The default options are {:create_target => true, :continuous => false}, meaning that
179
+ # the first argument provided will be the name of the target database, and it will be
180
+ # newly created. If the database already exists, set :continuous => false
181
+ client.replicate_db("test_2")
182
+
183
+ # More options can be passed, for example:
184
+ client.replicate_db("test_2", {:continuous => true, :user_ctx => {"name" => "test_user", "roles" => ["admin"]}})
185
+
186
+ # Sync a database (replicate a database with :continuous => true)
187
+ client.sync("test_2")
164
188
  ```
165
189
 
166
190
  ## To Do
167
191
 
168
- - Add database replication functionality - `/_replicator`
192
+ - Add support for `attachments`
193
+ - Expand database replication functionality - `/_replicator`
169
194
  - Add more robust options handling for various queries (expanding the `QueryBuilder` module, as used in view querying)
170
195
  - Currently, options have to be added to a query string by the user.
171
- - Add support for `attachments`
172
196
 
173
197
  ## Contributing
174
198
 
data/lib/cloudant.rb CHANGED
@@ -4,8 +4,10 @@ require 'uri'
4
4
  require 'rest-client'
5
5
 
6
6
  require "cloudant/version"
7
+ require "cloudant/utility"
7
8
  require "cloudant/query_builder"
8
9
  require "cloudant/security"
10
+ require "cloudant/replicator"
9
11
  require "cloudant/api"
10
12
  require "cloudant/client"
11
13
  require "cloudant/connection"
data/lib/cloudant/api.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  module Cloudant
2
2
  module API
3
+ include Cloudant::Utility
3
4
  include Cloudant::QueryBuilder
4
5
  include Cloudant::Security
6
+ include Cloudant::Replicator
5
7
  end
6
8
  end
@@ -7,10 +7,9 @@ module Cloudant
7
7
  fields = get_fields(type)
8
8
 
9
9
  fields.each do |field|
10
- key = field
11
10
  val = opts[field].to_s
12
11
 
13
- current = "#{key}=#{val}" if val != ""
12
+ current = "#{field}=#{val}" if val != ""
14
13
  query_str << "&" << current if (query_str != "" && current)
15
14
  query_str << "?" << current if (query_str == "" && current)
16
15
  end
@@ -18,7 +17,6 @@ module Cloudant
18
17
  query_str
19
18
  end
20
19
 
21
- # TODO: This will be expanded to calls other than /_view.
22
20
  def get_fields(type)
23
21
  case type
24
22
  when "view"
@@ -0,0 +1,41 @@
1
+ module Cloudant
2
+ module Replicator
3
+ # The Replicator Module contains methods to replicate a database
4
+ #
5
+ # Allows you to monitor a replication
6
+ def active_tasks(type="replication")
7
+ @conn.query({url_path: "_active_tasks", opts: {"type" => type}, method: :get})
8
+ end
9
+
10
+ def replicate_db(target,*opts)
11
+ opts && opts[0] ? options = opts[0] : options = {}
12
+
13
+ options[:target] = target
14
+ replication_doc = build_doc(options)
15
+ doc_name = Cloudant::Utility.generate_doc_name(database,target)
16
+ @conn.query({url_path: "_replicator/#{doc_name}", opts: replication_doc, method: :put})
17
+ end
18
+
19
+ def sync(target)
20
+ replicate_db(target,{:continuous => true, :create_target => true})
21
+ end
22
+
23
+ def build_doc(opts)
24
+ fields = [:continuous,:create_target,:doc_ids,:filter,:proxy,:selector,:since_seq,:use_checkpoints,:user_ctx]
25
+
26
+ replication_doc = {
27
+ :source => "https://#{username}:#{password}@#{username}.cloudant.com/#{database}",
28
+ :target => "https://#{username}:#{password}@#{username}.cloudant.com/#{opts[:target]}",
29
+ :create_target => true,
30
+ :continuous => false
31
+ }
32
+
33
+ fields.each do |field|
34
+ current = field.to_sym
35
+ replication_doc[current] = opts[current] if !opts[current].nil?
36
+ end
37
+
38
+ replication_doc
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,8 @@
1
+ module Cloudant
2
+ module Utility
3
+ def self.generate_doc_name(source,target)
4
+ time_now = Time.now.strftime('%F-%T').gsub(/(:|-)/,"_")
5
+ "#{source}_to_#{target}_#{time_now}"
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module Cloudant
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Yanai
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-17 00:00:00.000000000 Z
11
+ date: 2016-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,7 +116,9 @@ files:
116
116
  - lib/cloudant/client.rb
117
117
  - lib/cloudant/connection.rb
118
118
  - lib/cloudant/query_builder.rb
119
+ - lib/cloudant/replicator.rb
119
120
  - lib/cloudant/security.rb
121
+ - lib/cloudant/utility.rb
120
122
  - lib/cloudant/version.rb
121
123
  homepage: https://github.com/AlexYanai/cloudant
122
124
  licenses: