1

I'm working on mashup that scrapes a couple of sites for data. I want to scrape and cache the data on demand rather than index the entire sites.

The first time the data is fetched, the operation can be extremely slow--at least a couple of minutes.

What's the best practice for displaying pages line-by-line like this? Is there a way to display the page dynamically, showing data as it's fetched?

Related:

Community
  • 1
  • 1
Jeff Axelrod
  • 25,625
  • 29
  • 137
  • 239

2 Answers2

1

I've used jquery to allow each expensive partial to be rendered on clicking a button:

view:

  #book_forecast    
    = link_to 'See Sales Estimates'  , book_forecast_work_update_path(:work => @work),  :remote => true

book_forecast.js.erb:

$( "#book_forecast" ).html( "<%= escape_javascript( render( :partial => 'works/work_update/evaluation_forecast', :locals => { :work => @work} ) ) %>" );

work_update controller:

  def book_forecast
    @work = Work.find(params[:work])
    respond_to do | format |
        format.js
    end
  end

works/work_update/evaluation_forecast.html.haml:

# slow-loading code 

The downside is that the user has to click on a button to render each partial, but on the other hand, using jquery instead of rendering as normal means the expensive code doesn't run when the page loads.

You can also use a 'loading' icon so that the user's got something to look at whilst the heavy code runs, something like:

same view:

  #loading
    %h2
      .notice
        Loading, please wait...

js:

$(document).ready(function () {

  $('#loading')
      .hide()  
      .ajaxStart(function() {
          $(this).show();
      })
      .ajaxStop(function() {
          $(this).hide();
      })
  ;
snowangel
  • 3,299
  • 3
  • 24
  • 67
0

It looks like Rails Live Streaming is one approach. Disadvantage is that it appears to be highly web server dependent and touchy. Here's a tutorial:

http://tenderlovemaking.com/2012/07/30/is-it-live.html

class MyController < ActionController::Base
  include ActionController::Live

  def index
    100.times {
      response.stream.write "hello world\n"
    }
    response.stream.close
  end
end
Jeff Axelrod
  • 25,625
  • 29
  • 137
  • 239