Home arrow Ruby-on-Rails arrow Page 4 - Tracking the News with Google News
RUBY-ON-RAILS

Tracking the News with Google News


In this article, you'll create a graphical report from Google News RSS data, using a handy utility called FeedTools and a plug-in called CSS Graphs Helper. This article is excerpted from chapter 11 of the book Practical Reporting with Ruby and Rails, written by David Berube (Apress; ISBN: 1590599330).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
April 15, 2010
TABLE OF CONTENTS:
  1. · Tracking the News with Google News
  2. · Company News Coverage Reporting
  3. · Dissecting the Code
  4. · Creating the News Tracker Report Application
  5. · Dissecting the Code

print this article
SEARCH DEVARTICLES

Tracking the News with Google News - Creating the News Tracker Report Application
(Page 4 of 5 )

Next, let's take a look at creating a Rails application that shows the report. As noted at the beginning of the chapter, you'll use the CSS Graphs Helper plug-in to create the graphs. This provides a simple way to graph data, by creating HTML graphs using CSS. In Chapter 7, you generated custom HTML and CSS graphs using Markaby, which is the most flexible approach, but CSS Graphs Helper does this automatically.

In this example, you'll use the CSS Graphs Helper's complex_bar_graph method to create a thermometer-like chart. Also available are a bar_graph method, which creates vertical charts, and a
horizontal_bar_chart method, which creates horizontal bar charts . You can get more information about CSS Graphs Helper and its various charts at http://nubyonrails.com/pages/css_graphs.


Note  Unfortunately, CSS Graphs Helper currently supports only one chart per page. And, while it's extremely easy to use, it has a limited range of chart types. If you're looking for more complicated charts or for many charts per page, consider using the Gruff graphing library (which was created by the same person who created CSS Graphs Helper). You can find examples of Gruff in Chapters 3, 4, and 10, and at the Gruff home page: http://nubyonrails.com/pages/gruff.


Before we start, if you haven't already installed the Rails gem, do so now:

gem install rails

Now create the framework of your Rails application as follows:

rails newstracker

--------------------------------------------
  create  app/controllers
  create  app/helpers
  create  app/models
  create  app/views/layouts
  create  config/environments
  create  components
  create  db
  . . .
  create  log/server.log
  create  log/production.log
  create  log/development.log
  create  log/test.log
--------------------------------------------

Next, create a report controller, as follows:

cd newstracker
ruby script/generate controller Reporter

--------------------------------------------
  exists  app/controllers/
  exists  app/helpers/
  create  app/views/reporter
  exists  test/functional/
  create app/controllers/reporter_controller.rb
  create test/functional/reporter_controller_test.rb
  create app/helpers/reporter_helper.rb
--------------------------------------------

This controller covers a single action, which renders the report.

Your next step is to create the single model, which represents your stories table, as follows:

ruby script/generate model Story

--------------------------------------------
  exists  app/models/
  exists  test/unit/
  exists  test/fixtures/
  create  app/models/story.rb
  create  test/unit/story_test.rb
  create  test/fixtures/stories.yml
  create  db/migrate
  create  db/migrate/001_create_stories.rb
--------------------------------------------

Next, install CSS Graphs Helper by using the following command :

script/plugin install http://topfunky.net/svn/plugins/css_graphs 

--------------------------------------------
+ ./css_graphs/History.txt 
+ ./css_graphs/MIT-LICENSE 
+ ./css_graphs/Manifest.txt 
+ ./css_graphs/README.txt 
+ ./css_graphs/Rakefile 
+ ./css_graphs/about.yml
+ ./css_graphs/generators/css_graphs/ css_graphs_generator.rb 
+ ./css_graphs/generators/css_graphs/ templates/colorbar.jpg 
+ ./css_graphs/generators/css_graphs/ templates/g_colorbar.jpg 
+ ./css_graphs/generators/css_graphs/ templates/g_colorbar2.jpg 
+ ./css_graphs/generators/css_graphs/ templates/g_marker.gif 
+ ./css_graphs/images/colorbar.jpg 
+ ./css_graphs/init.rb
+ ./css_graphs/lib/css_graphs.rb 
+ ./css_graphs/test/test_css_graphs.rb
--------------------------------------------

After you install CSS Graphs Helper, you need to copy the files it uses into your public/images directory:

ruby ./script/generate css_graphs

--------------------------------------------
  create  public/images/css_graphs
  create public/images/css_graphs/colorbar.jpg
create public/images/css_graphs/g_colorbar.jpg
  create public/images/css_graphs/g_colorbar2.jpg
  create public/images/css_graphs/g_marker.gif
--------------------------------------------

Now, let's start filling in the code for the application. Add the code in Listing 11-3 to the Reporter controller.

Listing 11-3. Reporter Controller (app/controller/reporter_controller.rb)

class ReporterController < ApplicationController
  def index
    custom_sql = "SELECT published_at_formatted,
                         count(*) as count

                    FROM (SELECT DATE_FORMAT(published_at,
                                 '%m-%d-%y')
                                 AS published_at_formatted
                            FROM stories) AS grouped_table
                   GROUP
                      BY published_at_formatted
                   ;"

    @stories = Story.find_by_sql(custom_sql)
  end
end

Save this file as app/controller/reporter_controller.rb.

Next, create the single view, as shown in Listing 11-4.

Listing 11-4. Reporter View (app/views/reporter/index.rhtml)

<h1>Our Company In The Media </h1>

<p>
<%
  story_count_max = @stories.max { |a,b| a.count.to_i <=> b.count.to_i }.count.to_f

  story_data = @stories.map{|x| ["#{x.published_at_formatted} (#{x.count})",
                             (x.count.to_f / story_count_max)*100]}

%>
  <%= complex_bar_graph story_data  %>
</p>

Save this view as app/views/reporter/index.rhtml.

You don't need to add any code into your model file. The defaults are fine, since the model doesn't need any relationships or other customizations. So, your application is all set.

At this point, you'll need to edit the
config/database.yml file with your database connection parameters.

You can run this script using the following command:

ruby script/server

Now open a web browser and browse to http://localhost:3000/reporter/. You should see a screen similar to Figure 11-1.


Figure 11-1.  News tracker report

Next, let's take a look at this example line by line.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials