-
Notifications
You must be signed in to change notification settings - Fork 256
/
test_yajl.rb
47 lines (37 loc) · 914 Bytes
/
test_yajl.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
# frozen_string_literal: true
require 'yajl'
require 'socket'
Coordinate = Struct.new(:x, :y, :z)
def notify(msg)
Socket.tcp('localhost', 9001) { |s| s.puts msg }
rescue SystemCallError
# standalone usage
end
def calc(text)
jobj = Yajl::Parser.new.parse(text)
coordinates = jobj['coordinates']
len = coordinates.length
x = y = z = 0
coordinates.each do |coord|
x += coord['x']
y += coord['y']
z += coord['z']
end
Coordinate.new(x / len, y / len, z / len)
end
if __FILE__ == $PROGRAM_NAME
right = Coordinate.new(2.0, 0.5, 0.25)
['{"coordinates":[{"x":2.0,"y":0.5,"z":0.25}]}',
'{"coordinates":[{"y":0.5,"x":2.0,"z":0.25}]}'].each do |v|
left = calc(v)
if left != right
warn "#{left} != #{right}"
exit(1)
end
end
text = File.read('/tmp/1.json')
notify("Ruby (YAJL)\t#{Process.pid}")
results = calc(text)
notify('stop')
p results
end