lib/vines/storage/couchdb.rb in vines-0.3.2 vs lib/vines/storage/couchdb.rb in vines-0.4.0

- old
+ new

@@ -22,93 +22,72 @@ @url = url(@config) end 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 + return if jid.empty? + doc = get("user:#{jid}") + return unless 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 - yield user end end - fiber :find_user - def save_user(user, &callback) + def save_user(user) 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) + doc = get(id) || {'_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) end - fiber :save_user 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 + return if jid.empty? + doc = get("vcard:#{jid}") + return unless doc && doc['type'] == 'Vcard' + Nokogiri::XML(doc['card']).root rescue nil end - fiber :find_vcard - def save_vcard(jid, card, &callback) + def save_vcard(jid, card) 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 + doc = get(id) || {'_id' => id} + doc['type'] = 'Vcard' + doc['card'] = card.to_xml + save_doc(doc) end - fiber :save_vcard 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 + return if jid.empty? + doc = get(fragment_id(jid, node)) + return unless doc && doc['type'] == 'Fragment' + Nokogiri::XML(doc['xml']).root rescue nil end - fiber :find_fragment - def save_fragment(jid, node, &callback) + def save_fragment(jid, node) 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 + doc = get(id) || {'_id' => id} + doc['type'] = 'Fragment' + doc['xml'] = node.to_xml + save_doc(doc) end - fiber :save_fragment private def fragment_id(jid, node) id = Digest::SHA1.hexdigest("#{node.name}:#{node.namespace.href}") @@ -130,16 +109,18 @@ JSON.parse(http.response) rescue nil end yield doc end end + fiber :get - def save_doc(doc, &callback) - http = EM::HttpRequest.new(@url).post( - :head => {'Content-Type' => 'application/json'}, :body => doc.to_json) - http.callback(&callback) if callback - http.errback(&callback) if callback + def save_doc(doc) + head = {'Content-Type' => 'application/json'} + http = EM::HttpRequest.new(@url).post(head: head, body: doc.to_json) + http.callback { yield } + http.errback { yield } end + fiber :save_doc def escape(jid) URI.escape(jid, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end end