Class: Tastytrade::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tastytrade/client.rb

Overview

HTTP client wrapper for Tastytrade API communication

Constant Summary collapse

DEFAULT_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, timeout: DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.



14
15
16
17
# File 'lib/tastytrade/client.rb', line 14

def initialize(base_url:, timeout: DEFAULT_TIMEOUT)
  @base_url = base_url
  @timeout = timeout
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/tastytrade/client.rb', line 10

def base_url
  @base_url
end

Instance Method Details

#delete(path, headers = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/tastytrade/client.rb', line 40

def delete(path, headers = {})
  response = connection.delete(path, nil, default_headers.merge(headers))
  handle_response(response)
rescue Faraday::ConnectionFailed => e
  raise Tastytrade::NetworkTimeoutError, "Request timed out: #{e.message}"
end

#get(path, params = {}, headers = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/tastytrade/client.rb', line 19

def get(path, params = {}, headers = {})
  response = connection.get(path, params, default_headers.merge(headers))
  handle_response(response)
rescue Faraday::ConnectionFailed => e
  raise Tastytrade::NetworkTimeoutError, "Request timed out: #{e.message}"
end

#post(path, body = {}, headers = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/tastytrade/client.rb', line 26

def post(path, body = {}, headers = {})
  response = connection.post(path, body.to_json, default_headers.merge(headers))
  handle_response(response)
rescue Faraday::ConnectionFailed => e
  raise Tastytrade::NetworkTimeoutError, "Request timed out: #{e.message}"
end

#put(path, body = {}, headers = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/tastytrade/client.rb', line 33

def put(path, body = {}, headers = {})
  response = connection.put(path, body.to_json, default_headers.merge(headers))
  handle_response(response)
rescue Faraday::ConnectionFailed => e
  raise Tastytrade::NetworkTimeoutError, "Request timed out: #{e.message}"
end