Class: Couch

Inherits:
Object
  • Object
show all
Defined in:
lib/couch.rb,
lib/couch/client.rb,
lib/couch/version.rb,
lib/couch/request.rb,
lib/couch/response.rb,
lib/couch/replication.rb

Defined Under Namespace

Classes: Client, Replication, Request, Response

Constant Summary collapse

VERSION =
'0.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Couch

Returns a new instance of Couch


13
14
15
16
# File 'lib/couch.rb', line 13

def initialize(uri)
  @uri = URI(uri)
  @client = Client.new(@uri)
end

Class Method Details

.replicate(source, target) ⇒ Object


4
5
6
# File 'lib/couch/replication.rb', line 4

def self.replicate(source, target)
  Replication.new(source, target).replicate
end

Instance Method Details

#all_dbsObject


78
79
80
# File 'lib/couch.rb', line 78

def all_dbs
  @client.request(:get, '/_all_dbs').json
end

#bulk_docs(docs, options = {}) ⇒ Object


31
32
33
34
35
36
# File 'lib/couch.rb', line 31

def bulk_docs(docs, options = {})
  if docs.any?
    body = options.merge(docs: docs)
    @client.request(:post, bulk_docs_uri, body).successful?
  end
end

#changes(options = {}) ⇒ Object


26
27
28
29
# File 'lib/couch.rb', line 26

def changes(options = {})
  response = @client.request(:get, changes_uri(options))
  response.json['results']
end

#createObject


22
23
24
# File 'lib/couch.rb', line 22

def create
  @client.request(:put, @uri.path).successful?
end

#destroyObject


18
19
20
# File 'lib/couch.rb', line 18

def destroy
  @client.request(:delete, @uri.path).successful?
end

#get(doc, options = {}) ⇒ Object


64
65
66
67
68
69
70
71
# File 'lib/couch.rb', line 64

def get(doc, options = {})
  response = @client.request(:get, doc_uri(doc['_id'], options))
  if options[:open_revs]
    response.json.map { |row| row.values.first }
  else
    response.json
  end
end

#post(doc) ⇒ Object


54
55
56
57
58
59
60
61
62
# File 'lib/couch.rb', line 54

def post(doc)
  response = @client.request(:post, @uri.path, doc)
  if response.successful?
    doc.merge('_id'  => response.json['id'],
              '_rev' => response.json['rev'])
  else
    false
  end
end

#put(doc, options = {}) ⇒ Object


45
46
47
48
49
50
51
52
# File 'lib/couch.rb', line 45

def put(doc, options = {})
  response = @client.request(:put, doc_uri(doc['_id'], options), doc)
  if response.successful?
    doc.merge('_rev' => response.json['rev'])
  else
    false
  end
end

#revs_diff(revs) ⇒ Object


38
39
40
41
42
43
# File 'lib/couch.rb', line 38

def revs_diff(revs)
  response = @client.request(:post, revs_diff_uri, revs)
  response.json.inject({}) do |a, (id, revs)|
    a.merge(id => revs['missing'])
  end
end

#uuids(count) ⇒ Object


73
74
75
76
# File 'lib/couch.rb', line 73

def uuids(count)
  response = @client.request(:get, uuids_uri(count))
  response.json['uuids']
end