Permalink
Browse files

most simplist solution to displaying current count on the web

  • Loading branch information...
1 parent 0ff38c0 commit a6a41cbe94eceaf06fe84c2692f9aebf22afb77a @stevenwilkin committed Feb 12, 2012
Showing with 41 additions and 0 deletions.
  1. +5 −0 coffee_tracker.rb
  2. +24 −0 test/api/home_page_test.rb
  3. +12 −0 views/index.html
View
@@ -5,6 +5,11 @@
class CoffeeTracker < Sinatra::Base
+ get '/' do
+ count = $redis.hget('coffee', Time.now.strftime("%Y%m%d")) || 0
+ File.read('./views/index.html') % count
+ end
+
get '/api' do
count = $redis.hget('coffee', Time.now.strftime("%Y%m%d")) || 0
[200, {'Content-Type' => 'text/plain'}, count.to_s]
View
@@ -0,0 +1,24 @@
+require_relative '../test_helpers'
+
+class ApiHomePageTest < Test::Unit::TestCase
+ include TestHelpers
+
+ def setup
+ @valid_api_key = 'valid'
+
+ $redis.flushdb
+ $redis.sadd 'api_keys', @valid_api_key
+ end
+
+ def test_displays_zero_initially
+ get '/'
+ assert_match /<h1>0<\/h1>/, last_response.body, 'Should display zero'
+ end
+
+ def test_displays_one_after_post
+ header('X-Api-Key', @valid_api_key)
+ post '/api'
+ get '/'
+ assert_match /<h1>1<\/h1>/, last_response.body, 'Should display one'
+ end
+end
View
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8" />
+ <title>Coffee Tracker</title>
+</head>
+
+<body>
+ <h1>%d</h1>
+</body>
+
+</html>

0 comments on commit a6a41cb

Please sign in to comment.