Question : Ruby's Page - Open speed

I'm trying to read this site
1:
'http://dfwtraffic.dot.state.tx.us/DalTrans/GetFile.aspx?FileName=MapBurnerOutput/TrafficDetectors.js'
into a string so I can play with it. When I open it in a browser, it loads up pretty quick (like 5 seconds tops);

but when I do this:

1:
s = open(site)


it takes like 20. Wondering if there's not a way to make it go faster?

below is the full code I'm playing wiht,

thx,
-JW


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:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
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

Answer : Ruby's Page - Open speed

The open call does seem too expensive.

Good thinking - it does resemble json. I would look at <http://flori.github.com/json/doc/index.html> for how to read the thing.

require 'json'

...

JSON.load(source)
Random Solutions  
 
programming4us programming4us