mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-25 12:32:30 +01:00
Refactor internals
- Removed yajl-ruby - Removed sinatra-jsonp - Inlined #halt_with_message - Added last_modified header to root path
This commit is contained in:
parent
59df76477e
commit
702f6ac9aa
3
Gemfile
3
Gemfile
@ -8,9 +8,8 @@ gem 'fixer'
|
|||||||
gem 'newrelic_rpm'
|
gem 'newrelic_rpm'
|
||||||
gem 'rake'
|
gem 'rake'
|
||||||
gem 'sequel_pg'
|
gem 'sequel_pg'
|
||||||
gem 'sinatra-jsonp'
|
gem 'sinatra'
|
||||||
gem 'unicorn'
|
gem 'unicorn'
|
||||||
gem 'yajl-ruby'
|
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
gem 'minitest'
|
gem 'minitest'
|
||||||
|
@ -11,7 +11,6 @@ GEM
|
|||||||
minitest (5.8.4)
|
minitest (5.8.4)
|
||||||
minitest-around (0.3.2)
|
minitest-around (0.3.2)
|
||||||
minitest (~> 5.0)
|
minitest (~> 5.0)
|
||||||
multi_json (1.11.2)
|
|
||||||
newrelic_rpm (3.15.1.316)
|
newrelic_rpm (3.15.1.316)
|
||||||
oga (2.2)
|
oga (2.2)
|
||||||
ast
|
ast
|
||||||
@ -41,15 +40,11 @@ GEM
|
|||||||
rack (~> 1.5)
|
rack (~> 1.5)
|
||||||
rack-protection (~> 1.4)
|
rack-protection (~> 1.4)
|
||||||
tilt (>= 1.3, < 3)
|
tilt (>= 1.3, < 3)
|
||||||
sinatra-jsonp (0.4.4)
|
|
||||||
multi_json (~> 1.8)
|
|
||||||
sinatra (~> 1.0)
|
|
||||||
slop (3.6.0)
|
slop (3.6.0)
|
||||||
tilt (2.0.2)
|
tilt (2.0.2)
|
||||||
unicorn (5.1.0)
|
unicorn (5.1.0)
|
||||||
kgio (~> 2.6)
|
kgio (~> 2.6)
|
||||||
raindrops (~> 0.7)
|
raindrops (~> 0.7)
|
||||||
yajl-ruby (1.2.1)
|
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
@ -64,9 +59,8 @@ DEPENDENCIES
|
|||||||
rake
|
rake
|
||||||
sequel_pg
|
sequel_pg
|
||||||
shotgun
|
shotgun
|
||||||
sinatra-jsonp
|
sinatra
|
||||||
unicorn
|
unicorn
|
||||||
yajl-ruby
|
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
1.11.2
|
1.11.2
|
||||||
|
@ -6,7 +6,7 @@ require 'pathname'
|
|||||||
# Encapsulates app configuration
|
# Encapsulates app configuration
|
||||||
module App
|
module App
|
||||||
class << self
|
class << self
|
||||||
attr_reader :logger
|
attr_reader :logger, :version, :released_at
|
||||||
|
|
||||||
def env
|
def env
|
||||||
ENV['RACK_ENV'] || 'development'
|
ENV['RACK_ENV'] || 'development'
|
||||||
@ -15,11 +15,9 @@ module App
|
|||||||
def root
|
def root
|
||||||
Pathname.pwd
|
Pathname.pwd
|
||||||
end
|
end
|
||||||
|
|
||||||
def version
|
|
||||||
`git rev-parse --short HEAD 2>/dev/null`.strip!
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@logger = Logger.new(STDOUT)
|
@logger = Logger.new(STDOUT)
|
||||||
|
@version = `git rev-parse --short HEAD 2>/dev/null`.strip!
|
||||||
|
@released_at = `git show -s --format=%ci HEAD`
|
||||||
end
|
end
|
||||||
|
35
lib/api.rb
35
lib/api.rb
@ -1,8 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'json'
|
||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
require 'sinatra/jsonp'
|
|
||||||
require 'yajl'
|
|
||||||
require 'quote'
|
require 'quote'
|
||||||
|
|
||||||
configure do
|
configure do
|
||||||
@ -30,26 +29,31 @@ end
|
|||||||
helpers do
|
helpers do
|
||||||
def quote
|
def quote
|
||||||
@quote ||= begin
|
@quote ||= begin
|
||||||
ret = Quote.new(params).attributes
|
Quote.new(params).attributes.tap do |quote|
|
||||||
ret[:rates].keep_if { |k, _| symbols.include?(k) } if symbols
|
quote[:rates].keep_if { |k, _| symbols.include?(k) } if symbols
|
||||||
|
end
|
||||||
ret
|
|
||||||
end
|
end
|
||||||
rescue Quote::Invalid => ex
|
rescue Quote::Invalid => ex
|
||||||
halt_with_message 422, ex.message
|
halt 422, JSON.generate(error: ex.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def symbols
|
def symbols
|
||||||
return @symbols if defined?(@symbols)
|
@symbols ||= begin
|
||||||
|
params.values_at('symbols', 'currencies').first.tap do |symbols|
|
||||||
@symbols = begin
|
symbols.split(',') if symbols
|
||||||
ret = params.delete('symbols') || params.delete('currencies')
|
end
|
||||||
ret.split(',') if ret
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def halt_with_message(status, message)
|
def jsonp(data)
|
||||||
halt status, Yajl::Encoder.encode(error: message)
|
callback = params.delete('callback')
|
||||||
|
if callback
|
||||||
|
content_type :js
|
||||||
|
"#{callback}(#{JSON.generate(data)})"
|
||||||
|
else
|
||||||
|
content_type :json
|
||||||
|
JSON.generate(data)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def enable_cross_origin
|
def enable_cross_origin
|
||||||
@ -65,6 +69,7 @@ end
|
|||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
enable_cross_origin
|
enable_cross_origin
|
||||||
|
last_modified App.released_at
|
||||||
jsonp details: 'http://fixer.io', version: App.version
|
jsonp details: 'http://fixer.io', version: App.version
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -81,5 +86,5 @@ get(/(?<date>\d{4}-\d{2}-\d{2})/) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
not_found do
|
not_found do
|
||||||
halt_with_message 404, 'Not found'
|
halt 404, JSON.generate(error: 'Not found')
|
||||||
end
|
end
|
||||||
|
@ -8,7 +8,7 @@ describe 'the API' do
|
|||||||
include Rack::Test::Methods
|
include Rack::Test::Methods
|
||||||
|
|
||||||
let(:app) { Sinatra::Application }
|
let(:app) { Sinatra::Application }
|
||||||
let(:json) { Yajl::Parser.new.parse last_response.body }
|
let(:json) { JSON.parse last_response.body }
|
||||||
let(:headers) { last_response.headers }
|
let(:headers) { last_response.headers }
|
||||||
|
|
||||||
it 'describes itself' do
|
it 'describes itself' do
|
||||||
@ -42,14 +42,11 @@ describe 'the API' do
|
|||||||
json['rates'].wont_be :empty?
|
json['rates'].wont_be :empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a last modified header for latest quote' do
|
it 'returns a last modified header' do
|
||||||
get '/latest'
|
%w(/ /latest /2012-11-20).each do |path|
|
||||||
|
get path
|
||||||
headers['Last-Modified'].wont_be_nil
|
headers['Last-Modified'].wont_be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a last modified header for historical quote' do
|
|
||||||
get '/2012-11-20'
|
|
||||||
headers['Last-Modified'].wont_be_nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'allows cross-origin requests' do
|
it 'allows cross-origin requests' do
|
||||||
|
@ -6,31 +6,36 @@ describe 'the API' do
|
|||||||
include Rack::Test::Methods
|
include Rack::Test::Methods
|
||||||
|
|
||||||
let(:app) { Sinatra::Application }
|
let(:app) { Sinatra::Application }
|
||||||
let(:json) { Yajl::Parser.new.parse last_response.body }
|
let(:json) { JSON.parse last_response.body }
|
||||||
|
|
||||||
it 'handles unfound pages' do
|
it 'handles unfound pages' do
|
||||||
get '/foo'
|
get '/foo'
|
||||||
last_response.status.must_equal 404
|
last_response.status.must_equal 404
|
||||||
|
json.wont_be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'will not process an invalid date' do
|
it 'will not process an invalid date' do
|
||||||
get '/2010-31-01'
|
get '/2010-31-01'
|
||||||
last_response.must_be :unprocessable?
|
last_response.must_be :unprocessable?
|
||||||
|
json.wont_be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'will not process a date before 2000' do
|
it 'will not process a date before 2000' do
|
||||||
get '/1999-01-01'
|
get '/1999-01-01'
|
||||||
last_response.must_be :unprocessable?
|
last_response.must_be :unprocessable?
|
||||||
|
json.wont_be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'will not process an invalid base' do
|
it 'will not process an invalid base' do
|
||||||
get '/latest?base=UAH'
|
get '/latest?base=UAH'
|
||||||
last_response.must_be :unprocessable?
|
last_response.must_be :unprocessable?
|
||||||
|
json.wont_be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'handles malformed queries' do
|
it 'handles malformed queries' do
|
||||||
get '/latest?base=USD?callback=?'
|
get '/latest?base=USD?callback=?'
|
||||||
last_response.must_be :unprocessable?
|
last_response.must_be :unprocessable?
|
||||||
|
json.wont_be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns fresh dates' do
|
it 'returns fresh dates' do
|
||||||
|
Loading…
Reference in New Issue
Block a user