Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved popular nutrients to the top, general clean up

  • Loading branch information...
commit ae9ee6fadfed215c4b7ced4b0f2e29c7f34a54bf 1 parent e831a84
Xavier Shay authored
30 ausnom.rb
View
@@ -2,13 +2,18 @@
require 'json'
require './models'
-LAST_MODIFIED = Time.mktime(2011,1,20).httpdate
+LAST_MODIFIED = Time.mktime(2011,1,24).httpdate
+TOP_NUTRIENTS = %w(ENERGY-04DF PROT FE CA FAT)
get '/' do
last_modified LAST_MODIFIED
@q = params[:q]
@foods = @q ? Food.search(@q) : []
- erb :index
+ if @foods.length == 1
+ redirect "/foods/#{@foods[0].food_id}"
+ else
+ erb :index
+ end
end
get '/foods/:id.json' do
@@ -22,10 +27,31 @@
get '/foods/:id' do
last_modified LAST_MODIFIED
@food = Food.get!(params[:id])
+ all_nutrients = @food.nutrients.all(:order => [:description.asc])
+ @top_nutrients = all_nutrients.select {|x| TOP_NUTRIENTS.include?(x.nutrient_id) }.sort_by {|x| TOP_NUTRIENTS.index(x.nutrient_id) }
+ @other_nutrients = all_nutrients - @top_nutrients
erb :show
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
+
+ def format_value v
+ if v > 10
+ v.round
+ else
+ v
+ end
+ end
+
+ def page_title
+ if @food
+ @food.name
+ elsif @q
+ "Search for '#{@q}'"
+ else
+ "AU and NZ nutrional data web service"
+ end
+ end
end
26 public/ausnom.css
View
@@ -24,3 +24,29 @@ a:visited {
acronym {
border-bottom: 1px dotted;
}
+
+table {
+ border-spacing: 0;
+}
+
+td {
+ padding: 3px 5px;
+}
+
+td:first-child {
+ padding-right: 15px;
+}
+
+tr:nth-child(2n+1) {
+ background-color: #eee;
+}
+
+tr.spacer {
+ background-color: #999;
+}
+
+caption {
+ font-style: italic;
+ text-align: center;
+ width: 100%;
+}
4 views/index.erb
View
@@ -1,11 +1,11 @@
<% unless @q.blank? %>
- <p><a href="/?q=<%=h @q %>.json">JSON</a></p>
+ <p><a href="/?q=<%=h @q %>.json">Download as JSON</a></p>
<% end %>
<% if @foods.empty? %>
<p class='important'>Nutritional data of foods, as released by <acronym title="Food Standards Australia and New Zealand government agency">FSANZ</acronym>.</p>
<p>Results are also available in JSON format, to keep robots happy.</p>
<p>You may reproduce this data for commercial purposes only by contacting <a href="http://www.foodstandards.gov.au/monitoringandsurveillance/nuttab2006/index.cfm">foodstandards.gov.au</a>.</p>
- <p>Made by <a href="http://xaviershay.com">Xavier Shay</a>, <a href="mailto:contact@rhnh.net">drop us a line</a> if you like.</p>
+ <p>Made by <a href="http://xaviershay.com">Xavier Shay</a>, <a href="mailto:contact@rhnh.net">drop us a line</a> if you like. The code is <a href="https://github.com/xaviershay/ausnom">open source</a>.</p>
<% else %>
<ul>
<% @foods.each do |food| %>
2  views/layout.erb
View
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>ausnom - AU and NZ nutrional data web service</title>
+ <title>ausnom - <%=h page_title %></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/ausnom.css" type="text/css" media="screen" charset="utf-8" />
</head>
22 views/show.erb
View
@@ -1,11 +1,27 @@
<h1><%= @food.name %></h1>
<p><%= @food.description %></p>
-<p><a href="/foods/<%= @food.food_id %>.json">JSON</a></p>
+<p><a href="/foods/<%= @food.food_id %>.json">Download as JSON</a></p>
<table>
- <% @food.nutrients.all(:order => [:description.asc]).each do |nutrient| %>
+ <caption>Measurements per 100g</caption>
+ <% @top_nutrients.each do |nutrient| %>
<tr>
+ <td><%=format_value nutrient.value %><%= nutrient.scale.downcase %></td>
+ <td><%= nutrient.description %></td>
+ </tr>
+ <% if nutrient.scale == 'KJ' %>
+ <tr>
+ <td><%=format_value nutrient.value / 4.184 %>cal</td>
+ <td><%= nutrient.description %></td>
+ </tr>
+ <% end %>
+ <% end %>
+ <tr class='spacer'>
+ <td colspan='2'></td>
+ </tr>
+ <% @other_nutrients.each do |nutrient| %>
+ <tr>
+ <td><%=format_value nutrient.value %><%= nutrient.scale.downcase %></td>
<td><%= nutrient.description %></td>
- <td><%= nutrient.value %> <%= nutrient.scale %></td>
</tr>
<% end %>
</table>
Please sign in to comment.
Something went wrong with that request. Please try again.