Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 97e34dbc14bb93ea32c4c32fbc0e6905fba44742 @hakanensari committed Nov 20, 2012
Showing with 157 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +8 −0 Gemfile
  3. +38 −0 Gemfile.lock
  4. +1 −0 Procfile
  5. +1 −0 README.md
  6. +15 −0 Rakefile
  7. +3 −0 config.ru
  8. +2 −0 config/unicorn.rb
  9. +12 −0 db/migrations/001_create_currencies.rb
  10. +11 −0 lib/app.rb
  11. +13 −0 lib/db.rb
  12. +28 −0 lib/snapshot.rb
  13. +24 −0 public/index.html
@@ -0,0 +1 @@
+.bundle
@@ -0,0 +1,8 @@
+source 'http://rubygems.org'
+
+gem 'fixer'
+gem 'pg'
+gem 'sequel'
+gem 'sinatra-jsonp'
+gem 'unicorn'
+gem 'yajl-ruby', require: 'yajl'
@@ -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
@@ -0,0 +1 @@
+web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
@@ -0,0 +1 @@
+# Fixer.io
@@ -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
@@ -0,0 +1,3 @@
+require './lib/app'
+
+run Sinatra::Application
@@ -0,0 +1,2 @@
+worker_processes 4 # amount of unicorn workers to spin up
+timeout 30 # restarts workers that hang for 30 seconds
@@ -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
+
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,24 @@
+<!doctype html>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <meta name="description" content="Fixer is a free JSON API for foreign exchange rates.">
+ <title>Fixer</title>
+ <script type="text/javascript">
+ var _gaq = _gaq || [];
+ _gaq.push(['_setAccount', 'UA-36475354-1']);
+ _gaq.push(['_trackPageview']);
+
+ (function() {
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+ })();
+ </script>
+ </head>
+ <body>
+ <header>
+ <h1>Fixer is a free JSON API for exchange rates.</h1>
+ </header>
+ </body>
+</html>

0 comments on commit 97e34db

Please sign in to comment.