Class: Safubot::KnownUser

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
lib/safubot/known_user.rb,
lib/safubot/xmpp.rb,
lib/safubot/twitter.rb,
lib/safubot/test_helper.rb

Overview

General identity cache. Extended by service-specific modules. A single class in order to allow linking of identity between services.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_name(name) ⇒ Object

Retrieves user by internal identifier.



11
12
13
14
# File 'lib/safubot/test_helper.rb', line 11

def by_name(name)
    KnownUser.where(:name => name).first ||
 KnownUser.create(:name => name)
end

.by_twitter(name_or_id) ⇒ Object

Retrieve or construct a KnownUser by the given Twitter screen name.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/safubot/twitter.rb', line 11

def by_twitter(name_or_id)
    user = if name_or_id.is_a? String
 KnownUser.where('twitter.screen_name' => name_or_id).first
    else
 KnownUser.where('twitter.id' => name_or_id).first
    end

    if user.nil?
 details = ::Twitter.user(name_or_id)
 # This second lookup is necessary as the screen_name that comes back from
 # Twitter.user might not be the same as the one we were originally provided
 # with.
 user = KnownUser.where('twitter.screen_name' => details.screen_name).first ||
KnownUser.create(:twitter => details.attrs, :name => details.screen_name)
    end

    user
end

.by_xmpp(jid) ⇒ Object

Retrieves or creates a KnownUser by an XMPP JID string.



11
12
13
14
15
# File 'lib/safubot/xmpp.rb', line 11

def by_xmpp(jid)
    jid = jid.split('/')[0]
    KnownUser.where('jid' => jid).first ||
 KnownUser.create(:jid => jid, :name => jid.split('@')[0])
end

Instance Method Details

#merge(user) ⇒ Object

Combines the service-specific identities of two users to form a cohesive picture of an individual.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/safubot/known_user.rb', line 14

def merge(user)
    self.keys.keys.each do |key|
    Log.info "#{key.inspect} #{self[key].inspect} #{user[key].inspect}"
    self[key] = user[key] if self[key].nil? || (self[key].respond_to?(:empty?) && self[key].empty?)
    end

    user.requests.each do |req|
    req.user = self; req.save!
    end

    save!

    user.destroy
end

#tweetsObject

Plucky query with Tweets scoped to this user’s twitter id.



32
33
34
# File 'lib/safubot/twitter.rb', line 32

def tweets
  Twitter::Tweet.where('raw.user.id' => self.twitter['id'])
end