First commit

This commit is contained in:
Hakan Ensari 2012-11-20 16:36:12 +00:00
commit 97e34dbc14
13 changed files with 157 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.bundle

8
Gemfile Normal file
View File

@ -0,0 +1,8 @@
source 'http://rubygems.org'
gem 'fixer'
gem 'pg'
gem 'sequel'
gem 'sinatra-jsonp'
gem 'unicorn'
gem 'yajl-ruby', require: 'yajl'

38
Gemfile.lock Normal file
View File

@ -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

1
Procfile Normal file
View File

@ -0,0 +1 @@
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

1
README.md Normal file
View File

@ -0,0 +1 @@
# Fixer.io

15
Rakefile Normal file
View File

@ -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

3
config.ru Normal file
View File

@ -0,0 +1,3 @@
require './lib/app'
run Sinatra::Application

2
config/unicorn.rb Normal file
View File

@ -0,0 +1,2 @@
worker_processes 4 # amount of unicorn workers to spin up
timeout 30 # restarts workers that hang for 30 seconds

View File

@ -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

11
lib/app.rb Normal file
View File

@ -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

13
lib/db.rb Normal file
View File

@ -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

28
lib/snapshot.rb Normal file
View File

@ -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

24
public/index.html Normal file
View File

@ -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>