Class: Twords
- Inherits:
-
Object
- Object
- Twords
- Includes:
- ConfigAccessible
- Defined in:
- lib/twords.rb,
lib/twords/version.rb,
lib/twords/word_matcher.rb,
lib/twords/configuration.rb,
lib/twords/twitter_client.rb,
lib/twords/follower_bot_cop.rb,
lib/twords/instance_methods.rb,
lib/twords/config_accessible.rb,
lib/generators/twords_generator.rb
Overview
Instance methods
Defined Under Namespace
Modules: ConfigAccessible, Generators Classes: Configuration, FollowerBotCop, TwitterClient, WordMatcher
Constant Summary collapse
- VERSION =
The current gem version
'0.2.5'.freeze
Instance Attribute Summary collapse
-
#screen_names ⇒ Array<String>, Array
readonly
The screen names included in the analysis.
-
#words ⇒ Hash
readonly
The words and their number of occurrences.
Class Method Summary collapse
-
.client ⇒ Twords::TwitterClient
Access the Twitter client.
-
.config {|Twords::Configuration| ... } ⇒ Twords::Configuration
Set configuration options.
-
.reset_config! ⇒ Twords::Configuration
Resets all configuration options to default settings.
Instance Method Summary collapse
-
#audit ⇒ true
Fetch tweets and count words.
-
#audit! ⇒ true
Clear all results and audit from scratch.
-
#audited? ⇒ true, false
Have the #screen_names already been audited?.
-
#initialize(*screen_names) ⇒ Twords
constructor
Initializes a new Twords object.
-
#percentages ⇒ Hash
The frequency of each word as a share of the #total_word_count.
-
#sort_percentages ⇒ Array<Array<String, Float>>
Sorts #percentages in descending order.
-
#sort_tweets ⇒ Array<Twitter::Tweet>
Returns an array of #tweets sorted by time created in descending order.
-
#sort_tweets! ⇒ Array<Twitter::Tweet>
#sort_tweets destructively.
-
#sort_words ⇒ Array<Array<String, Integer>>
(also: #words_forward)
Sort words by frequency in descending order.
-
#to_csv ⇒ String
Generate a CSV formatted String of the sorted results, with column headers “word, count”.
-
#to_json ⇒ String
Generate a JSON formatted String of the sorted results, as one hash object with word-count key-value pairs.
-
#total_word_count ⇒ Integer
Total occurrences of all words included in analysis, i.e.
-
#tweets ⇒ Array<Twitter::Tweet>
Returns all of the tweets that fall within the configured time range.
-
#tweets_count ⇒ Integer
Number of tweets being analyzed.
-
#write_to_csv(opts = {}) ⇒ Integer
Write the output of #to_csv to a file.
-
#write_to_json(opts = {}) ⇒ Integer
Write the output of #to_json to a file.
Constructor Details
#initialize(*screen_names) ⇒ Twords
Initializes a new Twords object
31 32 33 34 35 |
# File 'lib/twords/instance_methods.rb', line 31 def initialize(*screen_names) @screen_names = screen_names.flatten @words = {} @audited = false end |
Instance Attribute Details
#screen_names ⇒ Array<String>, Array (readonly)
The screen names included in the analysis
18 19 20 |
# File 'lib/twords/instance_methods.rb', line 18 def screen_names @screen_names end |
#words ⇒ Hash (readonly)
The words and their number of occurrences
24 25 26 |
# File 'lib/twords/instance_methods.rb', line 24 def words @words end |
Class Method Details
.client ⇒ Twords::TwitterClient
Access the Twitter client
63 64 65 |
# File 'lib/twords.rb', line 63 def self.client config.client end |
.config {|Twords::Configuration| ... } ⇒ Twords::Configuration
Set configuration options. The same configuration is shared accross all objects in the Twords namespace. Configuration can be changed on the fly and will affect all instantiated objects.
for block { |config| … } the default configuration settings.
46 47 48 49 |
# File 'lib/twords.rb', line 46 def self.config @configuration ||= Configuration.new @configuration.tap { |config| yield config if block_given? } end |
.reset_config! ⇒ Twords::Configuration
Resets all configuration options to default settings
55 56 57 |
# File 'lib/twords.rb', line 55 def self.reset_config! config.reset! end |
Instance Method Details
#audit ⇒ true
Fetch tweets and count words. Short circuits and returns true if already audited.
50 51 52 53 |
# File 'lib/twords/instance_methods.rb', line 50 def audit count_words unless audited? @audited = true end |
#audit! ⇒ true
Clear all results and audit from scratch
59 60 61 62 63 64 65 66 67 |
# File 'lib/twords/instance_methods.rb', line 59 def audit! instance_variables.reject { |ivar| %i[@screen_names @words].include?(ivar) }.each do |ivar| instance_variable_set(ivar, nil) end @audited = false audit end |
#audited? ⇒ true, false
Have the #screen_names already been audited?
42 43 44 |
# File 'lib/twords/instance_methods.rb', line 42 def audited? @audited end |
#percentages ⇒ Hash
The frequency of each word as a share of the #total_word_count
123 124 125 126 127 |
# File 'lib/twords/instance_methods.rb', line 123 def percentages @_percentages ||= words.each_with_object({}) do |word_count, hash| hash[word_count.first] = percentage(word_count.last) end end |
#sort_percentages ⇒ Array<Array<String, Float>>
Sorts #percentages in descending order
133 134 135 |
# File 'lib/twords/instance_methods.rb', line 133 def sort_percentages @_sort_percentages ||= percentages.sort { |a, b| b.last <=> a.last } end |
#sort_tweets ⇒ Array<Twitter::Tweet>
Returns an array of #tweets sorted by time created in descending order
91 92 93 |
# File 'lib/twords/instance_methods.rb', line 91 def sort_tweets tweets.sort { |a, b| b.created_at <=> a.created_at } end |
#sort_tweets! ⇒ Array<Twitter::Tweet>
#sort_tweets destructively
99 100 101 |
# File 'lib/twords/instance_methods.rb', line 99 def sort_tweets! tweets.sort! { |a, b| b.created_at <=> a.created_at } end |
#sort_words ⇒ Array<Array<String, Integer>> Also known as: words_forward
Sort words by frequency in descending order
73 74 75 76 |
# File 'lib/twords/instance_methods.rb', line 73 def sort_words audit @_sort_words ||= words.sort { |a, b| b.last <=> a.last } end |
#to_csv ⇒ String
Generate a CSV formatted String of the sorted results, with column headers “word, count”
141 142 143 144 145 146 147 148 |
# File 'lib/twords/instance_methods.rb', line 141 def to_csv CSV.generate do |csv| csv << %w[word count] sort_words.each do |word_count| csv << word_count end end end |
#to_json ⇒ String
Generate a JSON formatted String of the sorted results, as one hash object with word-count key-value pairs.
166 167 168 |
# File 'lib/twords/instance_methods.rb', line 166 def to_json sort_words.to_h.to_json end |
#total_word_count ⇒ Integer
Total occurrences of all words included in analysis, i.e. sum of the count of all words.
115 116 117 |
# File 'lib/twords/instance_methods.rb', line 115 def total_word_count @_total_word_count ||= words.values.reduce(:+) end |
#tweets ⇒ Array<Twitter::Tweet>
Returns all of the tweets that fall within the configured time range
83 84 85 |
# File 'lib/twords/instance_methods.rb', line 83 def tweets @_tweets ||= client.filter_tweets(screen_names) end |
#tweets_count ⇒ Integer
Number of tweets being analyzed
107 108 109 |
# File 'lib/twords/instance_methods.rb', line 107 def tweets_count @_tweets_count ||= tweets.count end |
#write_to_csv(opts = {}) ⇒ Integer
Write the output of #to_csv to a file.
156 157 158 159 |
# File 'lib/twords/instance_methods.rb', line 156 def write_to_csv(opts = {}) filename = opts.fetch(:filename) { 'twords_report.csv' } write_file(filename, :to_csv, opts) end |
#write_to_json(opts = {}) ⇒ Integer
Write the output of #to_json to a file.
176 177 178 179 |
# File 'lib/twords/instance_methods.rb', line 176 def write_to_json(opts = {}) filename = opts.fetch(:filename) { 'twords_report.json' } write_file(filename, :to_json, opts) end |