Class: Twords

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*screen_names) ⇒ Twords

Initializes a new Twords object

Parameters:

  • screen_names (Array<String>)

    any number of screen names to include in the analysis



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_namesArray<String>, Array (readonly)

The screen names included in the analysis

Returns:

  • (Array<String>)

    if names are provided to #initialize

  • (Array)

    if no names are provided to #initialize



18
19
20
# File 'lib/twords/instance_methods.rb', line 18

def screen_names
  @screen_names
end

#wordsHash (readonly)

The words and their number of occurrences

Returns:

  • (Hash)

    returns the word(String) and counts(Integer) as key-value pairs



24
25
26
# File 'lib/twords/instance_methods.rb', line 24

def words
  @words
end

Class Method Details

.clientTwords::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.

Yields:

Returns:



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

#audittrue

Fetch tweets and count words. Short circuits and returns true if already audited.

Returns:

  • (true)


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

Returns:

  • (true)

    always returns true unless an error is raised



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?

Returns:

  • (true)

    if already audited

  • (false)

    if not audited yet



42
43
44
# File 'lib/twords/instance_methods.rb', line 42

def audited?
  @audited
end

#percentagesHash

The frequency of each word as a share of the #total_word_count

Returns:

  • (Hash)

    returns the word(String) and percentage(Float) as key-value pairs



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_percentagesArray<Array<String, Float>>

Sorts #percentages in descending order

Returns:

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


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_tweetsArray<Twitter::Tweet>

Returns an array of #tweets sorted by time created in descending order

Returns:

  • (Array<Twitter::Tweet>)


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

Returns:

  • (Array<Twitter::Tweet>)


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_wordsArray<Array<String, Integer>> Also known as: words_forward

Sort words by frequency in descending order

Returns:

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


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_csvString

Generate a CSV formatted String of the sorted results, with column headers “word, count”

Returns:

  • (String)

    in CSV format



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_jsonString

Generate a JSON formatted String of the sorted results, as one hash object with word-count key-value pairs.

Returns:

  • (String)

    in JSON format



166
167
168
# File 'lib/twords/instance_methods.rb', line 166

def to_json
  sort_words.to_h.to_json
end

#total_word_countInteger

Total occurrences of all words included in analysis, i.e. sum of the count of all words.

Returns:

  • (Integer)


115
116
117
# File 'lib/twords/instance_methods.rb', line 115

def total_word_count
  @_total_word_count ||= words.values.reduce(:+)
end

#tweetsArray<Twitter::Tweet>

Returns all of the tweets that fall within the configured time range

Returns:

  • (Array<Twitter::Tweet>)


83
84
85
# File 'lib/twords/instance_methods.rb', line 83

def tweets
  @_tweets ||= client.filter_tweets(screen_names)
end

#tweets_countInteger

Number of tweets being analyzed

Returns:

  • (Integer)


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.

Parameters:

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

    File writing options. All except for :filename are passed to File#open.

Options Hash (opts):

  • :filename (String)

    A relative pathname. Defaults to ‘twords_report.csv’

Returns:

  • (Integer)

    representing the byte count of the 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.

Parameters:

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

    customizable file writing options. All but :filename arepassed to File#open

Options Hash (opts):

  • :filename (String)

    A relative pathname. Defaults to ‘twords_report.json’

Returns:

  • (Integer)

    representing the byte count of the 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