EdConnell/stats — pull request #1 by gphil (2013-02-20 16:34:26+00:00)
Title: how to scrape in a rails app, and show the results on a page

Hey, so I just cloned your project, and made a few changes to demonstrate how you can scrape a site and display the data on a page.

First, I added nokogiri to your Gemfile, so you can use it from within your app. I also upgraded you to Rails 3.2.12 since there were security vulnerabilities in 3.2.8. Just run "bundle install" and it will get this stuff set up for you. I also had to require "open-uri" in your config/application.rb since your scraper needs that as well. config/application.rb is just where I put requires that aren't in my Gemfile. (You don't need it in your Gemfile because it is built into Ruby, but you have to require it somewhere to use it.)

Then I deleted the default Rails page, and added a new route and controller so that when you visit http://your_site/ you get the page in app/view/main/index.html.erb.

Within that controller I make a call to a new class in app/models/world_of_tanks.rb which runs the scraper code you route and returns the result. It's just a regular ruby class so it's not persisted in the database like ActiveRecord models are.

So, now every time you request http://your_site/ it hits the World of Tanks stats page, calculates the efficiency rating and displays it.

Hope that helps, and let me know if you have any questions.