Class: Vines::Storage::CouchDB

Inherits:
Vines::Storage show all
Defined in:
lib/vines/storage/couchdb.rb

Instance Attribute Summary

Attributes inherited from Vines::Storage

#ldap

Instance Method Summary collapse

Methods inherited from Vines::Storage

#authenticate, defer, fiber, from_name, #ldap?, register, wrap_ldap

Methods included from Log

#log

Constructor Details

#initialize(&block) ⇒ CouchDB

Returns a new instance of CouchDB.



18
19
20
21
22
23
# File 'lib/vines/storage/couchdb.rb', line 18

def initialize(&block)
  @config = {}
  instance_eval(&block)
  [:host, :port, :database].each {|key| raise "Must provide #{key}" unless @config[key] }
  @url = url(@config)
end

Instance Method Details

#find_fragment(jid, node) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/vines/storage/couchdb.rb', line 87

def find_fragment(jid, node)
  jid = JID.new(jid || '').bare.to_s
  if jid.empty? then yield; return end
  get(fragment_id(jid, node)) do |doc|
    fragment = if doc && doc['type'] == 'Fragment'
      Nokogiri::XML(doc['xml']).root rescue nil
    end
    yield fragment
  end
end

#find_user(jid) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vines/storage/couchdb.rb', line 25

def find_user(jid)
  jid = JID.new(jid || '').bare.to_s
  if jid.empty? then yield; return end
  get("user:#{jid}") do |doc|
    user = if doc && doc['type'] == 'User'
      User.new(:jid => jid).tap do |user|
        user.name, user.password = doc.values_at('name', 'password')
        (doc['roster'] || {}).each_pair do |jid, props|
          user.roster << Contact.new(
            :jid => jid,
            :name => props['name'],
            :subscription => props['subscription'],
            :ask => props['ask'],
            :groups => props['groups'] || [])
        end
      end
    end
    yield user
  end
end

#find_vcard(jid) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/vines/storage/couchdb.rb', line 63

def find_vcard(jid)
  jid = JID.new(jid || '').bare.to_s
  if jid.empty? then yield; return end
  get("vcard:#{jid}") do |doc|
    card = if doc && doc['type'] == 'Vcard'
      Nokogiri::XML(doc['card']).root rescue nil
    end
    yield card
  end
end

#save_fragment(jid, node, &callback) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/vines/storage/couchdb.rb', line 99

def save_fragment(jid, node, &callback)
  jid = JID.new(jid).bare.to_s
  id = fragment_id(jid, node)
  get(id) do |doc|
    doc ||= {'_id' => id}
    doc['type'] = 'Fragment'
    doc['xml'] = node.to_xml
    save_doc(doc, &callback)
  end
end

#save_user(user, &callback) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vines/storage/couchdb.rb', line 47

def save_user(user, &callback)
  id = "user:#{user.jid.bare}"
  get(id) do |doc|
    doc ||= {'_id' => id}
    doc['type'] = 'User'
    doc['name'] = user.name
    doc['password'] = user.password
    doc['roster'] = {}
    user.roster.each do |contact|
      doc['roster'][contact.jid.bare.to_s] = contact.to_h
    end
    save_doc(doc, &callback)
  end
end

#save_vcard(jid, card, &callback) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/vines/storage/couchdb.rb', line 75

def save_vcard(jid, card, &callback)
  jid = JID.new(jid).bare.to_s
  id = "vcard:#{jid}"
  get(id) do |doc|
    doc ||= {'_id' => id}
    doc['type'] = 'Vcard'
    doc['card'] = card.to_xml
    save_doc(doc, &callback)
  end
end