Class: Safubot::KnownUser
- Inherits:
-
Object
- Object
- Safubot::KnownUser
- 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)
-
+ (Object) by_name(name)
Retrieves user by internal identifier.
-
+ (Object) by_twitter(name_or_id)
Retrieve or construct a KnownUser by the given Twitter screen name.
-
+ (Object) by_xmpp(jid)
Retrieves or creates a KnownUser by an XMPP JID string.
Instance Method Summary (collapse)
-
- (Object) merge(user)
Combines the service-specific identities of two users to form a cohesive picture of an individual.
-
- (Object) tweets
Plucky query with Tweets scoped to this user’s twitter id.
Class Method Details
+ (Object) by_name(name)
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 |
+ (Object) by_twitter(name_or_id)
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 |
+ (Object) by_xmpp(jid)
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
- (Object) merge(user)
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 |
- (Object) tweets
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 |