Class: TwitterBackup::TBConfig

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

Class Method Summary collapse

Class Method Details

.check_backup_fileObject



93
94
95
96
97
98
99
100
# File 'lib/twitter_backup/config.rb', line 93

def check_backup_file
  begin 
    backup_file = File.expand_path(options[:backup_file])
    TwitterBackup.prepare_file( backup_file ) unless File.exist?( backup_file )
  rescue Exception => e
    raise Error::InvalidBackupFile, e.message
  end
end

.config_fileObject



123
124
125
# File 'lib/twitter_backup/config.rb', line 123

def config_file
  passed_opts[:config] || CONFIG_FILE
end

.configureObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/twitter_backup/config.rb', line 102

def configure
  say ".... configuring database" if passed_opts.verbose?
  configure_database

  say ".... configuring twitter gem" if passed_opts.verbose?
  configure_twitter_gem

  say ".... checking backup file: #{options[:backup_file]}" if passed_opts.verbose?
  check_backup_file
end

.configure_databaseObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/twitter_backup/config.rb', line 70

def configure_database
  ActiveRecord::Base.establish_connection(options[:db])
  unless ActiveRecord::Base.connection.tables.include?("tweets")
    ActiveRecord::Migration.create_table :tweets do |t|
      t.text :status
      t.integer :status_id
      t.timestamp :created_at
      t.text :raw
    end
  end
end

.configure_twitter_gemObject

Raises:

  • (MissingCredentials)


82
83
84
85
86
87
88
89
90
91
# File 'lib/twitter_backup/config.rb', line 82

def configure_twitter_gem
  raise MissingCredentials if credentials_missing?
  Twitter.configure do |config|
    config.consumer_key = options[:credentials][:consumer_key]
    config.consumer_secret = options[:credentials][:consumer_secret]
    config.oauth_token = options[:credentials][:oauth_token]
    config.oauth_token_secret = options[:credentials][:oauth_token_secret]
  end
  @@twitter_gem_configured = true
end

.credentials_missing?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/twitter_backup/config.rb', line 44

def credentials_missing?
  options[:credentials].values.map(&:strip).include? ""
end

.empty_credentialsObject



62
63
64
65
66
67
68
# File 'lib/twitter_backup/config.rb', line 62

def empty_credentials
  new_credentials = { :consumer_key => "",
                      :consumer_secret => "",
                      :oauth_token => "",
                      :oauth_token_secret => ""}
  save_credentials new_credentials
end

.loadObject



24
25
26
27
28
29
30
31
32
# File 'lib/twitter_backup/config.rb', line 24

def load
  TwitterBackup.prepare_file config_file
  yaml = YAML::load_file( config_file )
  @@options = if yaml.is_a? Hash
    CONFIG_DEFAULTS.merge(yaml)
  else
    CONFIG_DEFAULTS
  end
end

.optionsObject



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

def options
  @@options ||= load
end

.passed_optsObject



127
128
129
130
131
132
133
134
# File 'lib/twitter_backup/config.rb', line 127

def passed_opts 
  @@opts ||= Slop.parse(:help => true) do
    on :v, :verbose, 'Enable verbose mode'
    on :f, :force, "Try to download tweets, even it seems useless"
    on :s, :seed, "Try to download tweets older then the oldest one you have"
    on :c, :config, "Config file. Default: #{CONFIG_FILE}", :argument => true
  end
end

.path_to_backup_defined?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/twitter_backup/config.rb', line 48

def path_to_backup_defined?
  options[:backup_file].present?
end

.save(options) ⇒ Object



34
35
36
37
38
# File 'lib/twitter_backup/config.rb', line 34

def save options
  @@options = options
  TwitterBackup.prepare_file config_file
  File.open( config_file, "w" ){|f| YAML::dump( options, f )}
end

.save_backup_file(file) ⇒ Object



52
53
54
55
# File 'lib/twitter_backup/config.rb', line 52

def save_backup_file file
  options[:backup_file] = file
  save options
end

.save_credentials(new_credentials) ⇒ Object



57
58
59
60
# File 'lib/twitter_backup/config.rb', line 57

def save_credentials new_credentials
  options[:credentials] = options[:credentials].merge(new_credentials)
  save options
end

.userObject



119
120
121
# File 'lib/twitter_backup/config.rb', line 119

def user
  @@user ||= verify_credentials
end

.verify_credentialsObject



113
114
115
116
117
# File 'lib/twitter_backup/config.rb', line 113

def verify_credentials
  say ".... checking validity of credentials" if passed_opts.verbose?
  configure_twitter_gem unless @@twitter_gem_configured
  @@user = Twitter.verify_credentials(:skip_status => true, :include_entities => false)
end