forked from brandur/rocket-rides-unified
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer-sinatra.rb
68 lines (59 loc) · 1.06 KB
/
consumer-sinatra.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
require 'sinatra'
require 'json'
configure do
@@i=0
@@habcolors = []
retry_count = 0
begin
#sleep 60
habfile = File.open("HAB-consumer.txt", 'r')
rescue Errno::ENOENT => e
retry_count += 1
sleep 10
if retry_count > 5
raise
else
retry
end
end
habfile.each do |line|
line.chomp
@@habcolors << JSON.parse(line).to_json
end
end
before do
content_type :json
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']
end
# get '/stream' do
# return @@habcolors[@@i]
# @@i=@@i+1
#end
## { :hab1color => '#00FF00', :hab2color => '#00FF00' }.to_json
class Cache
@@count = 0
def self.init()
@@count = 0
end
def self.increment()
@@count = @@count + 1
end
def self.count()
return @@count
end
end
configure do
Cache::init()
end
get '/stream' do
if Cache::count() == 0
Cache::increment()
puts @@habcolors[Cache::count-1]
@@habcolors[Cache::count-1]
else
Cache::increment()
puts @@habcolors[Cache::count-1]
@@habcolors[Cache::count-1]
end
end