Created
December 4, 2009 19:37
-
-
Save KeithHanson/249283 to your computer and use it in GitHub Desktop.
Demo App for Fremont Consulting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'sinatra/base' | |
| require 'mongomapper' | |
| require 'faker' | |
| MongoMapper.database = 'demo' | |
| # MongoMapper Model with fields defined. | |
| class RandomRecord | |
| include MongoMapper::Document | |
| key :name, String | |
| key :timestamp, Float | |
| key :random_number, Float | |
| end | |
| class FremontConsultingDemo < Sinatra::Base | |
| get "/" do | |
| <<-OUTPUT | |
| Please Select a Link: <br /><br /> | |
| <ol> | |
| <li><a href="/generate">Generate Data (and clear current data)</a></li> | |
| <li><a href="/average">Average all random numbers</a></li> | |
| <li><a href="/sum">Sum all random numbers</a></li> | |
| <li><a href="/update_timestamps">Iterate over all records and update the timestamp</a></li> | |
| <li><a href="http://gist.github.com/249283">See the code</a> | |
| </ol> | |
| OUTPUT | |
| end | |
| get "/generate" do | |
| # Empty current database | |
| RandomRecord.collection.clear | |
| # Loop 100 times and collect the return string of each iteration | |
| 100.times.collect do | |
| # Create a new record with a fake name, current time, and a random float | |
| record = RandomRecord.create( | |
| {:name => Faker::Name.name, | |
| :timestamp => Time.now.to_f, | |
| :random_number => rand * 1000} | |
| ) | |
| # Output the record in a nice format | |
| "Record Generated: #{record.to_json}" | |
| end.join("<br /><br />") | |
| end | |
| get "/average" do | |
| # Retrieve only the random_number fields and collect the data into an array | |
| "The Average of all the random_numbers in the database is: " + | |
| (RandomRecord.all(:fields => [:random_number]).collect do |record| | |
| record.random_number | |
| # inject takes an initial sum, and then sets the sum to whatever the current | |
| # iteration returns, thus sum and average is accomplished easily | |
| end.inject(0.0) {|sum, number| sum + number} / RandomRecord.count).to_s | |
| end | |
| get "/sum" do | |
| # Retrieve only the random_number fields and collect the data into an array | |
| "The Sum of all the random_numbers in the database is: " + | |
| RandomRecord.all(:fields => [:random_number]).collect do |record| | |
| record.random_number | |
| # Inject to get the sum easily | |
| end.inject(0.0) {|sum, number| sum + number}.to_s | |
| end | |
| get "/update_timestamps" do | |
| RandomRecord.all.collect do |record| | |
| record.timestamp = Time.now.to_f | |
| record.save | |
| "Record Updated: #{record.to_json}" | |
| end.join("<br /><br />") | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment