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