Permalink
Browse files

Put the countries list back in and made the Add City form better. Use…

…s Google Maps to get the country name now, not user inputted.
  • Loading branch information...
1 parent d3f4c44 commit 32df3b31a1f86c71865ed96b5ea8bb8579f01c6a @paulmwatson committed Mar 20, 2009
View
24 app/controllers/cities_controller.rb
@@ -4,8 +4,9 @@ class CitiesController < ApplicationController
#caches_page :index
def index
- @cities = City.find :all, :order => 'country, name', :conditions => {:hidden => false}
+ @cities = City.find :all, :order => 'country, name', :conditions => {:hidden => false, :country => params[:country]}
@topten_cities = Mention.find :all, :group => 'city_id', :select => 'count(id) as city_mentions, city_id', :order => 'city_mentions DESC', :limit => 20
+ @location = get_location(params[:country])
@mentions = Mention.find :all, :conditions => {:mentioned_at => ("#{Date.today.year}-#{Date.today.month.to_s.rjust(2, '0')}-#{Date.today.day.to_s.rjust(2, '0')} 00:00".."#{Date.today.year}-#{Date.today.month.to_s.rjust(2, '0')}-#{Date.today.day.to_s.rjust(2, '0')} 23:59")}, :order => :mentioned_at
@@ -29,8 +30,7 @@ def new
def check
@city = City.new
@city.name = params[:city][:name]
- @city.country = params[:city][:country]
- @location = get_location(@city.name, @city.country)
+ @location = get_location(@city.name)
end
def create
@@ -45,27 +45,27 @@ def create
end
- def get_location(city, country)
+ def get_location(place)
require 'hpricot'
require 'open-uri'
- location = Location.find :first, :conditions => {:name => city}
+ #location = Location.find :first, :conditions => {:name => place}
+ location = nil
if !location
- begin
- geocode_url = "http://maps.google.com/maps/geo?q=#{city.gsub(' ', '%20')},#{country.gsub(' ', '%20')}&output=xml&oe=utf8&sensor=false&key=#{GOOGLE_API_KEY}"
- geocode_doc = open(geocode_url) { |f| Hpricot(f) }
+ geocode_url = "http://maps.google.com/maps/geo?q=#{place.gsub(' ', '%20')}&output=xml&oe=utf8&sensor=false&key=#{GOOGLE_API_KEY}"
+ puts geocode_url
+ geocode_doc = open(geocode_url) { |f| Hpricot(f) }
- coords_ele = geocode_doc.search('//coordinates')[0]
- rescue
- end
+ coords_ele = geocode_doc.search('//coordinates')[0]
if coords_ele
coords = coords_ele.inner_html
else
coords = '0,0'
end
location = Location.new
- location.name = city
+ location.name = place
+ location['country'] = geocode_doc.search('//countryname')[0].inner_html
location.latitude = coords.split(',')[1]
location.longitude = coords.split(',')[0]
location.save
View
22 app/controllers/countries_controller.rb
@@ -0,0 +1,22 @@
+class CountriesController < ApplicationController
+ layout 'base'
+ def index
+ @countries = City.find :all, :group => 'country', :order => 'country'
+ @cities = City.find :all, :order => 'country, name', :conditions => {:hidden => false}
+ @topten_cities = Mention.find :all, :group => 'city_id', :select => 'count(id) as city_mentions, city_id', :order => 'city_mentions DESC', :limit => 20
+
+ @mentions = Mention.find :all, :conditions => {:mentioned_at => ("#{Date.today.year}-#{Date.today.month.to_s.rjust(2, '0')}-#{Date.today.day.to_s.rjust(2, '0')} 00:00".."#{Date.today.year}-#{Date.today.month.to_s.rjust(2, '0')}-#{Date.today.day.to_s.rjust(2, '0')} 23:59")}, :order => :mentioned_at
+
+ @trend = Hash.new()
+ @cities.each do |city|
+ @trend[city.id] = Array.new(24,0)
+ end
+
+ @mentions.each do |mention|
+ begin
+ @trend[mention.city_id][mention.mentioned_at.hour] = @trend[mention.city_id][mention.mentioned_at.hour] + 1
+ rescue
+ end
+ end
+ end
+end
View
2 app/helpers/countries_helper.rb
@@ -0,0 +1,2 @@
+module CountriesHelper
+end
View
7 app/views/cities/check.html.erb
@@ -2,23 +2,18 @@
<h2>Add Your City</h2>
</div>
<div class="intro alt">
-<% if @city.country.gsub(' ','') == '' %>
-<p>Please enter a country and city.</p><p>&nbsp;</p>
-<% else %>
<a id="map" href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=<%= @location.latitude %>,<%= @location.longitude %>"><img src="http://maps.google.com/staticmap?center=<%= @location.latitude %>,<%= @location.longitude %>&zoom=11&size=500x213&maptype=mobile&key=<%= GOOGLE_API_KEY %>&sensor=false" alt="Map" /></a>
<% if @location.latitude == '0' %>
<p>Not even almighty Google could find your city.</p>
<% else %>
<% form_for :city, @city, :url => { :action => "create" } do |f| %>
<%= f.error_messages %>
-
<%= f.hidden_field :name %>
- <%= f.hidden_field :country %>
+ <%= f.hidden_field :country, :value => @location['country'] %>
<p style="text-align: center; padding: 25px 0 0 0;">
<%= f.submit "That's the place, add it!" %>
</p>
<% end %>
-<% end %>
</div>
<% end %>
<div class="navbar">
View
29 app/views/cities/index.html.erb
@@ -1,34 +1,17 @@
<div class="intro info">
- <p><a href="http://diycity.org"><img src="/images/diycity.png" alt="DIYCity" /></a></p>
- <p>&nbsp;</p>
- <p>Realtime disease detection for your city from messages on <em>Twitter</em> (and soon <em>Facebook</em>)</p>
- <p>&nbsp;</p>
- <p<span class="phrase">flu</span>, <span class="phrase">food poisoning</span>, <span class="phrase">headcolds</span>, <span class="phrase">chicken pox</span> &amp; <span class="phrase">sore throats</span></p>
+ <h2><%= params[:country] %></h2>
+ <p>Realtime disease detection <a href="http://diycity.org/sickcity-realtime-disease-detection-your-city">by DIYCity</a></p>
</div>
-<h1>SickCity's Ten Sickest Cities*</h1>
-<p style="font-size: 11px; text-align: center">*Some cities are still coming online</p>
-<ol class="list">
- <% i = 0 %>
- <% @topten_cities.each do |mention|%>
- <%- city = mention.city -%>
- <%- if !city.hidden %>
- <% i = i + 1%>
- <li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>"><span class="city"><span class="toptencount"><%= "%02d" % i %></span> <%= city.name %></span> <span id="city_graph_<%= city.id %>" class="tiny_graph"><% @trend[city.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
- <% end %>
- <% break if i > 9 %>
- <% end %>
-</ol>
-<h1>All Cities</h1>
<ul class="list">
<% @cities.each do |city|%>
- <li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>"><span class="city"><%= city.country %>, <%= city.name %></span> <span id="city_graph_<%= city.id %>" class="tiny_graph"><% @trend[city.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
+ <li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>"><span class="city"><%= city.name %></span> <span id="city_graph_<%= city.id %>" class="tiny_graph"><% @trend[city.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
<% end %>
</ul>
+<a id="map" href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=<%= @location.latitude %>,<%= @location.longitude %>"><img src="http://maps.google.com/staticmap?center=<%= @location.latitude %>,<%= @location.longitude %>&zoom=3&size=500x213&maptype=mobile&key=<%= GOOGLE_API_KEY %>&sensor=false" alt="Map" /></a>
<div class="navbar">
+ <a class="back home" href="/">Countries<img src="/images/bkr.png" /></a>
<a class="forward button home" href="/"><img src="/images/bk2l.png" /><img class="buttonimage" src="/images/logosmall.png" /></a>
- <a class="forward button" href="/cities/add/"><img src="/images/bk2l.png" />Add Your City</a>
- <a class="forward button" href="http://github.com/paulmwatson/sickcity"><img src="/images/bk2l.png" />Download SickCity</a>
- <a class="forward button" href="/about/"><img src="/images/bk2l.png" />About</a>
+ <h1><%= params[:country] %></h1>
</div>
<script type="text/javascript">
$('.tiny_graph').sparkline();
View
2 app/views/cities/new.html.erb
@@ -5,7 +5,7 @@
<% form_for :city, @city, :url => { :action => "check" } do |f| %>
<%= f.error_messages %>
- <p>I want to add <%= f.text_field :name, :value => 'Timbuktu' %> in <%= f.text_field :country, :value => 'Mali' %></p>
+ <p>I want to add <%= f.text_field :name, :value => 'Timbuktu' %> to SickCity.</p>
<p>
<%= f.submit "Add it" %>
</p>
View
35 app/views/countries/index.html.erb
@@ -0,0 +1,35 @@
+<div class="intro info">
+ <p><a href="http://diycity.org"><img src="/images/diycity.png" alt="DIYCity" /></a></p>
+ <p>&nbsp;</p>
+ <p>Realtime disease detection for your city from messages on <em>Twitter</em> (and soon <em>Facebook</em>)</p>
+ <p>&nbsp;</p>
+ <p<span class="phrase">flu</span>, <span class="phrase">food poisoning</span>, <span class="phrase">headcolds</span>, <span class="phrase">chicken pox</span> &amp; <span class="phrase">sore throats</span></p>
+</div>
+<h1>SickCity's Ten Sickest Cities*</h1>
+<p style="font-size: 11px; text-align: center">*Some cities are still coming online</p>
+<ol class="list">
+ <% i = 0 %>
+ <% @topten_cities.each do |mention|%>
+ <%- city = mention.city -%>
+ <%- if !city.hidden %>
+ <% i = i + 1%>
+ <li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>"><span class="city"><span class="toptencount"><%= "%02d" % i %></span> <%= city.name %></span> <span id="city_graph_<%= city.id %>" class="tiny_graph"><% @trend[city.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
+ <% end %>
+ <% break if i > 9 %>
+ <% end %>
+</ol>
+<h1>All Countries</h1>
+<ul class="list">
+ <% @countries.each do |country|%>
+ <li><a href="/<%= country.country %>/"><span class="city"><%= country.country %></span></a></li>
+ <% end %>
+</ul>
+<div class="navbar">
+ <a class="forward button home" href="/"><img src="/images/bk2l.png" /><img class="buttonimage" src="/images/logosmall.png" /></a>
+ <a class="forward button" href="/cities/add/"><img src="/images/bk2l.png" />Add Your City</a>
+ <a class="forward button" href="http://github.com/paulmwatson/sickcity"><img src="/images/bk2l.png" />Download SickCity</a>
+ <a class="forward button" href="/about/"><img src="/images/bk2l.png" />About</a>
+</div>
+<script type="text/javascript">
+ $('.tiny_graph').sparkline();
+</script>
View
2 app/views/layouts/base.html.erb
@@ -4,6 +4,8 @@
<title><%= @phrase.title %> in <%= @city.name %> on <%= @range_center.strftime('%B %d') %> - SickCity</title>
<% elsif controller.controller_name == 'phrases' %>
<title>Realtime disease detection for <%= @city.name %> - SickCity</title>
+ <% elsif controller.controller_name == 'cities' %>
+ <title>Realtime disease detection for <%= params[:country] %> - SickCity</title>
<% else %>
<title>Realtime disease detection for your city - SickCity</title>
<% end %>
View
2 app/views/phrases/index.html.erb
@@ -9,7 +9,7 @@
</ul>
<a id="map" href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=<%= @location.latitude %>,<%= @location.longitude %>"><img src="http://maps.google.com/staticmap?center=<%= @location.latitude %>,<%= @location.longitude %>&zoom=11&size=500x213&maptype=mobile&key=<%= GOOGLE_API_KEY %>&sensor=false" alt="Map" /></a>
<div class="navbar">
- <a class="back home" href="/">Cities<img src="/images/bkr.png" /></a>
+ <a class="back home" href="/<%= params[:country] %>/">Cities<img src="/images/bkr.png" /></a>
<a class="forward button home" href="/"><img src="/images/bk2l.png" /><img class="buttonimage" src="/images/logosmall.png" /></a>
<h1><%= params[:city] %></h1>
</div>
View
2 config/routes.rb
@@ -12,7 +12,7 @@
map.resources :cities
- map.root :controller => 'cities'
+ map.root :controller => 'countries'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
View
8 test/functional/countries_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class CountriesControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit 32df3b3

Please sign in to comment.