Refactor query building out of server

This commit is contained in:
Hakan Ensari 2020-05-02 15:14:47 +01:00
parent 72fe6022c9
commit ec302e726c
3 changed files with 10 additions and 4 deletions

View File

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

View File

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

View File

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