-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_store.rb
104 lines (94 loc) · 2.58 KB
/
tweet_store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'json'
require 'redis'
require File.join(File.dirname(__FILE__), 'tweet')
class TweetStore
NUM_TWEETS = 40
TRIM_THRESHOLD = 200
def initialize
if ENV["REDISTOGO_URL"]
uri = URI.parse(ENV["REDISTOGO_URL"])
@db = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
else
@db = Redis.new
end
@power_trim_count = 0
@public_trim_count = 0
@function_trim_count = 0
@players = []
@db.lrange('player', 0, 99).each{|id| @players << id.to_i}
#@players = [53945780, 14060355, 15363578] #lilianelijn, andistuder, richardwilding
@croupiers = [599169181, 292503547] # @pgotest and @powergameonline
end
def tweets(key='public', limit=15, since=0)
@db.lrange(key, 0, limit - 1).collect {|t|
Tweet.new(JSON.parse(t))
}.reject {|t| t.received_at <= since}
end
# @param [Object] data
def push(data)
if @croupiers.include?(data["userid"])
@db.lpush('function', data.to_json)
@function_trim_count += 1
if @function_trim_count > TRIM_THRESHOLD
@db.ltrim('function', 0, NUM_TWEETS)
@function_trim_count = NUM_TWEETS
end
else
if @players.include?(data["userid"])
@db.lpush('power', data.to_json)
@power_trim_count += 1
if @power_trim_count > TRIM_THRESHOLD
@db.ltrim('power', 0, NUM_TWEETS)
@power_trim_count = NUM_TWEETS
end
end
@db.lpush('public', data.to_json)
@public_trim_count += 1
if @public_trim_count > TRIM_THRESHOLD
@db.ltrim('public', 0, NUM_TWEETS)
@public_trim_count = NUM_TWEETS
end
end
end
def get_croupier_tweet
JSON.parse(@db.lrange('function',0,0)[0])
end
def get_tweet_data(key="power", limit=15, since=0)
@db.lrange(key, 0, limit - 1).reject {|t|
JSON.parse(t)["received_at"] <= since
}.reverse
#TODO maybe write to parse proper?
end
def write_errors(message)
@db.lpush("error", message.to_json)
end
def get_errors
no_of_errors = @db.llen('error')
@db.lrange('error', 0, no_of_errors - 1).collect{|e|
JSON.parse(e)}
#TODO join with above?
end
def delete_errors
no_of_errors = @db.llen('error')
@errors = []
@db.llen('error').times do
@errors << JSON.parse(@db.lpop('error'))
end
@errors
#TODO join with above?
end
def set_players(new_players)
@db.del('player')
@players = []
new_players.each do |player|
@db.lpush('player', player)
@players << player
end
end
def get_players
@players
end
def clear_all
@db.flushall
end
end