-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimulator.rb
58 lines (45 loc) · 1020 Bytes
/
simulator.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
require "net/http"
require "securerandom"
require_relative "./api"
class Simulator
def initialize(port:)
self.port = port
end
def run
loop do
run_once
duration = rand * 2
$stdout.puts "Sleeping for #{duration}"
sleep(duration)
end
end
def run_once
http = Net::HTTP.new("localhost", port)
request = Net::HTTP::Post.new("/rides")
request.set_form_data({
"distance" => rand * (MAX_DISTANCE - MIN_DISTANCE) + MIN_DISTANCE
})
response = http.request(request)
$stdout.puts "Response: status=#{response.code} body=#{response.body}"
end
#
# private
#
MAX_DISTANCE = 1000.0
private_constant :MAX_DISTANCE
MIN_DISTANCE = 5.0
private_constant :MIN_DISTANCE
attr_accessor :port
end
#
# run
#
if __FILE__ == $0
# so output appears in Forego
$stderr.sync = true
$stdout.sync = true
port = ENV["API_PORT"] || abort("need API_PORT")
# wait a moment for the API to come up
sleep(3)
Simulator.new(port: port).run
end