class Sensor<
#Purpose:
# => 1) Recieves text containing information about 1 sensor
# => 2) Parses the info, and stores it as properties of the obj
# through the Struct object
# => 3) Has method to push the data into a .csv file
#
Struct.new(:s_id,
:s_name,
:s_dataSourceId,
:s_tmcId,
:s_type,
:s_status,
:s_speed,
:s_volume,
:s_longVolume,
:s_occupancy,
:s_lastUpdated)
def fill_struct(s_text)
@arr = Array.new(11)
@arr[0]=/(?<=id: ").*?(?=")/
@arr[1]=/(?<=name: ").*?(?=")/
@arr[2]=/(?<=dataSourceId: ").*?(?=")/
@arr[3]=/(?<=tmcID: ").*?(?=")/
@arr[4]=/(?<=type: ").*?(?=")/
@arr[5]=/(?<=status: ").*?(?=")/
@arr[6]=/(?<=speed: ).*?(?=,)/
@arr[7]=/(?<=volume: ).*?(?=,)/
@arr[8]=/(?<=longVolume: ).*?(?=,)/
@arr[9]=/(?<=occupancy: ).*?(?=,)/
@arr[10]=/(?<=lastUpdated: new Date\(').*?(?=')/
self.s_id = s_text.scan(@arr[0])[0]
self.s_name = s_text.scan(@arr[1])[0]
self.s_dataSourceId = s_text.scan(@arr[2])[0]
self.s_tmcId = s_text.scan(@arr[3])[0]
self.s_type = s_text.scan(@arr[4])[0]
self.s_status = s_text.scan(@arr[5])[0]
self.s_speed = s_text.scan(@arr[6])[0].to_i
self.s_volume = s_text.scan(@arr[7])[0].to_i
self.s_longVolume = s_text.scan(@arr[8])[0].to_i
self.s_occupancy = s_text.scan(@arr[9])[0].to_i
self.s_lastUpdated = s_text.scan(@arr[10])[0]
end
def mysql_out(db_mysql)
end
def csv_out(f_output)
self.each do |i|
f_output.print i, ","
end
f_output.print "\n"
end
end
#Objective -
# =>
# =>
# =>
# =>
def collect_stats(s_html, s_path)
rgx_pass_1 = /id.*?}/m #1. Create a RegExp to get individual
# results
s_file = open(s_html).read #2. Pass web site into s_file variable
b = [] #3. Create an empty array to hold
# sensor objects
s_file.scan(rgx_pass_1).each do |i| #4. Scan String for RegExp matches
# a) Pass each result
a = Sensor.new #5. Create new Sensor object
a.fill_struct(i) #6. Invoke the object's fill method
# a) with the result
b<<a #7. Store the Object in the array
end
the_file = open(s_path,'a')
b.each do |x|
x.csv_out(the_file)
end
end |