Module: Cloudant::Replicator

Included in:
API
Defined in:
lib/cloudant/replicator.rb

Instance Method Summary collapse

Instance Method Details

#active_tasks(type = "replication") ⇒ Object

The Replicator Module contains methods to replicate a database

Allows you to monitor a replication



6
7
8
# File 'lib/cloudant/replicator.rb', line 6

def active_tasks(type="replication")
  @conn.query({url_path: "_active_tasks", opts: {"type" => type}, method: :get})
end

#build_doc(opts) ⇒ Object

The default options assume that the target database does not exist and the replication will be one-time only.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cloudant/replicator.rb', line 44

def build_doc(opts)
  fields = [:continuous,:create_target,:doc_ids,:filter,:proxy,:selector,:since_seq,:use_checkpoints,:user_ctx]

  replication_doc = {
    :source        => "https://#{username}:#{password}@#{username}.cloudant.com/#{opts[:source]}",
    :target        => "https://#{username}:#{password}@#{username}.cloudant.com/#{opts[:target]}",
    :create_target => true,
    :continuous    => false
  }  

  fields.each do |field|
    current = field.to_sym
    replication_doc[current] = opts[current] if !opts[current].nil?
  end    

  replication_doc
end

#replicate_db(target, *opts) ⇒ Object

Accepts a string, the name of a database towards which to replicate the database. and optionally a hash of options for creating the replication doc.



12
13
14
15
16
17
18
# File 'lib/cloudant/replicator.rb', line 12

def replicate_db(target,*opts)
  opts && opts[0] ? options = opts[0] : options = {}
  options[:source] = database
  options[:target] = target
  
  replication(options)
end

#replicate_dbs(source, target, *opts) ⇒ Object

Allows database replication between 2 databses instead of defaulting to the set database.



21
22
23
24
25
26
27
# File 'lib/cloudant/replicator.rb', line 21

def replicate_dbs(source,target,*opts)
  opts && opts[0] ? options = opts[0] : options = {}
  options[:source] = source
  options[:target] = target
  
  replication(options)
end

#replication(args) ⇒ Object

Method accepts options for replication document and builds query



35
36
37
38
39
40
# File 'lib/cloudant/replicator.rb', line 35

def replication(args)
  replication_doc = build_doc(args)

  doc_name = Cloudant::Utility.generate_doc_name(args[:source],args[:target])
  @conn.query({url_path: "_replicator/#{doc_name}", opts: replication_doc, method: :put})
end

#sync(target) ⇒ Object

Sets the database to repliacte the 2 databases continuously.



30
31
32
# File 'lib/cloudant/replicator.rb', line 30

def sync(target)
  replicate_db(target,{:continuous => true, :create_target => true})
end