tastytrade-api-ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '039f4f50b776893b0cf82d0f7a4e74376ced5683c7e4a3328e143be465cd5b80'
4
+ data.tar.gz: cecafd94ea98b0d2a1adf55c9d4f8694f2ddd3423b21d28fa903dfd2c4b4c6b8
5
+ SHA512:
6
+ metadata.gz: abcbbcbe2ba28a6c1452af6c8bfeb477857a592fbf5a9453fdc95b5c223a2f20f476f189ca58782cbc430b12b15960ba7a7a50afb63c6f14c8d7829824418886
7
+ data.tar.gz: bdc8682292370b10fd62e89447bb1fba6857f0539c84fabcedd0b06d88fa935d8383d755c868974e2fe7ed33229c2cbce8f29a23cff3405a57fa9e365a90bb03
@@ -0,0 +1,77 @@
1
+ require 'tastytrade/util'
2
+ require 'httparty'
3
+
4
+ module Tastytrade
5
+ module Authentication
6
+ include Util
7
+
8
+ attr_reader :client_id, :secret, :redirect_uri, :authorization_code, :access_token, :refresh_token,
9
+ :access_token_expires_at
10
+
11
+ def default_headers
12
+ { 'Content-Type': 'application/x-www-form-urlencoded' }
13
+ end
14
+
15
+ # Returns a hash with access_token, refresh_token, expires_in, etc.
16
+ # Used for trusted partner applications that have obtained an authorization code.
17
+ def request_access_token(authorization_grant_code)
18
+ options = {
19
+ body: {
20
+ 'grant_type' => 'authorization_code',
21
+ 'code' => authorization_grant_code,
22
+ 'client_id' => client_id,
23
+ 'client_secret' => secret,
24
+ 'redirect_uri' => redirect_uri
25
+ },
26
+ headers: default_headers
27
+ }
28
+
29
+ response = HTTParty.post(
30
+ "#{Tastytrade::BASE_URL}/oauth/token",
31
+ options
32
+ )
33
+
34
+ unless response_success?(response)
35
+ raise Tastytrade::APIError.new(
36
+ "Unable to retrieve access tokens from API - #{response.code} - #{response.body}"
37
+ )
38
+ end
39
+
40
+ update_tokens(response)
41
+ response
42
+ end
43
+
44
+ # Refresh tokens never expire per TastyTrade docs.
45
+ def refresh_access_token
46
+ options = {
47
+ body: {
48
+ 'grant_type' => 'refresh_token',
49
+ 'refresh_token' => refresh_token,
50
+ 'client_secret' => secret
51
+ },
52
+ headers: default_headers
53
+ }
54
+
55
+ response = HTTParty.post(
56
+ "#{Tastytrade::BASE_URL}/oauth/token",
57
+ options
58
+ )
59
+
60
+ update_tokens(response)
61
+ response
62
+ end
63
+
64
+ private
65
+
66
+ def update_tokens(args={})
67
+ raise_error("#{args['error_code']} - #{args['error_description']}") if args.has_key?('error_code')
68
+
69
+ # Refresh tokens never expire per TastyTrade documentation
70
+ @access_token = args['access_token']
71
+ @refresh_token = args['refresh_token'] if args['refresh_token']
72
+ @access_token_expires_at = Time.now + args['expires_in'].to_i
73
+
74
+ args
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,49 @@
1
+ require 'tastytrade/authentication'
2
+ require 'tastytrade/error'
3
+ require 'tastytrade/version'
4
+ require 'tastytrade/operations/account_transactions/get_total_fees'
5
+ require 'tastytrade/operations/account_transactions/get_transaction'
6
+ require 'tastytrade/operations/account_transactions/list_transactions'
7
+ require 'tastytrade/operations/customer_account_info/get_customer'
8
+ require 'tastytrade/operations/customer_account_info/list_customer_accounts'
9
+ require 'tastytrade/operations/customer_account_info/get_account'
10
+
11
+ module Tastytrade
12
+ class Client
13
+ include Tastytrade::Authentication
14
+ include Tastytrade::Error
15
+
16
+ def initialize(**args)
17
+ @access_token = args[:access_token]
18
+ @refresh_token = args[:refresh_token]
19
+ @access_token_expires_at = args[:access_token_expires_at]
20
+ @client_id = args[:client_id] || raise_error('client_id is required!')
21
+ @secret = args[:secret] || raise_error('secret is required!')
22
+ @redirect_uri = args[:redirect_uri] || raise_error('redirect_uri is required!')
23
+ end
24
+
25
+ def get_total_fees(account_number:, **options)
26
+ Operations::AccountTransactions::GetTotalFees.new(self).call(account_number:, **options)
27
+ end
28
+
29
+ def get_transaction(account_number:, id:)
30
+ Operations::AccountTransactions::GetTransaction.new(self).call(account_number:, id:)
31
+ end
32
+
33
+ def list_transactions(account_number:, **options)
34
+ Operations::AccountTransactions::ListTransactions.new(self).call(account_number:, **options)
35
+ end
36
+
37
+ def get_customer
38
+ Operations::CustomerAccountInfo::GetCustomer.new(self).call
39
+ end
40
+
41
+ def list_customer_accounts
42
+ Operations::CustomerAccountInfo::ListCustomerAccounts.new(self).call
43
+ end
44
+
45
+ def get_account(account_number:)
46
+ Operations::CustomerAccountInfo::GetAccount.new(self).call(account_number:)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ module Tastytrade
2
+ class APIError < StandardError
3
+ end
4
+
5
+ class RateLimitError < StandardError
6
+ end
7
+
8
+ class NotAuthorizedError < StandardError
9
+ end
10
+
11
+ class BadRequestError < StandardError
12
+ end
13
+
14
+ module Error
15
+ module_function
16
+
17
+ def raise_error(message, klass=Tastytrade::APIError)
18
+ error = klass.new(message)
19
+ error.set_backtrace(caller)
20
+ raise error
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ require 'tastytrade/operations/base_operation'
2
+
3
+ module Tastytrade
4
+ module Operations
5
+ module AccountTransactions
6
+ class GetTotalFees < BaseOperation
7
+
8
+ # Calls GET /accounts/{account_number}/transactions/total-fees
9
+ #
10
+ # Options:
11
+ # date: String - "YYYY-MM-DD" (default: today)
12
+ def call(account_number:, date: nil)
13
+ query = date ? { date: date } : nil
14
+
15
+ perform_api_get_request(
16
+ url: "#{Tastytrade::BASE_URL}/accounts/#{account_number}/transactions/total-fees",
17
+ query: query
18
+ )
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ require 'tastytrade/operations/base_operation'
2
+
3
+ module Tastytrade
4
+ module Operations
5
+ module AccountTransactions
6
+ class GetTransaction < BaseOperation
7
+
8
+ # Calls GET /accounts/{account_number}/transactions/{id}
9
+ def call(account_number:, id:)
10
+ perform_api_get_request(
11
+ url: "#{Tastytrade::BASE_URL}/accounts/#{account_number}/transactions/#{id}"
12
+ )
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,113 @@
1
+ require 'tastytrade/operations/base_operation'
2
+
3
+ module Tastytrade
4
+ module Operations
5
+ module AccountTransactions
6
+ class ListTransactions < BaseOperation
7
+
8
+ VALID_SORT_VALUES = %w[Asc Desc].freeze
9
+
10
+ VALID_TYPES = [
11
+ 'Administrative Transfer',
12
+ 'Money Movement',
13
+ 'Receive Deliver',
14
+ 'Trade'
15
+ ].freeze
16
+
17
+ VALID_SUB_TYPES = [
18
+ 'ACAT', 'Assignment', 'Balance Adjustment', 'Cash Merger',
19
+ 'Cash Settled Assignment', 'Cash Settled Exercise', 'Credit Interest',
20
+ 'Debit Interest', 'Deposit', 'Dividend', 'Exercise', 'Expiration',
21
+ 'Fee', 'Forward Split', 'Fully Paid Stock Lending Income',
22
+ 'Futures Settlement', 'Mark to Market', 'Maturity', 'Reverse Split',
23
+ 'Reverse Split Removal', 'Special Dividend', 'Stock Merger',
24
+ 'Stock Merger Removal', 'Symbol Change', 'Transfer', 'Withdrawal'
25
+ ].freeze
26
+
27
+ VALID_INSTRUMENT_TYPES = %w[
28
+ Bond Cryptocurrency Equity EquityOffering EquityOption
29
+ Future FutureOption Index Unknown Warrant
30
+ ].freeze
31
+
32
+ VALID_ACTIONS = [
33
+ 'Buy', 'Buy to Close', 'Buy to Open',
34
+ 'Sell', 'Sell to Close', 'Sell to Open'
35
+ ].freeze
36
+
37
+ # Calls GET /accounts/{account_number}/transactions
38
+ #
39
+ # Options:
40
+ # sort: String - "Asc" or "Desc" (default: "Desc")
41
+ # type: String - single transaction type (mutually exclusive with types:)
42
+ # types: Array - multiple transaction types (mutually exclusive with type:)
43
+ # sub_type: Array - transaction sub-types (passed as 'sub-type[]')
44
+ # start_date: String - "YYYY-MM-DD"
45
+ # end_date: String - "YYYY-MM-DD"
46
+ # instrument_type: String
47
+ # symbol: String
48
+ # underlying_symbol: String
49
+ # action: String
50
+ # partition_key: String
51
+ # futures_symbol: String
52
+ # start_at: String - "YYYY-MM-DDTHH:MM:SS"
53
+ # end_at: String - "YYYY-MM-DDTHH:MM:SS"
54
+ # page_offset: Integer - for pagination
55
+ def call(account_number:, **options)
56
+ validate_options!(options)
57
+
58
+ query = build_query(options)
59
+
60
+ perform_api_get_request(
61
+ url: "#{Tastytrade::BASE_URL}/accounts/#{account_number}/transactions",
62
+ query: query.empty? ? nil : query
63
+ )
64
+ end
65
+
66
+ private
67
+
68
+ def validate_options!(options)
69
+ if options[:type] && options[:types]
70
+ raise ArgumentError, "'type' and 'types' are mutually exclusive"
71
+ end
72
+
73
+ if options[:sort] && !VALID_SORT_VALUES.include?(options[:sort])
74
+ raise ArgumentError, "sort must be one of: #{VALID_SORT_VALUES.join(', ')}"
75
+ end
76
+
77
+ if options[:type] && !VALID_TYPES.include?(options[:type])
78
+ raise ArgumentError, "type must be one of: #{VALID_TYPES.join(', ')}"
79
+ end
80
+
81
+ if options[:types]
82
+ invalid = Array(options[:types]).reject { |t| VALID_TYPES.include?(t) }
83
+ raise ArgumentError, "invalid types: #{invalid.join(', ')}" if invalid.any?
84
+ end
85
+
86
+ if options[:action] && !VALID_ACTIONS.include?(options[:action])
87
+ raise ArgumentError, "action must be one of: #{VALID_ACTIONS.join(', ')}"
88
+ end
89
+ end
90
+
91
+ def build_query(options)
92
+ query = {}
93
+ query[:sort] = options[:sort] if options[:sort]
94
+ query[:type] = options[:type] if options[:type]
95
+ query[:types] = options[:types] if options[:types]
96
+ query['sub-type'] = options[:sub_type] if options[:sub_type]
97
+ query['start-date'] = options[:start_date] if options[:start_date]
98
+ query['end-date'] = options[:end_date] if options[:end_date]
99
+ query['instrument-type'] = options[:instrument_type] if options[:instrument_type]
100
+ query[:symbol] = options[:symbol] if options[:symbol]
101
+ query['underlying-symbol'] = options[:underlying_symbol] if options[:underlying_symbol]
102
+ query[:action] = options[:action] if options[:action]
103
+ query['partition-key'] = options[:partition_key] if options[:partition_key]
104
+ query['futures-symbol'] = options[:futures_symbol] if options[:futures_symbol]
105
+ query['start-at'] = options[:start_at] if options[:start_at]
106
+ query['end-at'] = options[:end_at] if options[:end_at]
107
+ query['page-offset'] = options[:page_offset] if options[:page_offset]
108
+ query
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,62 @@
1
+ require 'tastytrade/error'
2
+ require 'tastytrade/util'
3
+
4
+ module Tastytrade
5
+ module Operations
6
+ class BaseOperation
7
+ include Tastytrade::Error
8
+ include Util
9
+
10
+ attr_reader :client
11
+
12
+ def initialize(client)
13
+ @client = client
14
+ end
15
+
16
+ private
17
+
18
+ def debug_output?
19
+ ENV['DEBUG_OUTPUT'].to_s == 'true'
20
+ end
21
+
22
+ def handle_api_error(response)
23
+ if response.code == 429
24
+ raise Tastytrade::RateLimitError.new(response.body)
25
+ elsif response.code == 401
26
+ raise Tastytrade::NotAuthorizedError.new(response.body)
27
+ elsif response.code == 404
28
+ raise Tastytrade::BadRequestError.new(response.body)
29
+ end
30
+
31
+ error_message = response['error'].to_s
32
+ raise Tastytrade::APIError.new("#{response.code}: #{error_message}")
33
+ rescue JSON::ParserError
34
+ raise Tastytrade::APIError.new(
35
+ "Unable to parse error response from TastyTrade API: #{response.code} - #{response.body} | #{response}"
36
+ )
37
+ end
38
+
39
+ def parse_response(response)
40
+ raise ArgumentError unless response.is_a?(HTTParty::Response)
41
+ handle_api_error(response) unless response_success?(response)
42
+
43
+ JSON.parse(response.body)
44
+ end
45
+
46
+ def perform_api_get_request(url:, query: nil)
47
+ options = {
48
+ headers: {
49
+ 'Authorization': "Bearer #{client.access_token}",
50
+ 'Accept': 'application/json'
51
+ }
52
+ }
53
+ options.merge!(query: query) if query
54
+ options.merge!(debug_output: $stdout) if debug_output?
55
+
56
+ response = HTTParty.get(url, options)
57
+
58
+ parse_response(response)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,18 @@
1
+ require 'tastytrade/operations/base_operation'
2
+
3
+ module Tastytrade
4
+ module Operations
5
+ module CustomerAccountInfo
6
+ class GetAccount < BaseOperation
7
+
8
+ # Calls GET /customers/me/accounts/{account_number}
9
+ def call(account_number:)
10
+ perform_api_get_request(
11
+ url: "#{Tastytrade::BASE_URL}/customers/me/accounts/#{account_number}"
12
+ )
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'tastytrade/operations/base_operation'
2
+
3
+ module Tastytrade
4
+ module Operations
5
+ module CustomerAccountInfo
6
+ class GetCustomer < BaseOperation
7
+
8
+ # Calls GET /customers/me
9
+ def call
10
+ perform_api_get_request(url: "#{Tastytrade::BASE_URL}/customers/me")
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'tastytrade/operations/base_operation'
2
+
3
+ module Tastytrade
4
+ module Operations
5
+ module CustomerAccountInfo
6
+ class ListCustomerAccounts < BaseOperation
7
+
8
+ # Calls GET /customers/me/accounts
9
+ def call
10
+ perform_api_get_request(url: "#{Tastytrade::BASE_URL}/customers/me/accounts")
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ require 'tastytrade/error'
2
+
3
+ module Tastytrade
4
+ module Util
5
+ module_function
6
+
7
+ def response_success?(response)
8
+ response.code.to_s =~ /^2\d\d/
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module Tastytrade
2
+ VERSION = '0.1.0'
3
+ BASE_URL = 'https://api.tastyworks.com'
4
+ end
@@ -0,0 +1 @@
1
+ require 'tastytrade'
data/lib/tastytrade.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'httparty'
2
+
3
+ require 'tastytrade/version'
4
+ require 'tastytrade/error'
5
+ require 'tastytrade/util'
6
+ require 'tastytrade/authentication'
7
+ require 'tastytrade/operations/base_operation'
8
+ require 'tastytrade/operations/account_transactions/get_total_fees'
9
+ require 'tastytrade/operations/account_transactions/get_transaction'
10
+ require 'tastytrade/operations/account_transactions/list_transactions'
11
+ require 'tastytrade/client'
12
+
13
+ module Tastytrade
14
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tastytrade-api-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Winston Kotzan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0.20'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0.20'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: This is a gem for connecting to the OAuth/JSON-based TastyTrade API.
98
+ See https://developer.tastytrade.com/ for the official documentation and to create
99
+ your developer account.
100
+ email:
101
+ - wak@wakproductions.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - lib/tastytrade-api-ruby.rb
107
+ - lib/tastytrade.rb
108
+ - lib/tastytrade/authentication.rb
109
+ - lib/tastytrade/client.rb
110
+ - lib/tastytrade/error.rb
111
+ - lib/tastytrade/operations/account_transactions/get_total_fees.rb
112
+ - lib/tastytrade/operations/account_transactions/get_transaction.rb
113
+ - lib/tastytrade/operations/account_transactions/list_transactions.rb
114
+ - lib/tastytrade/operations/base_operation.rb
115
+ - lib/tastytrade/operations/customer_account_info/get_account.rb
116
+ - lib/tastytrade/operations/customer_account_info/get_customer.rb
117
+ - lib/tastytrade/operations/customer_account_info/list_customer_accounts.rb
118
+ - lib/tastytrade/util.rb
119
+ - lib/tastytrade/version.rb
120
+ homepage: https://github.com/wakproductions/tastytrade-api-ruby
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.3.26
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: This is a simple gem for connecting to the TastyTrade OAuth API
143
+ test_files: []