cloudant 0.1.3 → 0.1.4

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: ad5b5cd110b37dca3817af668014ebb8744b1562
4
- data.tar.gz: 79fc72e582dc8bf1e087b2e0e79e757e95a4015e
3
+ metadata.gz: 99609d3fa61ccfd3fa8046ea0af2610f81ee7c79
4
+ data.tar.gz: 16d69b0453ef9949469bbdb88c1e9b69799ba5e8
5
5
  SHA512:
6
- metadata.gz: 2cd186a5327fc5c86a79cc983babbbb14d6c4e4a101888ba9119acbab8574c1c3e574e6a0f04800a0322f6048543b6d553fd4cc1338b4ef89f36a448f133ce7e
7
- data.tar.gz: 120d73762c1aed47893600a6241f3708a4f478adeef3de15fe7fc87522651f523dcc0fbe2a9b9abab4fcb894e995b6f16cf2c125c8f4d75a678c703c29528f7b
6
+ metadata.gz: 04511a9264ea1ccdbd53b417c8d09be5d8484552f5bb00a3c9da86a3464b3fe87b17d3b24df8f262b4ffce2b0a7497b1581cdf531d45f75babfd322231e416d6
7
+ data.tar.gz: 4b911e228bfcb476c949de5d2f1c632022bd7bac3080bdd9e4ff0cd319941e109cd747662ee48a7bce1745280496f9c3a2b129f805a7c45e5961383424339a17
data/README.md CHANGED
@@ -169,7 +169,7 @@ username = "test_user"
169
169
  client.delete_user(username)
170
170
  ```
171
171
 
172
- **Database Replication and Sync)**
172
+ **Database Replication and Sync**
173
173
  ```ruby
174
174
  # View Active tasks (including replications)
175
175
  client.active_tasks
@@ -177,22 +177,49 @@ client.active_tasks
177
177
  # Replicate a database
178
178
  # The default options are {:create_target => true, :continuous => false}, meaning that
179
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
180
+ # newly created. If the database already exists, set :create_target => false
181
181
  client.replicate_db("test_2")
182
182
 
183
183
  # More options can be passed, for example:
184
184
  client.replicate_db("test_2", {:continuous => true, :user_ctx => {"name" => "test_user", "roles" => ["admin"]}})
185
185
 
186
+ # Replication defualts to the datbase initially set; a different source can be used by calling :replicate_dbs
187
+ client.replicate_dbs("test_2", "test_3", {:continuous => true, :user_ctx => {"name" => "test_user", "roles" => ["admin"]}})
188
+
186
189
  # Sync a database (replicate a database with :continuous => true)
187
190
  client.sync("test_2")
