From 97e34dbc14bb93ea32c4c32fbc0e6905fba44742 Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Tue, 20 Nov 2012 16:36:12 +0000 Subject: [PATCH] First commit --- .gitignore | 1 + Gemfile | 8 ++++++ Gemfile.lock | 38 ++++++++++++++++++++++++++ Procfile | 1 + README.md | 1 + Rakefile | 15 ++++++++++ config.ru | 3 ++ config/unicorn.rb | 2 ++ db/migrations/001_create_currencies.rb | 12 ++++++++ lib/app.rb | 11 ++++++++ lib/db.rb | 13 +++++++++ lib/snapshot.rb | 28 +++++++++++++++++++ public/index.html | 24 ++++++++++++++++ 13 files changed, 157 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Procfile create mode 100644 README.md create mode 100644 Rakefile create mode 100644 config.ru create mode 100644 config/unicorn.rb create mode 100644 db/migrations/001_create_currencies.rb create mode 100644 lib/app.rb create mode 100644 lib/db.rb create mode 100644 lib/snapshot.rb create mode 100644 public/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..677c465 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.bundle diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4e4f171 --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +source 'http://rubygems.org' + +gem 'fixer' +gem 'pg' +gem 'sequel' +gem 'sinatra-jsonp' +gem 'unicorn' +gem 'yajl-ruby', require: 'yajl' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..009dab9 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,38 @@ +GEM + remote: http://rubygems.org/ + specs: + fixer (0.5.0) + nokogiri (~> 1.5) + kgio (2.7.4) + multi_json (1.3.7) + nokogiri (1.5.5) + pg (0.14.1) + rack (1.4.1) + rack-protection (1.2.0) + rack + raindrops (0.10.0) + sequel (3.41.0) + sinatra (1.3.3) + rack (~> 1.3, >= 1.3.6) + rack-protection (~> 1.2) + tilt (~> 1.3, >= 1.3.3) + sinatra-jsonp (0.4) + multi_json (~> 1.3.7) + sinatra (~> 1.0) + tilt (1.3.3) + unicorn (4.4.0) + kgio (~> 2.6) + rack + raindrops (~> 0.7) + yajl-ruby (1.1.0) + +PLATFORMS + ruby + +DEPENDENCIES + fixer + pg + sequel + sinatra-jsonp + unicorn + yajl-ruby diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..9c82374 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb diff --git a/README.md b/README.md new file mode 100644 index 0000000..eaab3ea --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Fixer.io diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a9e0a70 --- /dev/null +++ b/Rakefile @@ -0,0 +1,15 @@ +require_relative 'lib/db' +require 'fixer' + +task :reset do + data = Fixer::Feed.new(:historical).to_a + + Currency.delete + Currency.multi_insert data +end + +task :update do + Fixer::Feed.new.each do |hsh| + Currency.find_or_create hsh + end +end diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..e47fcde --- /dev/null +++ b/config.ru @@ -0,0 +1,3 @@ +require './lib/app' + +run Sinatra::Application diff --git a/config/unicorn.rb b/config/unicorn.rb new file mode 100644 index 0000000..faa09d4 --- /dev/null +++ b/config/unicorn.rb @@ -0,0 +1,2 @@ +worker_processes 4 # amount of unicorn workers to spin up +timeout 30 # restarts workers that hang for 30 seconds diff --git a/db/migrations/001_create_currencies.rb b/db/migrations/001_create_currencies.rb new file mode 100644 index 0000000..c723eb5 --- /dev/null +++ b/db/migrations/001_create_currencies.rb @@ -0,0 +1,12 @@ +Sequel.migration do + change do + create_table :currencies do + Date :date + String :iso_code + Float :rate + + index [:date, :iso_code], unique: true + end + end +end + diff --git a/lib/app.rb b/lib/app.rb new file mode 100644 index 0000000..b874035 --- /dev/null +++ b/lib/app.rb @@ -0,0 +1,11 @@ +require_relative 'snapshot' +require 'sinatra' +require 'sinatra/jsonp' + +get '/' do + File.read File.join 'public', 'index.html' +end + +get '/latest' do + jsonp Snapshot.last +end diff --git a/lib/db.rb b/lib/db.rb new file mode 100644 index 0000000..8976761 --- /dev/null +++ b/lib/db.rb @@ -0,0 +1,13 @@ +require 'sequel' + +Sequel.connect ENV['DATABASE_URL'] || 'postgres://localhost/fixer' + +class Currency < Sequel::Model + def self.last_date + order(:date).last.date + end + + def to_hash + { iso_code => rate } + end +end diff --git a/lib/snapshot.rb b/lib/snapshot.rb new file mode 100644 index 0000000..85438b2 --- /dev/null +++ b/lib/snapshot.rb @@ -0,0 +1,28 @@ +require_relative 'db' +require 'yajl' + +class Snapshot + def self.last + new Currency.last_date + end + + def initialize(date) + @date = date + end + + def to_json + Yajl::Encoder.new.encode base: 'EUR', + date: @date, + rates: rates + end + + private + + def rates + Currency + .where(date: @date) + .reduce({}) { |hsh, currency| + hsh.update currency.to_hash + } + end +end diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..c2b6fce --- /dev/null +++ b/public/index.html @@ -0,0 +1,24 @@ + + + + + + Fixer + + + +
+

Fixer is a free JSON API for exchange rates.

+
+ +