Skip to content

Commit

Permalink
Add getTradingHours command
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekmaciag committed Oct 20, 2024
1 parent 81833d4 commit 07c8c0d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/xtb/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions lib/xtb/http/trading_hours.rb
Original file line number Diff line number Diff line change
@@ -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<String|Symbol>]
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
1 change: 0 additions & 1 deletion spec/xtb/http/trade_records_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
sl: 0.0,
storage: -4.46,
symbol: 'EURUSD',

timestamp: 1_272_540_251_000,
tp: 0.0,
volume: 0.10
Expand Down
61 changes: 61 additions & 0 deletions spec/xtb/http/trading_hours_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 07c8c0d

Please sign in to comment.