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
- .any_to_datestring(obj) ⇒ Object
- .connect_database ⇒ Object
- .export_to_csv(csvfile) ⇒ Object
- .prepare(option_string = "") ⇒ Object
- .prepared? ⇒ Boolean
- .update_database_scheme ⇒ Object
Class Method Details
.any_to_datestring(obj) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rbitter/records.rb', line 100 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_database ⇒ Object
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 |
.export_to_csv(csvfile) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rbitter/records.rb', line 111 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rbitter/records.rb', line 75 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
27 28 29 |
# File 'lib/rbitter/records.rb', line 27 def prepared? ActiveRecord::Base.connection.table_exists?(:records) end |
.update_database_scheme ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/rbitter/records.rb', line 66 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.("../records_migrate", __FILE__), SCHEME_VERSION) end end |