From 3d763e15b0f21284bb2cf89011e494574b28d958 Mon Sep 17 00:00:00 2001 From: Jack Dempsey Date: Sat, 15 Nov 2008 15:16:34 -0500 Subject: [PATCH] add in ability to convert rps -> rpd; rework layouts --- app/controllers/home.rb | 14 +++++++---- app/helpers/home_helper.rb | 9 ++++++- app/views/home/_rps.html.haml | 9 +++++-- app/views/home/index.html.haml | 3 --- app/views/home/rps_calc.html.haml | 7 +----- app/views/shared/_header_links.html.haml | 4 +-- spec/requests/home_spec.rb | 31 +++++++++++++++--------- 7 files changed, 47 insertions(+), 30 deletions(-) diff --git a/app/controllers/home.rb b/app/controllers/home.rb index 6f2c90a..6cc422b 100644 --- a/app/controllers/home.rb +++ b/app/controllers/home.rb @@ -8,12 +8,16 @@ def index def rps_calc redirect '/' if request.get? - if params[:rpd] - @rpd = params[:rpd].tr(',','').to_i - if @rpd >= 86_400 - @rps = @rpd / 86_400 # number of seconds in a day + if params[:requests_per] + @requests_per = params[:requests_per].tr(',','').to_i + if params[:convert_to] == 'rpd' + @answer = @requests_per * 86_400 # number of seconds in a day else - message[:error] = "You need a number greater than 86,400" + if @requests_per >= 86_400 + @answer = @requests_per / 86_400 # number of seconds in a day + else + message[:error] = "You need a number greater than 86,400" + end end end render diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb index aab1536..7a03b1c 100644 --- a/app/helpers/home_helper.rb +++ b/app/helpers/home_helper.rb @@ -1,5 +1,12 @@ module Merb module HomeHelper - + def show_conversion_message(convert_to) + conversion_string = if convert_to == 'rpd' + "reqs/sec is #{@answer} reqs/day" + else + "reqs/day is #{@answer} reqs/sec" + end + "#{h params[:requests_per]} #{conversion_string}" + end end end # Merb \ No newline at end of file diff --git a/app/views/home/_rps.html.haml b/app/views/home/_rps.html.haml index b54e664..3105e4c 100644 --- a/app/views/home/_rps.html.haml +++ b/app/views/home/_rps.html.haml @@ -1,3 +1,8 @@ +%h2 Convert to: = form :action => '/home/rps_calc' do - %input{:name => 'rpd', :size => 40} - =submit 'submit' + = radio_group :convert_to, [{:value => :rps, :label => 'reqs/sec'}, {:value => :rpd, :label => 'reqs/day'}] + %p + = show_conversion_message(params[:convert_to]) + %p + %input{:name => 'requests_per', :size => 40} + = submit 'submit' diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 8aff91a..e7fa5e5 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -1,4 +1 @@ - -Requests per day -> Requests per second: - = partial :rps \ No newline at end of file diff --git a/app/views/home/rps_calc.html.haml b/app/views/home/rps_calc.html.haml index 0e307c3..9388f53 100644 --- a/app/views/home/rps_calc.html.haml +++ b/app/views/home/rps_calc.html.haml @@ -1,7 +1,2 @@ - unless message[:error] - = h params[:rpd] - Requests per day is - = @rps - Requests per second - - = partial :rps \ No newline at end of file + = partial :rps diff --git a/app/views/shared/_header_links.html.haml b/app/views/shared/_header_links.html.haml index 295c766..f9e80c4 100644 --- a/app/views/shared/_header_links.html.haml +++ b/app/views/shared/_header_links.html.haml @@ -1,3 +1,3 @@ -= link_to 'about', 'http://github.com/jackdempsey/builtbythenet/tree/master' += link_to 'about', 'http://github.com/jackdempsey/builtbythenet/tree/master', :target => :blank | -= link_to 'request a feature', 'http://builtbythenet.uservoice.com/' \ No newline at end of file += link_to 'request a feature', 'http://builtbythenet.uservoice.com/', :target => :blank \ No newline at end of file diff --git a/spec/requests/home_spec.rb b/spec/requests/home_spec.rb index 1a5c99e..5781319 100644 --- a/spec/requests/home_spec.rb +++ b/spec/requests/home_spec.rb @@ -1,24 +1,33 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb') describe "/home/rps_calc" do - - it "should return 100 RPS for a input of 8.64 million" do - request("/home/rps_calc", :params => {:rpd => 8_640_000}).should contain('100') + + it "should redirect to / if request is a GET" do + request("/home/rps_calc").should redirect end - it "should show an error message if RPD is < 86,400" do - request("/home/rps_calc", :params => {:rpd => 86399}).should have_selector('div#error') + it "should convert to RPS by default" do + request("/home/rps_calc", :params => {:requests_per => 8_640_000}).should contain('reqs/day is 100 reqs/sec') end it "should handle numbers with commas in them" do - request("/home/rps_calc", :params => {:rpd => '8,640,000'}).should contain('100') + request("/home/rps_calc", :params => {:requests_per => '8,640,000'}).should contain('100') end - it "should show the RPD and RPS" do - request("/home/rps_calc", :params => {:rpd => '8,640,000'}).should contain('8,640,000') + describe "converting into RPS" do + it "should return 100 RPS for a input of 8.64 million" do + request("/home/rps_calc", :params => {:requests_per => 8_640_000}).should contain('100') + end + + it "should show an error message if requests_per is < 86,400" do + request("/home/rps_calc", :params => {:requests_per => 86399}).should have_selector('div#error') + end end - - it "should redirect to / if request is a GET" do - request("/home/rps_calc").should redirect + + describe "converting into RPD" do + it "should return 8.64 million reqs/day for a input of 100 reqs/sec" do + request("/home/rps_calc", :params => {:requests_per => 100, :convert_to => 'rpd'}).should contain('100 reqs/sec is 8640000 reqs/day') + end end + end \ No newline at end of file