|
1 | 1 | require 'net/http'
|
2 | 2 | require 'uri'
|
3 | 3 | require 'json'
|
| 4 | +require 'thread' |
4 | 5 |
|
5 | 6 | module StatHat
|
6 | 7 | class API
|
7 | 8 | CLASSIC_VALUE_URL = "http://api.stathat.com/v"
|
8 | 9 | CLASSIC_COUNT_URL = "http://api.stathat.com/c"
|
9 | 10 | EZ_URL = "http://api.stathat.com/ez"
|
10 | 11 |
|
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) |
12 | 58 | args = { :key => stat_key,
|
13 | 59 | :ukey => user_key,
|
14 | 60 | :value => value }
|
15 |
| - return self.send_to_stathat(CLASSIC_VALUE_URL, args) |
| 61 | + enqueue(CLASSIC_VALUE_URL, args) |
16 | 62 | end
|
17 | 63 |
|
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) |
19 | 69 | args = { :key => stat_key,
|
20 | 70 | :ukey => user_key,
|
21 | 71 | :count => count }
|
22 |
| - return self.send_to_stathat(CLASSIC_COUNT_URL, args) |
| 72 | + enqueue(CLASSIC_COUNT_URL, args) |
23 | 73 | end
|
24 | 74 |
|
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) |
26 | 80 | args = { :stat => stat_name,
|
27 | 81 | :ezkey => ezkey,
|
28 | 82 | :value => value }
|
29 |
| - return self.send_to_stathat(EZ_URL, args) |
| 83 | + enqueue(EZ_URL, args) |
30 | 84 | end
|
31 | 85 |
|
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) |
33 | 91 | args = { :stat => stat_name,
|
34 | 92 | :ezkey => ezkey,
|
35 | 93 | :count => count }
|
36 |
| - return self.send_to_stathat(EZ_URL, args) |
| 94 | + enqueue(EZ_URL, args) |
37 | 95 | end
|
38 | 96 |
|
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) |
44 | 99 | end
|
45 | 100 | end
|
46 | 101 |
|
|
0 commit comments