Class: Tweetly::User

Inherits:
Object
  • Object
show all
Defined in:
lib/tweetly/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ User

Returns a new instance of User.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tweetly/user.rb', line 6

def initialize(name)
	user = Twitter.user(name)
	if !user.protected?
		@name = user.name
		@screen_name = user.screen_name
		@protected = user.protected?
		@statuses_count = user.statuses_count
		@timeline = []
	else
		raise Tweetly::Error::Unauthorized, "User is protected", caller
	end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/tweetly/user.rb', line 4

def name
  @name
end

#protectedObject

Returns the value of attribute protected.



4
5
6
# File 'lib/tweetly/user.rb', line 4

def protected
  @protected
end

#screen_nameObject

Returns the value of attribute screen_name.



4
5
6
# File 'lib/tweetly/user.rb', line 4

def screen_name
  @screen_name
end

#statuses_countObject

Returns the value of attribute statuses_count.



4
5
6
# File 'lib/tweetly/user.rb', line 4

def statuses_count
  @statuses_count
end

#timelineObject

Returns the value of attribute timeline.



4
5
6
# File 'lib/tweetly/user.rb', line 4

def timeline
  @timeline
end

Instance Method Details

#fetch_timeline(count = 1000) ⇒ Boolean

TODO:

Duplicate tweets due to max_id

Fetches user timeline and caches it in the @timeline field.

Parameters:

  • count (Integer) (defaults to: 1000)

    the number of Tweets to fetch

Returns:

  • (Boolean)

    Success or failure (amount already cached)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/tweetly/user.rb', line 103

def fetch_timeline(count=1000)
	# Twitter API limits to 3200 tweets
	count = [count, 3200].min

	if @timeline.count < count
		opts = { 
			trim_user: 1,
			include_rts: 1
		}

		while @timeline.length < count
			# Fetch only what we don't have
			opts[:max_id] = @timeline.last.id unless @timeline.empty?

			# Max step count is 200 by API
			# We may need less depending on what's already fetched
			opts[:count] = [200, count - @timeline.count].min

			# Fetch an extra since max_id is included
			opts[:count] += 1 if opts[:max_id]

			resp = Twitter.user_timeline(@screen_name, opts)

			# Avoid reinserting max_id tweet
			resp.delete_at(0) if opts[:max_id]

			resp.each { |tweet| @timeline << tweet }
		end

		return true
	else
		# We've pulled this amount or more already
		return false
	end
end

Prints word frequency distribution list.

Parameters:

  • options (Hash) (defaults to: {})

    printing options

  • [Integer] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tweetly/user.rb', line 81

def print_word_freq(options={})
	opts = {
		tweets: 1000,
		print_count: true
	}
	opts.merge!(options)
	dist = word_freq(opts)
	opts[:count] ||= dist.count
	dist[0,opts[:count]].each do |k,v| 
		line = "#{k}"
		line += " (#{v})" if opts[:print_count]
		puts line
	end
	return
end

#word_freq(options = {}) ⇒ Array<Array<String, Integer>>

Builds a word frequency array from recent tweets.

Parameters:

  • options (Hash) (defaults to: {})

    frequency distribution options

  • [Integer] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

  • [Array<String>] (Hash)

    a customizable set of options

Returns:

  • (Array<Array<String, Integer>>)

    list of word frequencies in descending order



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tweetly/user.rb', line 29

def word_freq(options={})
	# Default parameters
	params = {
		tweets: 1000,
		words_only: false,
		case_sensitive: true,
		include_rts: true,
		min_length: nil,
		ignore: []
	}
	params.merge!(options)

	# Check for users with < 1000 tweets
	params[:tweets] = [params[:tweets], @statuses_count].min

	# Fetch Tweets if necessary
	fetch_timeline(params[:tweets]) if @timeline.count < params[:tweets]

	freqDist = {}
	@timeline.each_with_index do |tweet, i|
		# :include_rts option
		next if !params[:include_rts] && tweet.retweeted_status

		tweet.text.split.each do |word|
			# Adjust according to options
			next if params[:ignore].include? word
			word.downcase! if !params[:case_sensitive]
			if params[:words_only]
				word = word.match(/\w+/)
				next unless word
				word = word[0]
			end
			next if params[:min_length] && word.length < params[:min_length]

			if freqDist.has_key? word
				freqDist[word] += 1
			else
				freqDist[word] = 1
			end
		end

		break if i == params[:tweets]
	end
	freqDist.sort_by { |k,v| -v }
end