Class: Safubot::Twitter::Tweet

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

Overview

Tweet is both a Request source and general-purpose tweet storage. Mentions are automatically made into Requests, but not timeline tweets.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](id) ⇒ Object

Quickly look up a Tweet.

Parameters:

  • id

    The Twitter id.



61
62
63
# File 'lib/safubot/twitter.rb', line 61

def [](id)
  Tweet.where('raw.id' => id).first
end

.from(status) ⇒ Object

Find or create a Tweet.

Parameters:

  • status

    An appropriate ::Twitter or ::TweetStream object.



68
69
70
71
# File 'lib/safubot/twitter.rb', line 68

def from(status)
  Tweet.where('raw.id' => status['id']||status.id).first ||
	Tweet.create(:raw => status['attrs']||status)
end

Instance Method Details

#header_mentionsObject

Retrieve a substring containing the leading mentions.



85
86
87
88
# File 'lib/safubot/twitter.rb', line 85

def header_mentions
		m = text.match(/(@\w+ )+/)
		m.nil? ? [] : m[0].split
end

#make_requestObject

Finds or creates a Request sourced from this Tweet.



111
112
113
# File 'lib/safubot/twitter.rb', line 111

def make_request
		self.request || self.request = Request.create(:user => user, :source => self, :text => self.targetless_text)
end

#original_tweetObject

Returns the original tweet of which this is a RT if available.



91
92
93
# File 'lib/safubot/twitter.rb', line 91

def original_tweet
		self.raw['retweeted_status'] && Tweet.from(self.raw['retweeted_status'])
end

#posted_atObject

Returns a Time object specifying the time of publication.



106
107
108
# File 'lib/safubot/twitter.rb', line 106

def posted_at
		Time.parse(self.raw['created_at'])
end

#targetless_textObject

Plain text without the leading mentions.



80
81
82
# File 'lib/safubot/twitter.rb', line 80

def targetless_text
		text.gsub(/^(@\w+ )+/, '')
end

#textObject

The plain text.



75
76
77
# File 'lib/safubot/twitter.rb', line 75

def text
		self.raw['text']
end

#userObject

Finds or creates a KnownUser connected to this Tweet.



96
97
98
# File 'lib/safubot/twitter.rb', line 96

def user
		KnownUser.by_twitter(self.raw['user']['id'])
end

#usernameObject

Finds the user’s screen_name.



101
102
103
# File 'lib/safubot/twitter.rb', line 101

def username
		user.twitter['screen_name']
end