Class: IDeleteMyTweets::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/i_delete_my_tweets/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/i_delete_my_tweets/config.rb', line 32

def initialize(opts = {})
  @consumer_key = opts[:consumer_key] || ENV.fetch("CONSUMER_KEY", nil)
  @consumer_secret = opts[:consumer_secret] || ENV.fetch("CONSUMER_SECRET", nil)
  @access_token = opts[:access_token] || ENV.fetch("ACCESS_TOKEN", nil)
  @access_token_secret = opts[:access_token_secret] || ENV.fetch("ACCESS_TOKEN_SECRET", nil)
  @older_than = opts[:older_than] || Time.parse(ENV.fetch("OLDER_THAN", 1.week.ago).to_s)
  @path_to_csv = opts[:path_to_csv] || ENV.fetch("PATH_TO_CSV", "./")
  @fave_threshold = opts[:fave_threshold] || ENV.fetch("FAVE_THRESHOLD", 0)
  @rt_threshold = opts[:rt_threshold] || ENV.fetch("RT_THRESHOLD", 0)
  @with_words = opts[:with_words] || ENV.fetch("WITH_WORDS", "")
  @screen_name = opts[:screen_name] || ENV.fetch("SCREEN_NAME", "")
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def access_token
  @access_token
end

#access_token_secretObject

Returns the value of attribute access_token_secret.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def access_token_secret
  @access_token_secret
end

#consumer_keyObject

Returns the value of attribute consumer_key.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def consumer_secret
  @consumer_secret
end

#fave_thresholdObject

Returns the value of attribute fave_threshold.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def fave_threshold
  @fave_threshold
end

#older_thanObject

Returns the value of attribute older_than.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def older_than
  @older_than
end

#path_to_csvObject

Returns the value of attribute path_to_csv.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def path_to_csv
  @path_to_csv
end

#rt_thresholdObject

Returns the value of attribute rt_threshold.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def rt_threshold
  @rt_threshold
end

#screen_nameObject

Returns the value of attribute screen_name.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def screen_name
  @screen_name
end

#with_wordsObject

Returns the value of attribute with_words.



21
22
23
# File 'lib/i_delete_my_tweets/config.rb', line 21

def with_words
  @with_words
end

Instance Method Details

#compiled_words_regexObject



74
75
76
77
78
79
80
# File 'lib/i_delete_my_tweets/config.rb', line 74

def compiled_words_regex
  @compiled_words_regex ||= Regexp.union(
    with_words.split(",")
      .map(&:squish)
      .map { |word| /(?<=\s|^)#{Regexp.quote(word)}\b/i }
  )
end

#empty_valuesObject



68
69
70
71
72
# File 'lib/i_delete_my_tweets/config.rb', line 68

def empty_values
  zipped.reject { |tuples| OPTIONALS.member?(tuples.first.downcase) }
        .map { |tuples| tuples.second.to_s.empty? ? tuples.first : nil }
        .compact
end

#to_envObject



63
64
65
66
# File 'lib/i_delete_my_tweets/config.rb', line 63

def to_env
  CONFIG_VARS.map { |k| "#{k}='#{send(k.downcase.to_sym).to_s.gsub("'"){ "\\'" }}'" }
  # ^ Escaping with single quotes because some shells are not nice with ruby unquoted timestamps
end

#to_tableObject



54
55
56
57
58
59
60
61
# File 'lib/i_delete_my_tweets/config.rb', line 54

def to_table
  Terminal::Table.new do |table|
    table.title = "[I Delete My Tweets] Configuration"
    table.headings = ["KEY", "VALUE"]
    table.rows = zipped
    table.style = Presenter::TABLE_STYLE
  end
end

#zippedObject



45
46
47
48
49
50
51
52
# File 'lib/i_delete_my_tweets/config.rb', line 45

def zipped
  values = CONFIG_VARS.map do |k|
    normalized_key = k.downcase
    value = send(normalized_key.to_sym)
    OBFUSCATE_WORDS.any? { |word| normalized_key.include?(word) } ? obfuscate_token(value) : value
  end
  CONFIG_VARS.zip(values)
end