mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-22 02:52:49 +01:00
Refactor query building out of server
This commit is contained in:
parent
72fe6022c9
commit
ec302e726c
@ -1,6 +1,10 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Query
|
class Query
|
||||||
|
def self.build(params)
|
||||||
|
new(params).to_h
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(params = {})
|
def initialize(params = {})
|
||||||
@params = params
|
@params = params
|
||||||
end
|
end
|
||||||
|
@ -32,6 +32,7 @@ set :static_cache_control, [:public, max_age: 300]
|
|||||||
helpers do
|
helpers do
|
||||||
def end_of_day_quote
|
def end_of_day_quote
|
||||||
@end_of_day_quote ||= begin
|
@end_of_day_quote ||= begin
|
||||||
|
query = Query.build(params)
|
||||||
quote = Quote::EndOfDay.new(query)
|
quote = Quote::EndOfDay.new(query)
|
||||||
quote.perform
|
quote.perform
|
||||||
halt 404 if quote.not_found?
|
halt 404 if quote.not_found?
|
||||||
@ -42,6 +43,7 @@ helpers do
|
|||||||
|
|
||||||
def interval_quote
|
def interval_quote
|
||||||
@interval_quote ||= begin
|
@interval_quote ||= begin
|
||||||
|
query = Query.build(params)
|
||||||
quote = Quote::Interval.new(query)
|
quote = Quote::Interval.new(query)
|
||||||
quote.perform
|
quote.perform
|
||||||
halt 404 if quote.not_found?
|
halt 404 if quote.not_found?
|
||||||
@ -50,10 +52,6 @@ helpers do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def query
|
|
||||||
Query.new(params).to_h
|
|
||||||
end
|
|
||||||
|
|
||||||
def json(data)
|
def json(data)
|
||||||
json = Oj.dump(data, mode: :compat)
|
json = Oj.dump(data, mode: :compat)
|
||||||
callback = params['callback']
|
callback = params['callback']
|
||||||
|
@ -4,6 +4,10 @@ require_relative 'helper'
|
|||||||
require 'query'
|
require 'query'
|
||||||
|
|
||||||
describe Query do
|
describe Query do
|
||||||
|
it 'builds a query hash' do
|
||||||
|
Query.build(date: '2014-01-01').must_be_kind_of Hash
|
||||||
|
end
|
||||||
|
|
||||||
it 'returns given amount' do
|
it 'returns given amount' do
|
||||||
query = Query.new(amount: '100')
|
query = Query.new(amount: '100')
|
||||||
query.amount.must_equal 100.0
|
query.amount.must_equal 100.0
|
||||||
|
Loading…
Reference in New Issue
Block a user