module Github module Archive class Connections class << self SETTINGS = {} def connect_mysql ::ActiveRecord::Base.establish_connection( adapter: 'mysql', server: SETTINGS['GHA']['MYSQL']['SERVER'], username: SETTINGS['GHA']['MYSQL']['USERNAME'], password: SETTINGS['GHA']['MYSQL']['PASSWORD'], database: SETTINGS['GHA']['MYSQL']['DATABASE'] ) end def config_mysql(server, user, password, database) SETTINGS['GHA']['MYSQL']['SERVER'] = server SETTINGS['GHA']['MYSQL']['USERNAME'] = user SETTINGS['GHA']['MYSQL']['PASSWORD'] = password SETTINGS['GHA']['MYSQL']['DATABASE'] = database end def connect_redis if SETTINGS['GHA']['REDIS']['PASSWORD'].nil? ::Resque.redis = Redis.new(:host => SETTINGS['GHA']['REDIS']['SERVER'], :port => SETTINGS['GHA']['REDIS']['PORT'], :thread_safe => true) else ::Resque.redis = Redis.new(:host => SETTINGS['GHA']['REDIS']['SERVER'], :port => SETTINGS['GHA']['REDIS']['PORT'], :password => SETTINGS['GHA']['REDIS']['PASSWORD'], :thread_safe => true) end end def config_redis(server, port, password) SETTINGS['GHA']['REDIS']['SERVER'] = server SETTINGS['GHA']['REDIS']['PORT'] = port SETTINGS['GHA']['REDIS']['PASSWORD'] = password end def init_settings SETTINGS['GHA'] ||= {} SETTINGS['GHA']['MYSQL'] ||= {} SETTINGS['GHA']['MYSQL']['SERVER'] ||= 'localhost' SETTINGS['GHA']['MYSQL']['USERNAME'] ||= 'root' SETTINGS['GHA']['MYSQL']['PASSWORD'] ||= nil SETTINGS['GHA']['MYSQL']['DATABASE'] ||= 'github_archive' SETTINGS['GHA']['REDIS'] ||= {} SETTINGS['GHA']['REDIS']['SERVER'] ||= 'localhost' SETTINGS['GHA']['REDIS']['PASSWORD'] ||= nil SETTINGS['GHA']['REDIS']['PORT'] ||= '6379' end def read_settings if File.exist?(setting_path) settings = YAML::load_file setting_path config_mysql settings['MYSQL']['SERVER'], settings['MYSQL']['USERNAME'], settings['MYSQL']['PASSWORD'], settings['MYSQL']['DATABASE'] config_redis settings['REDIS']['SERVER'], settings['REDIS']['PORT'], settings['REDIS']['PASSWORD'] end end def write_settings File.open(setting_path, "w") do |file| file.write SETTINGS['GHA'].to_yaml end end private def setting_path "#{File.expand_path('~')}/.github-archive" end end end end end