From 6fb6ab7feb67e3d9c8603cd59e65df8d2c827f99 Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Wed, 21 Nov 2012 11:31:50 +0000 Subject: [PATCH] Set base currency --- lib/app.rb | 16 +++++++++++++--- lib/snapshot.rb | 10 ++++++---- spec/snapshots_spec.rb | 1 - 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/app.rb b/lib/app.rb index a81d9f1..466a789 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -2,14 +2,24 @@ require_relative 'snapshot' require 'sinatra' require 'sinatra/jsonp' +helpers do + def base + params[:base] || 'EUR' + end +end + get '/' do File.read File.join 'public', 'index.html' end get '/latest' do - jsonp Snapshot.last + jsonp Snapshot + .last + .to_base base end -get '/:date' do - jsonp Snapshot.new params[:date] +get '/:date' do |date| + jsonp Snapshot + .new(date) + .to_base base end diff --git a/lib/snapshot.rb b/lib/snapshot.rb index 85438b2..e84748a 100644 --- a/lib/snapshot.rb +++ b/lib/snapshot.rb @@ -10,10 +10,12 @@ class Snapshot @date = date end - def to_json - Yajl::Encoder.new.encode base: 'EUR', - date: @date, - rates: rates + def to_base(base) + { + base: base, + date: @date, + rates: rates + } end private diff --git a/spec/snapshots_spec.rb b/spec/snapshots_spec.rb index 6b7c156..e54cf33 100644 --- a/spec/snapshots_spec.rb +++ b/spec/snapshots_spec.rb @@ -14,7 +14,6 @@ describe 'the application' do end it 'sets base currency' do - skip 'pending' get '/latest?base=USD' json['base'].must_equal 'USD' end