Module: TwitterBackup::UI

Defined in:
lib/twitter_backup/ui.rb

Class Method Summary collapse

Class Method Details

.ask_credentialsObject

Raises:

  • (MissingCredentials)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/twitter_backup/ui.rb', line 4

def ask_credentials
  say "You need to give us necessary credentials"
  say "Get them at https://dev.twitter.com/apps"

  existing_credentials = TBConfig.options[:credentials]
  new_credentials = {}

  new_credentials[:consumer_key]       = ask("Consumer key?  ").to_s          if existing_credentials[:consumer_key].blank?
  new_credentials[:consumer_secret]    = ask("Consumer secret?  ").to_s       if existing_credentials[:consumer_secret].blank?
  new_credentials[:oauth_token]        = ask("Access token?  ").to_s          if existing_credentials[:oauth_token].blank?
  new_credentials[:oauth_token_secret] = ask("Access token secret?  ").to_s   if existing_credentials[:oauth_token_secret].blank?

  TBConfig.save_credentials new_credentials
  raise MissingCredentials if TBConfig.credentials_missing?
end

.define_path_to_backupObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/twitter_backup/ui.rb', line 20

def define_path_to_backup
  default_path = File.join(CONFIG_DIR, "tweets.yml")
  say "We are going to save your tweets to #{TBConfig.options[:db][:database]}"
  say "And also as plain text to #{default_path}"
  if agree ("Dou you want to define another directory for text copy?  ")
    backup_dir = ask("Enter a path to your direcory:  ") do |q|
      q.validate = lambda { |file| File.exist?(File.dirname(File.expand_path(file))) }
      q.responses[:not_valid] = "You can't save files there"
      q.confirm  = true
    end
    backup_file = File.expand_path(File.join(backup_dir, "tweets.yml"))
  else
    backup_file = default_path
  end
  
  TBConfig.save_backup_file backup_file
  
end

.exit_screenObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twitter_backup/ui.rb', line 44

def exit_screen
  if ActiveRecord::Base.connected?
    say %[.... Your tweets in this backup: #{Tweet.count}]
    if Tweet.earliest.present?
      say %[.... Your earliest tweet:]
      say %[.... <%= color("#{Tweet.earliest.try(:status)}", YELLOW) %>]
    end
    if Tweet.latest.present?
      say %[.... Your latest tweet:]
      say %[.... <%= color("#{Tweet.latest.try(:status)}", GREEN) %>]
    end
  end
end

.failed_backup_path_exit(message) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/twitter_backup/ui.rb', line 72

def failed_backup_path_exit message
  say %[.... <%= color("We've failed to create #{TBConfig.options[:backup_file]} for you.", RED) %>]
  say %[.... #{message}]
  TBConfig.save_backup_file("")
  say ".... Run this script again and chose another place to store your backup"
  say ".... Or edit it manually at #{TBConfig.config_file}"
  exit 1
end

.greet_userObject



39
40
41
42
# File 'lib/twitter_backup/ui.rb', line 39

def greet_user
  say %[.... <%= color("Tweets for: #{TBConfig.user.name}", GREEN) %>!]
  say %[.... Your tweets at twitter.com: #{TBConfig.user.statuses_count}]
end

.missing_credentials_exitObject



58
59
60
61
62
63
# File 'lib/twitter_backup/ui.rb', line 58

def missing_credentials_exit
  say %[.... <%= color("We can't work without credentials. Sorry.", RED) %>]
  say ".... Go get them at https://dev.twitter.com/apps"
  say ".... And run this script again"
  exit 1
end

.too_many_requests_exitObject



81
82
83
84
85
# File 'lib/twitter_backup/ui.rb', line 81

def too_many_requests_exit
  say %[.... <%= color("You've exceeded your request limit", RED) %>]
  say ".... Try again tomorrow =)"
  exit 1
end

.wrong_credentials_exitObject



65
66
67
68
69
70
# File 'lib/twitter_backup/ui.rb', line 65

def wrong_credentials_exit
  say %[.... <%= color("Your credentials are somehow wrong.", RED) %>]
  say ".... Go get the right ones at https://dev.twitter.com/apps"
  say ".... And run this script again"
  exit 1
end