From 07c8c0d013711c5dd5f0886c1adc71c82f272e25 Mon Sep 17 00:00:00 2001 From: jacekmaciag Date: Sun, 20 Oct 2024 23:21:38 +0200 Subject: [PATCH] Add getTradingHours command --- lib/xtb/http.rb | 1 + lib/xtb/http/trading_hours.rb | 38 ++++++++++++++++++ spec/xtb/http/trade_records_spec.rb | 1 - spec/xtb/http/trading_hours_spec.rb | 61 +++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 lib/xtb/http/trading_hours.rb create mode 100644 spec/xtb/http/trading_hours_spec.rb diff --git a/lib/xtb/http.rb b/lib/xtb/http.rb index 2cf2833..acd1bc6 100644 --- a/lib/xtb/http.rb +++ b/lib/xtb/http.rb @@ -21,6 +21,7 @@ require_relative 'http/trade_records' require_relative 'http/trades' require_relative 'http/trades_history' +require_relative 'http/trading_hours' module Xtb module Http # :nodoc: diff --git a/lib/xtb/http/trading_hours.rb b/lib/xtb/http/trading_hours.rb new file mode 100644 index 0000000..ba9c172 --- /dev/null +++ b/lib/xtb/http/trading_hours.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Xtb + module Http + # http://developers.xstore.pro/documentation/2.5.0#getTradingHours + class TradingHours < Command + TradingHoursRecord = Data.define(:quotes, :symbol, :trading) do + # Represents both quotes and trading times records. + TimesRecord = Data.define(:day, :from_t, :to_t) + + def initialize(symbol:, quotes: TimesRecord.new, trading: TimesRecord.new) + super(quotes:, symbol:, trading:) + end + end + + # @param symbols [Array] + def initialize(symbols) + @symbols = symbols + end + + def call + super.map { |record| TradingHoursRecord.new(**record) } + end + + private + + attr_reader :symbols + + def command = :getTradingHours + + def arguments + { + symbols: + } + end + end + end +end diff --git a/spec/xtb/http/trade_records_spec.rb b/spec/xtb/http/trade_records_spec.rb index b821395..9e5a372 100644 --- a/spec/xtb/http/trade_records_spec.rb +++ b/spec/xtb/http/trade_records_spec.rb @@ -82,7 +82,6 @@ sl: 0.0, storage: -4.46, symbol: 'EURUSD', - timestamp: 1_272_540_251_000, tp: 0.0, volume: 0.10 diff --git a/spec/xtb/http/trading_hours_spec.rb b/spec/xtb/http/trading_hours_spec.rb new file mode 100644 index 0000000..dfff653 --- /dev/null +++ b/spec/xtb/http/trading_hours_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +RSpec.describe Xtb::Http::TradingHours do + subject(:command) { described_class.new(symbols) } + + let(:symbols) { %w[EURUSD] } + + let(:request) do + { + command: :getTradingHours, + arguments: { + symbols: + } + } + end + let(:response) do + JSON.dump( + { + 'status': true, + 'return_data': [ + { + 'quotes': { + 'day': 2, + 'from_t': 63_000_000, + 'to_t': 63_300_000 + }, + 'symbol': 'EURUSD', + 'trading': { + 'day': 2, + 'from_t': 63_000_000, + 'to_t': 63_300_000 + } + } + ] + } + ) + end + + describe '#call' do + specify do + expect(Xtb::Http::SslClient) + .to receive(:request) + .with(JSON.dump(request)) + .and_return(response) + expect(command.call.first) + .to have_attributes( + quotes: { + day: 2, + from_t: 63_000_000, + to_t: 63_300_000 + }, + symbol: 'EURUSD', + trading: { + day: 2, + from_t: 63_000_000, + to_t: 63_300_000 + } + ) + end + end +end