2012-04-05 1 views
0

2次元配列にチャッキングする前に、gifとURLをHTMLテーブルの有用なデータに置き換える助けをコミュニティから得ました実際に必要なのは、テーブルの各行をactiverecordエントリのハッシュとして格納することだと思います。ここでActiveRecordの使用を開始しました - HTMLテーブルを保存してハッシュに変換したい

は、ヘッダーとサンプルデータの最初の行です:

html2 = <<TABLE2 
<table class="status"> 
<caption class="status">Drive status</caption> 
<tr class="status"> 
<th class="status"></th> 
<th class="status">Drive</th> 
<th class="status">State</th> 
<th class="status">Health</th> 
<th class="status">Make/Model</th> 
<th class="status">Speed</th> 
<th class="status">Serial</th> 
<th class="status">Firmware</th> 
<th class="status"><a href="/cgi-bin/status_dylan?cont=0&amp;dylan=0&amp;display=1">Sectors</a></th> 
<th class="status">Temp</th> 
<th class="status"> </th> 
</tr> 
<tr class="status"> 
<td class="status"><img border="0" src="/tick_green.gif"></td> 
<td class="status">0</td> 
<td class="status">Ready</td> 
<td class="status"><a href="/cgi-bin/status_drive?cont=0&amp;dylan=0&amp;drive=0"><img border="0" src="/bar10.gif"></a></td> 
<td class="status">SEAGATE ST3146807FC</td> 
<td class="status">10000 RPM</td> 
<td class="status">3HY61E1B</td> 
<td class="status">XR12</td> 
<td class="status">286749488</td> 
<td class="status"> 29.0&#176;C</td> 
<td class="status" style="background-color: #fefe00">&#160; 
</td> 
</tr> 

clean_table2 = [] 
    table2.css('tr').each do |tr| 
    clean_row = [] 
    tr.css('td').each do |td| 
     #for each cell, look for img tags, and replace the images with text as appropriate, then strip the html 
     img = td.at('img') 
     clean_row.push case 
     when img && img[:src][/bar(\d+)\.gif/] then 'Health: '+$1 
     when img && img[:src][/tick_green/] then 'Healthy' 
     when img && img[:src][/cross_red/] then 'Failed' 
     when img && img[:src][/caution/] then 'Caution' 
     else td.text.strip 
     end 

    end 
    clean_table2.push clean_row 
    #puts clean_row[5] 
    end 
    puts "\n" 
#puts clean_table.join("\n") 
clean_table2.each {|x| 
    puts "#{x}" 
} 

ここでは重要でないすべてのものを削除し、合理的なテキストで「非役立つ」GIFを交換するコードです - =が、ハッシュは、私はARENを作成しています私が望むように便利だから、テーブルヘッダーをキーとして使ってハッシュを作成することにしました。 次に、サーバーのシリアル番号とアドレスを入力して、activerecordエントリに入力して、デルタの比較と表示ができますレコードのインスタンス(たとえば、ドライブの状態が10から5に低下した場合) あなたはどう思いますか? 配列を比較することはできますが、レコードの取得が高速であるため、変更があるたびに2-d配列を格納するのではなく、別個の変更のみを格納できます(これは急速に制御外になると思います)。

...あなたはおそらく推測できるように、私はあまりにもストレート私の頭の中でこれを取得しようとしている。) 多くのおかげで、わずかに書き換え スコット

答えて

0

、それはもう少し論理作ら...

table = html_page.parser.xpath('//table/caption[contains(.,"Drive")]/..') 
    #loop through each row individually (or do I want to chuck the whole thing into a nice juicy hash) 
    #Am I using this? #REMOVE 
    clean_table = Array.new 
    clean_head=[] 
    table.css('tr').each do |tr| 
    #stash WWN number, fake interface and fake address [can get, but not needed at this stage] 
    clean_row = {:wwn=>cells[0],:dyl_if=>'1',:dyl_addr=>'0'} 
    #grab headers 
    tr.css('th').each_with_index do |th,i| 
     if i == 0 
     clean_head.push "Drive Health" 
     else if i == 10 
     clean_head.push "BG Temp" 
     else clean_head.push th.text.strip 
     end 
    end 
    end 
    #each td in each tr - add index so I can add table headers as keys in hash 
    tr.css('td').each_with_index do |td, i| 
     #for each cell, look for img tags, and replace the images with text as appropriate, then strip the html 
     img = td.at('img') 

     clean_row[clean_head[i]] = case 
     when img && img[:src][/bar(\d+)\.gif/] then 'Health: '+$1 
     when img && img[:src][/tick_green/] then 'Healthy' 
     when img && img[:src][/cross_red/] then 'Failed' 
     when img && img[:src][/caution/] then 'Caution' 
    else td.text.strip 
     end 
    end 
    #Debug output - confirm nothing cocked up 
    puts clean_row 
    if clean_row.has_key?("Health") 
     Drive_Record.create(clean_row) 
     puts "Add Drive Recprd" 
    end 

end 
関連する問題