module Cloudant::Replicator

Public Instance Methods

active_tasks(type="replication") click to toggle source

The Replicator Module contains methods to replicate a database

Allows you to monitor a replication

# 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) click to toggle source

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

# 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) click to toggle source

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.

# 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) click to toggle source

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

# 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) click to toggle source

Method accepts options for replication document and builds query

# 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) click to toggle source

Sets the database to repliacte the 2 databases continuously.

# File lib/cloudant/replicator.rb, line 30
def sync(target)
  replicate_db(target,{:continuous => true, :create_target => true})
end