class Vines::Storage::Mongomapper

Public Class Methods

new(&block) click to toggle source
# File lib/vines/mongomapper.rb, line 19
def initialize(&block)
  @config, @hosts = {}, []
  instance_eval(&block)
  raise "Must provide database" unless @config[:database]
  raise "Must provide at least one host connection" if @hosts.empty?
  connect
end

Public Instance Methods

find_fragment(jid, node) click to toggle source
# File lib/vines/mongomapper.rb, line 87
def find_fragment(jid, node)

end
find_user(jid) click to toggle source
# File lib/vines/mongomapper.rb, line 33
def find_user(jid)


  jid = JID.new(jid).bare.to_s
  return if jid.empty?

  #find a usr by it's bson id
  u = Vines::User.where(:_id=>jid_to_bson_id(jid)).first

  # find a user by it's name
  user_name = jid.split('@')[0]
  u = Vines::User.where(:user_name=>user_name).first unless u
  u.jid = JID.new(jid)if u
  u.roster=[] if u
  u

end
find_vcard(jid) click to toggle source

def save_user(user)

jid = user.jid.bare.to_s
user_name = user.user_name

#find a user by name create a new if failed
u = Vines::User.where(:user_name=>user_name).first
u = Vines::User.new(jid:user.jid) unless u

u.email = jid
u.name = user.name
u.user_name = user_name
u.password = user.password

# doc['roster'] = {}
# user.roster.each do |contact|
#   doc['roster'][contact.jid.bare.to_s] = contact.to_h
# end
save(u)

end

# File lib/vines/mongomapper.rb, line 79
def find_vcard(jid)

end
get() click to toggle source
# File lib/vines/mongomapper.rb, line 96
def get

end
host(name, port) click to toggle source
# File lib/vines/mongomapper.rb, line 27
def host(name, port)
  pair = [name, port]
  raise "duplicate hosts not allowed: #{name}:#{port}" if @hosts.include?(pair)
  @hosts << pair
end
save(model) click to toggle source
# File lib/vines/mongomapper.rb, line 101
def save(model)
  model.save!
end
save_fragment(jid, node) click to toggle source
# File lib/vines/mongomapper.rb, line 91
def save_fragment(jid, node)

end
save_vcard(jid, card) click to toggle source
# File lib/vines/mongomapper.rb, line 83
def save_vcard(jid, card)

end

Private Instance Methods

connect() click to toggle source
# File lib/vines/mongomapper.rb, line 126
def connect

  opts = {
      pool_timeout: 5,
      pool_size: @config[:pool] || 5,
      ssl: @config[:tls]
  }



  conn = if @hosts.size == 1
           Mongo::Connection.new(@hosts.first[0], @hosts.first[1], opts)
         else
           Mongo::ReplSetConnection.new(*@hosts, opts)
         end

  conn.db(@config[:database]).tap do |db|
    user = @config[:username] || ''
    pass = @config[:password] || ''
    db.authenticate(user, pass) unless user.empty? || pass.empty?
  end
  MongoMapper.connection = conn
  MongoMapper.database = @config[:database]
end
fragment_id(jid, node) click to toggle source
# File lib/vines/mongomapper.rb, line 107
def fragment_id(jid, node)
  id = Digest::SHA1.hexdigest("#{node.name}:#{node.namespace.href}")
  "#{jid}:#{id}"
end
jid_to_bson_id(jid) click to toggle source
# File lib/vines/mongomapper.rb, line 121
def jid_to_bson_id(jid)
  id = jid.split('@')[0]
  BSON::ObjectId(id) rescue nil
end