mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-21 18:42:29 +01:00
First commit
This commit is contained in:
commit
97e34dbc14
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.bundle
|
8
Gemfile
Normal file
8
Gemfile
Normal 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
38
Gemfile.lock
Normal 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
1
Procfile
Normal file
@ -0,0 +1 @@
|
||||
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
|
15
Rakefile
Normal file
15
Rakefile
Normal 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
|
2
config/unicorn.rb
Normal file
2
config/unicorn.rb
Normal file
@ -0,0 +1,2 @@
|
||||
worker_processes 4 # amount of unicorn workers to spin up
|
||||
timeout 30 # restarts workers that hang for 30 seconds
|
12
db/migrations/001_create_currencies.rb
Normal file
12
db/migrations/001_create_currencies.rb
Normal 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
11
lib/app.rb
Normal 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
13
lib/db.rb
Normal 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
28
lib/snapshot.rb
Normal 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
24
public/index.html
Normal 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>
|
Loading…
Reference in New Issue
Block a user