2018-03-08 02:05:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-11-21 14:45:44 +01:00
|
|
|
# FIXME: Validations are all over. Clean up!
|
2018-03-08 02:05:19 +01:00
|
|
|
class Query
|
2024-11-19 20:06:47 +01:00
|
|
|
class << self
|
|
|
|
def build(params)
|
2024-11-21 14:45:44 +01:00
|
|
|
query = new(params).to_h
|
|
|
|
raise ArgumentError, "bad currency pair" if [query[:base]] == query[:symbols]
|
|
|
|
|
|
|
|
query
|
2024-11-19 20:06:47 +01:00
|
|
|
end
|
2020-05-02 16:14:47 +02:00
|
|
|
end
|
|
|
|
|
2018-03-08 02:05:19 +01:00
|
|
|
def initialize(params = {})
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def amount
|
2018-07-05 21:19:37 +02:00
|
|
|
return unless @params[:amount]
|
2018-09-11 19:08:14 +02:00
|
|
|
|
2024-11-21 13:15:06 +01:00
|
|
|
value = @params[:amount].to_f
|
|
|
|
raise ArgumentError, "invalid amount" unless value.positive?
|
|
|
|
|
|
|
|
value
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def base
|
2018-07-05 21:19:37 +02:00
|
|
|
@params.values_at(:from, :base).compact.first&.upcase
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def symbols
|
2024-11-19 20:06:47 +01:00
|
|
|
@params.values_at(:to, :symbols).compact.first&.upcase&.split(",")
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def date
|
2018-07-05 21:19:37 +02:00
|
|
|
if @params[:date]
|
|
|
|
Date.parse(@params[:date])
|
|
|
|
else
|
|
|
|
(Date.parse(@params[:start_date])..Date.parse(@params[:end_date]))
|
|
|
|
end
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_h
|
2023-02-28 17:49:48 +01:00
|
|
|
{ amount:, base:, date:, symbols: }.compact
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
end
|