Class: IDeleteMyTweets::Api

Inherits:
Object
  • Object
show all
Includes:
Presenter
Defined in:
lib/i_delete_my_tweets/api.rb

Constant Summary

Constants included from Presenter

Presenter::COLORS, Presenter::TABLE_STYLE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Presenter

#summary, #to_date, #to_human_time, #truncate, #tweet_not_found, #tweet_presenter

Constructor Details

#initialize(opts = {}) ⇒ Api

Returns a new instance of Api.



9
10
11
12
13
14
# File 'lib/i_delete_my_tweets/api.rb', line 9

def initialize(opts = {})
  @config = opts[:config] || Config.new
  @verbose, @log, @dry_run = opts[:verbose], opts[:logger], opts[:dry_run]
  @delete_count, @skipped_count, @not_found_count = 0, 0, 0
  @a_bit = opts[:a_bit] || 5
end

Instance Attribute Details

#a_bitObject (readonly)

Returns the value of attribute a_bit.



6
7
8
# File 'lib/i_delete_my_tweets/api.rb', line 6

def a_bit
  @a_bit
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/i_delete_my_tweets/api.rb', line 6

def config
  @config
end

#delete_countObject

Returns the value of attribute delete_count.



7
8
9
# File 'lib/i_delete_my_tweets/api.rb', line 7

def delete_count
  @delete_count
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



6
7
8
# File 'lib/i_delete_my_tweets/api.rb', line 6

def dry_run
  @dry_run
end

#logObject (readonly)

Returns the value of attribute log.



6
7
8
# File 'lib/i_delete_my_tweets/api.rb', line 6

def log
  @log
end

#not_found_countObject

Returns the value of attribute not_found_count.



7
8
9
# File 'lib/i_delete_my_tweets/api.rb', line 7

def not_found_count
  @not_found_count
end

#skipped_countObject

Returns the value of attribute skipped_count.



7
8
9
# File 'lib/i_delete_my_tweets/api.rb', line 7

def skipped_count
  @skipped_count
end

#verboseObject (readonly)

Returns the value of attribute verbose.



6
7
8
# File 'lib/i_delete_my_tweets/api.rb', line 6

def verbose
  @verbose
end

Instance Method Details

#clientObject



47
48
49
50
51
52
53
54
# File 'lib/i_delete_my_tweets/api.rb', line 47

def client
  @client ||= Twitter::REST::Client.new do |c|
    c.consumer_key = config.consumer_key
    c.consumer_secret = config.consumer_secret
    c.access_token = config.access_token
    c.access_token_secret = config.access_token_secret
  end
end

#traverse_api!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/i_delete_my_tweets/api.rb', line 16

def traverse_api!
  all_tweets.each do |tweet|
    if can_be_destroyed?(tweet)
      destroy_with_retry(tweet)
      sleep a_bit
    else
      self.skipped_count += 1
    end
  end
rescue IOError
  do_log(" 💥 Oops, there was a connection error! ", color: :red)
ensure
  do_log summary delete_count, skipped_count, not_found_count, dry_run
end

#traverse_csv!Object



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

def traverse_csv!
  CSV.foreach(config.path_to_csv, headers: true) do |row|
    tweet = csv_row_to_struct(row)
    if can_be_destroyed?(tweet)
      destroy_with_retry(tweet)
      sleep a_bit
    else
      self.skipped_count += 1
    end
  end
rescue IOError
  do_log(" 💥 Oops, there was a connection error! ", color: :red)
ensure
  do_log summary delete_count, skipped_count, not_found_count, dry_run
end

#verify_credentialsObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/i_delete_my_tweets/api.rb', line 56

def verify_credentials
  client.verify_credentials(skip_status: true)
  do_log(" 🎉 We could verify your ❝#{config.screen_name}❞ credentials with Twitter and it looks good! ", color: :white, force_new_line: false)
  true
rescue Twitter::Error => e
  if e.is_a?(Twitter::Error::Forbidden)
    do_log(" 🚫 Oops, Twitter cannot verify the crendentials for #{config.screen_name} ", color: :red)
  else
    do_log(" 🚫 Oops, something bad happened: #{e.message} ", color: :red)
  end
  false
end