-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.rb
84 lines (71 loc) · 1.52 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'json'
require 'csv'
INPUT_FILE = 'sample.json'
OUTPUT_FILE = 'output.csv'
class BoundaryPoint
attr_accessor :input_file
attr_accessor :room_index
attr_accessor :json
def initialize(input_file)
@json = nil
@room_index = 1
@input_file = input_file
remove_output
load_json
end
def load_json
file = File.open @input_file
@json = JSON.load file.read
end
def remove_output
begin
File.unlink OUTPUT_FILE
rescue
end
end
def generate
search_and_generae_csv @json
end
def search_and_generae_csv(obj)
case obj
when Array
obj.each do |array|
search_and_generae_csv array
end
when Hash
obj.each do |key, value|
if key == 'rooms'
value.each do |room|
append_csv room
end
else
search_and_generae_csv value
end
end
end
end
def append_csv(room)
CSV.open OUTPUT_FILE, 'a' do |csv|
boundary_points = room['properties']['boundaryPoints']
if boundary_points.length.zero?
csv << [
room['properties']['name'],
@room_index
]
else
boundary_points.each do |boundary_point|
csv << [
room['properties']['name'],
@room_index,
boundary_point['X'],
boundary_point['Y'],
boundary_point['Z']
]
end
end
end
@room_index += 1
end
end
boundary_point = BoundaryPoint.new INPUT_FILE
boundary_point.generate