191
+
192
+ # To stop a replication, delete the replication doc as you would any other document
193
+ client.delete_doc(doc_id)
194
+ ```
195
+
196
+ **Attachments**
197
+ ```ruby
198
+ # Type refers to the MIME type of the attachment.
199
+ args = {
200
+ :id => "test_doc",
201
+ :name => "test_attachment",
202
+ :type => "text/html",
203
+ :rev => "1-f53050cbc4e66a4dcf6db59a9e0bc6b",
204
+ :path => "./test.html"
205
+ }
206
+
207
+ # Create an attachment on an existing document. If no rev is given, a call will be made to get the document and extract the rev.
208
+ client.create_attachment(args)
209
+ client.update_attachment(args) # Alias of :create_attachment
210
+
211
+ # Get the attachments associated with a document. The :name field is the name of the attachment to be retrieved.
212
+ client.read_attachment(:id => "test_doc", :name => "test_attachment")
213
+
214
+ # Delete an attachment. The current rev is required.
215
+ client.delete_attachment(:id => "test_doc", :name => "test_attachment", :rev => "2-b52037c9456d75e05f718c1286d63bf6")
188
216
  ```
189
217
 
190
218
  ## To Do
191
219
 
192
- - Add support for `attachments`
193
- - Expand database replication functionality - `/_replicator`
194
220
  - Add more robust options handling for various queries (expanding the `QueryBuilder` module, as used in view querying)
195
- - Currently, options have to be added to a query string by the user.
221
+ - Currently, options have to be added to a query string by the user.
222
+ - Expand the `Replication` module.
196
223
 
197
224
  ## Contributing
198
225
 
@@ -8,6 +8,7 @@ require "cloudant/utility"
8
8
  require "cloudant/query_builder"
9
9
  require "cloudant/security"
10
10
  require "cloudant/replicator"
11
+ require "cloudant/attachment"
11
12
  require "cloudant/api"
12
13
  require "cloudant/client"
13
14
  require "cloudant/connection"
@@ -4,5 +4,6 @@ module Cloudant
4
4
  include Cloudant::QueryBuilder
5
5
  include Cloudant::Security
6
6
  include Cloudant::Replicator
7
+ include Cloudant::Attachment
7
8
  end
8
9
  end
@@ -0,0 +1,64 @@
1
+ module Cloudant
2
+ module Attachment
3
+ # The Attachment Module contains methods to interact with document attachments.
4
+ #
5
+ # Add an attachment to an existing document.
6
+ # Accepts the document id, the MIME type (ie: image/jpg), and the name for the attachment
7
+ # a rev field is also required (and suggested), but if not, the document is searched and rev extracted
8
+ def create_attachment(args)
9
+ if args[:id]
10
+ args[:rev] = get_current_rev(args[:id]) unless args[:rev]
11
+ attachment = Cloudant::Attachment.make_attachment(args)
12
+ query_str = build_attachment_query(args)
13
+
14
+ @conn.query({url_path: query_str, opts: attachment, method: :put})
15
+ end
16
+ end
17
+ alias_method :update_attachment, :create_attachment
18
+
19
+ # Read a document's attachments.
20
+ # Accepts a document id and the name of an attachment associated with that doc.
21
+ def read_attachment(args)
22
+ query_str = build_attachment_query(args)
23
+
24
+ @conn.query({url_path: query_str, method: :get})
25
+ end
26
+
27
+ # Delete a document's attachment.
28
+ # Accepts a document id, the rev in question, and the name of an attachment associated with that doc.
29
+ def delete_attachment(args)
30
+ query_str = build_attachment_query(args)
31
+
32
+ @conn.query({url_path: query_str, method: :delete})
33
+ end
34
+
35
+ # Accepts a Hash including :doc => the name of the doc to which the attachment will be attached,
36
+ # file_name, the name to be given to the attachment, the doc's content type, and the attachment's
37
+ # file type.
38
+ # Returns attachment to be uploaded
39
+ def self.make_attachment(args)
40
+ doc_name = args[:id]
41
+ file_name = args[:name]
42
+ file_type = args[:type]
43
+ file_path = args[:path]
44
+
45
+ attachment = {
46
+ "_id" => doc_name,
47
+ "_attachments" => {
48
+ file_name => {
49
+ "content_type" => file_type
50
+ }
51
+ }
52
+ }
53
+
54
+ if File.exists?(file_path)
55
+ data = File.open(file_path,'rb').read
56
+ attachment["_attachments"][file_name]["data"] = data
57
+ else
58
+ raise Errno::ENOENT.new('file does not exist')
59
+ end
60
+
61
+ attachment
62
+ end
63
+ end
64
+ end
@@ -32,6 +32,11 @@ module Cloudant
32
32
  alias_method :get, :get_doc
33
33
  alias_method :doc, :get_doc
34
34
 
35
+ def get_current_rev(doc)
36
+ retrieved = get_doc(doc)
37
+ retrieved["_rev"]
38
+ end
39
+
35
40
  # A valid doc must be provided. The doc must be a hash that can.
36
41
  # Use create_docs to create multiple documents at once.
37
42
  def create_doc(doc)
@@ -140,8 +145,8 @@ module Cloudant
140
145
 
141
146
  args[:index] ? new_index["index"] = args[:index] : new_index["index"] = {}
142
147
 
143
- new_index["name"] = args[:name] if args[:name]
144
- new_index["ddoc"] = args[:ddoc] if args[:ddoc]
148
+ new_index["name"] = args[:name] if args[:name]
149
+ new_index["ddoc"] = args[:ddoc] if args[:ddoc]
145
150
 
146
151
  args[:type] ? new_index["type"] = args[:type] : new_index["type"] = "text"
147
152
 
@@ -29,5 +29,15 @@ module Cloudant
29
29
  return [:local_seq,:attachments,:att_encoding_info,:atts_since,:conflicts,:deleted_conflicts,:latest,:meta,:open_revs,:rev,:revs,:revs_info]
30
30
  end
31
31
  end
32
+
33
+ # Built the query string for attachments
34
+ def build_attachment_query(args)
35
+ q = ""
36
+ q << "#{database}"
37
+ q << "/#{args[:id]}"
38
+ q << "/#{args[:name]}"
39
+ q << "?rev=#{args[:rev]}" if args[:rev]
40
+ q
41
+ end
32
42
  end
33
43
  end
@@ -7,27 +7,48 @@ module Cloudant
7
7
  @conn.query({url_path: "_active_tasks", opts: {"type" => type}, method: :get})
8
8
  end
9
9
 
10
+ # Accepts a string, the name of a database towards which to replicate the database. and
11
+ # optionally a hash of options for creating the replication doc.
10
12
  def replicate_db(target,*opts)
11
13
  opts && opts[0] ? options = opts[0] : options = {}
14
+ options[:source] = database
15
+ options[:target] = target
16
+
17
+ replication(options)
18
+ end
12
19
 
20
+ # Allows database replication between 2 databses instead of defaulting to the set database.
21
+ def replicate_dbs(source,target,*opts)
22
+ opts && opts[0] ? options = opts[0] : options = {}
23
+ options[:source] = source
13
24
  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})
25
+
26
+ replication(options)
17
27
  end
18
28
 
29
+ # Sets the database to repliacte the 2 databases continuously.
19
30
  def sync(target)
20
31
  replicate_db(target,{:continuous => true, :create_target => true})
21
32
  end
22
33
 
34
+ # Method accepts options for replication document and builds query
35
+ def replication(args)
36
+ replication_doc = build_doc(args)
37
+
38
+ doc_name = Cloudant::Utility.generate_doc_name(args[:source],args[:target])
39
+ @conn.query({url_path: "_replicator/#{doc_name}", opts: replication_doc, method: :put})
40
+ end
41
+
42
+ # The default options assume that the target database does not exist and the replication
43
+ # will be one-time only.
23
44
  def build_doc(opts)
24
45
  fields = [:continuous,:create_target,:doc_ids,:filter,:proxy,:selector,:since_seq,:use_checkpoints,:user_ctx]
25
46
 
26
47
  replication_doc = {
27
- :source => "https://#{username}:#{password}@#{username}.cloudant.com/#{database}",
28
- :target => "https://#{username}:#{password}@#{username}.cloudant.com/#{opts[:target]}",
48
+ :source => "https://#{username}:#{password}@#{username}.cloudant.com/#{opts[:source]}",
49
+ :target => "https://#{username}:#{password}@#{username}.cloudant.com/#{opts[:target]}",
29
50
  :create_target => true,
30
- :continuous => false
51
+ :continuous => false
31
52
  }
32
53
 
33
54
  fields.each do |field|
@@ -1,3 +1,3 @@
1
1
  module Cloudant
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
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.3
4
+ version: 0.1.4
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-22 00:00:00.000000000 Z
11
+ date: 2016-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,6 +113,7 @@ files:
113
113
  - cloudant.gemspec
114
114
  - lib/cloudant.rb
115
115
  - lib/cloudant/api.rb
116
+ - lib/cloudant/attachment.rb
116
117
  - lib/cloudant/client.rb
117
118
  - lib/cloudant/connection.rb
118
119
  - lib/cloudant/query_builder.rb