Skip to content

Commit 0f4ff71

Browse files
author
Patrick Crosby
committed
working on threaded version
1 parent d6024bb commit 0f4ff71

File tree

1 file changed

+68
-13
lines changed

1 file changed

+68
-13
lines changed

lib/stathat.rb

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,101 @@
11
require 'net/http'
22
require 'uri'
33
require 'json'
4+
require 'thread'
45

56
module StatHat
67
class API
78
CLASSIC_VALUE_URL = "http://api.stathat.com/v"
89
CLASSIC_COUNT_URL = "http://api.stathat.com/c"
910
EZ_URL = "http://api.stathat.com/ez"
1011

11-
def self.post_value(stat_key, user_key, value)
12+
REPORTER = self.new()
13+
14+
def initialize()
15+
@Q = Queue.new
16+
run_pool()
17+
end
18+
19+
def enqueue(url, args)
20+
point = {:url => url, :args => args}
21+
@Q << point
22+
end
23+
24+
def run_pool()
25+
@running = true
26+
@pool = []
27+
5.times do |i|
28+
pool[i] = Thread.new do
29+
while @running do
30+
point = @Q.pop
31+
# XXX check for error?
32+
send_to_stathat(point[:url], point[:args])
33+
end
34+
end
35+
end
36+
end
37+
38+
def stop_pool()
39+
@running = false
40+
@pool.each do |th|
41+
th.join
42+
end
43+
end
44+
45+
def finish()
46+
stop_pool
47+
# XXX serialize queue?
48+
end
49+
50+
def send_to_stathat(url, args)
51+
uri = URI.parse(url)
52+
uri.query = URI.encode_www_form(args)
53+
resp = Net::HTTP.get(uri)
54+
return Response.new(resp)
55+
end
56+
57+
def post_value(stat_key, user_key, value)
1258
args = { :key => stat_key,
1359
:ukey => user_key,
1460
:value => value }
15-
return self.send_to_stathat(CLASSIC_VALUE_URL, args)
61+
enqueue(CLASSIC_VALUE_URL, args)
1662
end
1763

18-
def self.post_count(stat_key, user_key, count)
64+
def self.post_value(stat_key, user_key, value)
65+
REPORTER.post_value(stat_key, user_key, value)
66+
end
67+
68+
def post_count(stat_key, user_key, count)
1969
args = { :key => stat_key,
2070
:ukey => user_key,
2171
:count => count }
22-
return self.send_to_stathat(CLASSIC_COUNT_URL, args)
72+
enqueue(CLASSIC_COUNT_URL, args)
2373
end
2474

25-
def self.ez_post_value(stat_name, ezkey, value)
75+
def self.post_count(stat_key, user_key, count)
76+
REPORTER.post_count(stat_key, user_key, count)
77+
end
78+
79+
def ez_post_value(stat_name, ezkey, value)
2680
args = { :stat => stat_name,
2781
:ezkey => ezkey,
2882
:value => value }
29-
return self.send_to_stathat(EZ_URL, args)
83+
enqueue(EZ_URL, args)
3084
end
3185

32-
def self.ez_post_count(stat_name, ezkey, count)
86+
def self.ez_post_value(stat_name, ezkey, value)
87+
REPORTER.ez_post_value(stat_name, ezkey, value)
88+
end
89+
90+
def ez_post_count(stat_name, ezkey, count)
3391
args = { :stat => stat_name,
3492
:ezkey => ezkey,
3593
:count => count }
36-
return self.send_to_stathat(EZ_URL, args)
94+
enqueue(EZ_URL, args)
3795
end
3896

39-
def self.send_to_stathat(url, args)
40-
uri = URI.parse(url)
41-
uri.query = URI.encode_www_form(args)
42-
resp = Net::HTTP.get(uri)
43-
return Response.new(resp)
97+
def self.ez_post_count(stat_name, ezkey, count)
98+
REPORTER.ez_post_count(stat_name, ezkey, count)
4499
end
45100
end
46101

0 commit comments

Comments
 (0)