Module: ARSupport

Defined in:
lib/rbitter/records.rb

Constant Summary collapse

SCHEME_VERSION =
20150504
SCHEME =
{
  :marker => :integer, # 0 normal, 1 begin 2 halt
  :marker_msg => :string,
  :userid => :integer,
  :username => :string,
  :tweetid => :integer,
  :replyto => :integer,
  :tweet => :text, # with url unpacked
  :date => :datetime,
  :rt_count => :integer,
  :fav_count => :integer
}

Class Method Summary collapse

Class Method Details

.any_to_datestring(obj) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/rbitter/records.rb', line 106

def any_to_datestring(obj)
  if obj.is_a?(String)
    # try to parse it
    DateTime.parse(obj).strftime("%Y-%m-%d %H:%M:%S")
  elsif obj.is_a?(DateTime) or obj.is_a?(Time)
    obj.strftime("%Y-%m-%d %H:%M:%S")
  else
    raise ArgumentError.new("Can\'t automatically extract DateTime info")
  end
end

.connect_databaseObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rbitter/records.rb', line 31

def connect_database
  if Rbitter['activerecord'] == 'sqlite3'
    warn "Warning: If you enable XMLRPC access, using sqlite is not recommended."
    warn "Warning: Random crash can happen because of concurrency."
    
    if RUBY_PLATFORM == 'java'
      require "jdbc/sqlite3"
      Jdbc::SQLite3.load_driver
      ActiveRecord::Base.establish_connection(
        adapter: 'jdbcsqlite3',
        database: Rbitter['sqlite3']['dbfile'],
        timeout: 10000) # Long timeout for slow computer
    else
      ActiveRecord::Base.establish_connection(
        adapter: 'sqlite3',
        database: Rbitter['sqlite3']['dbfile'],
        timeout: 10000) # Long timeout for slow computer
    end
  elsif Rbitter['activerecord'] == 'mysql2'
    Jdbc::MySQL.load_driver if RUBY_PLATFORM == 'java'
    
    ActiveRecord::Base.establish_connection(
      adapter: (RUBY_PLATFORM == 'java' ? 'jdbcmysql' : 'mysql2'),
      host: Rbitter['mysql2']['host'],
      port: Rbitter['mysql2']['port'],
      database: Rbitter['mysql2']['dbname'],
      username: Rbitter['mysql2']['username'],
      password: Rbitter['mysql2']['password'],
      encoding: "utf8mb4",
      collation: "utf8mb4_unicode_ci")
  else
    raise RuntimeException.new("Unknown configuration value. 'activerecord' value should be sqlite3 or mysql2.")
  end
end

.disconnect_databaseObject



66
67
68
69
70
# File 'lib/rbitter/records.rb', line 66

def disconnect_database
  if ActiveRecord::Base.connected?
    ActiveRecord::Base.connection.close
  end
end

.export_to_csv(csvfile) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/rbitter/records.rb', line 117

def export_to_csv(csvfile)
  open(csvfile, 'w') { |f|
    f.write("marker,marker_msg,userid,username,tweetid,replyto,tweet,date,rt_count,fav_count")
    f.write("\n")
    Rbitter::Record.find_each { |t|
      f.write("#{t.marker},#{t.marker_msg},#{t.userid},#{t.username},#{t.tweetid},")
      f.write("#{t.replyto},#{t.tweet},#{t.date},#{t.rt_count},#{t.fav_count}\n")
    }
  }
end

.prepare(option_string = "") ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rbitter/records.rb', line 81

def prepare option_string=""
  ActiveRecord::Schema.define(version: SCHEME_VERSION) {
    # MySQL specific option_string:
    # utf8mb4 -> supporting UTF-8 4-byte characters (i.e. Emoji)
    create_table(:records, { :options => option_string }) do |t|
      SCHEME.each_key { |column|
        case SCHEME[column]
        when :string
          t.string column
        when :integer
          t.integer column, :limit => 8
        when :datetime
          t.datetime column
        when :text
          t.text column
        else
          puts "Unexpected column type '#{SCHEME[column]}' of #{column}"
        end
      }
    end

    add_index :records, :tweetid
  }
end

.prepared?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rbitter/records.rb', line 27

def prepared?
  ActiveRecord::Base.connection.table_exists?(:records)
end

.update_database_schemeObject



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

def update_database_scheme
  current_version = ActiveRecord::Migrator.current_version
  if current_version < SCHEME_VERSION
    warn "[records] Your ActiveRecord scheme is outdated."
    warn "[records] Migrate... #{current_version} => #{SCHEME_VERSION}"
    ActiveRecord::Migrator.migrate(File.expand_path("../records_migrate", __FILE__), SCHEME_VERSION)
  end
end