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

+ (Object) [](id)

Quickly look up a Tweet.



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

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

+ (Object) from(status)

Find or create a Tweet.



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

- (Object) header_mentions

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

- (Object) make_request

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

- (Object) original_tweet

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

- (Object) posted_at

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

- (Object) targetless_text

Plain text without the leading mentions.



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

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

- (Object) text

The plain text.



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

def text
		self.raw['text']
end

- (Object) user

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

- (Object) username

Finds the user's screen_name.



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

def username
		user.twitter['screen_name']
end