Class: Xtb::Http::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/xtb/http/command.rb

Overview

Base class for all commands.

Defined Under Namespace

Classes: Request, Response

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Command

Returns a new instance of Command.



83
84
85
86
# File 'lib/xtb/http/command.rb', line 83

def initialize(**args)
  @args = args
  @request_data = Request.new(command:, arguments:).to_json
end

Class Method Details

.call(**args) ⇒ Object



88
89
90
# File 'lib/xtb/http/command.rb', line 88

def self.call(**args)
  new(**args).call
end

Instance Method Details

#callObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/xtb/http/command.rb', line 92

def call
  raw_response = if args[:connection]
                   args.fetch(:connection).request(request_data)
                 else
                   Xtb::Http::Client.post do |connection|
                     connection.request(request_data)
                   end
                 end

  response = Response.new(command:, raw_response:)

  raise ErrorFactory.create(response.error_code, response.error_description) unless response.success?

  response
end