tdameritrade-api-ruby 1.3.0.20210215

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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +2 -0
  4. data/CHANGELOG.md +29 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +84 -0
  8. data/Rakefile +8 -0
  9. data/lib/tdameritrade-api-ruby.rb +1 -0
  10. data/lib/tdameritrade.rb +9 -0
  11. data/lib/tdameritrade/authentication.rb +65 -0
  12. data/lib/tdameritrade/client.rb +56 -0
  13. data/lib/tdameritrade/error.rb +20 -0
  14. data/lib/tdameritrade/operations/base_operation.rb +36 -0
  15. data/lib/tdameritrade/operations/create_watchlist.rb +30 -0
  16. data/lib/tdameritrade/operations/get_instrument_fundamentals.rb +22 -0
  17. data/lib/tdameritrade/operations/get_price_history.rb +55 -0
  18. data/lib/tdameritrade/operations/get_quotes.rb +21 -0
  19. data/lib/tdameritrade/operations/get_watchlists.rb +12 -0
  20. data/lib/tdameritrade/operations/replace_watchlist.rb +33 -0
  21. data/lib/tdameritrade/operations/support/build_watchlist_items.rb +21 -0
  22. data/lib/tdameritrade/operations/update_watchlist.rb +33 -0
  23. data/lib/tdameritrade/util.rb +50 -0
  24. data/lib/tdameritrade/version.rb +3 -0
  25. data/spec/spec_helper.rb +110 -0
  26. data/spec/support/authenticated_client.rb +12 -0
  27. data/spec/support/spec/mocks/mock_get_instrument_fundamentals.rb +17 -0
  28. data/spec/support/spec/mocks/mock_get_price_history.rb +17 -0
  29. data/spec/support/spec/mocks/mock_get_quotes.rb +17 -0
  30. data/spec/support/spec/mocks/mock_watchlists.rb +43 -0
  31. data/spec/support/spec/mocks/tdameritrade_api_mock_base.rb +21 -0
  32. data/spec/support/webmock_off.rb +7 -0
  33. data/spec/tdameritrade/operations/create_watchlist_spec.rb +42 -0
  34. data/spec/tdameritrade/operations/error_spec.rb +75 -0
  35. data/spec/tdameritrade/operations/get_instrument_fundamentals_spec.rb +182 -0
  36. data/spec/tdameritrade/operations/get_price_history_spec.rb +160 -0
  37. data/spec/tdameritrade/operations/get_quotes_spec.rb +351 -0
  38. data/spec/tdameritrade/operations/get_watchlists_spec.rb +80 -0
  39. data/spec/tdameritrade/operations/replace_watchlist_spec.rb +45 -0
  40. data/spec/tdameritrade/operations/update_watchlist_spec.rb +45 -0
  41. data/tdameritrade-api-ruby.gemspec +33 -0
  42. metadata +253 -0
@@ -0,0 +1,55 @@
1
+ require 'tdameritrade/operations/base_operation'
2
+
3
+ module TDAmeritrade; module Operations
4
+ class GetPriceHistory < BaseOperation
5
+
6
+ # Not used right now, but can be used later on for validation
7
+ FREQUENCY_TYPE=[:minute, :daily, :weekly, :monthly]
8
+ PERIOD_TYPE=[:day, :month, :year, :ytd]
9
+
10
+ def call(
11
+ symbol,
12
+ period_type: nil,
13
+ period: nil,
14
+ frequency_type: nil,
15
+ frequency: nil,
16
+ end_date: nil, # should be a Date
17
+ start_date: nil, # should be a Date
18
+ need_extended_hours_data: false
19
+ )
20
+ params = {
21
+ apikey: client.client_id,
22
+ needExtendedHoursData: need_extended_hours_data,
23
+ }
24
+
25
+ params.merge!(frequencyType: frequency_type) if frequency_type
26
+ params.merge!(frequency: frequency) if frequency
27
+
28
+ # NOTE: can't use period if using start and end dates
29
+ params.merge!(periodType: period_type) if period_type
30
+ params.merge!(period: period) if period
31
+ params.merge!(startDate: "#{start_date.strftime('%s')}000") if start_date
32
+ params.merge!(endDate: "#{end_date.strftime('%s')}000") if end_date
33
+
34
+ response = perform_api_get_request(
35
+ url: "https://api.tdameritrade.com/v1/marketdata/#{symbol}/pricehistory",
36
+ query: params,
37
+ )
38
+
39
+ parsed_response = parse_api_response(response)
40
+
41
+ if parsed_response["candles"]
42
+ parsed_response["candles"].map do |candle|
43
+ if candle["datetime"].is_a? Numeric
44
+ candle["datetime"] = Time.at(candle["datetime"] / 1000)
45
+ end
46
+ end
47
+ end
48
+
49
+ parsed_response
50
+ end
51
+
52
+ private
53
+
54
+ end
55
+ end; end
@@ -0,0 +1,21 @@
1
+ require 'tdameritrade/operations/base_operation'
2
+
3
+ module TDAmeritrade; module Operations
4
+ class GetQuotes < BaseOperation
5
+
6
+ def call(symbols: [])
7
+ params = {
8
+ apikey: client.client_id,
9
+ symbol: symbols.join(','),
10
+ }
11
+
12
+ response = perform_api_get_request(
13
+ url: "https://api.tdameritrade.com/v1/marketdata/quotes",
14
+ query: params,
15
+ )
16
+
17
+ parse_api_response(response)
18
+ end
19
+
20
+ end
21
+ end; end
@@ -0,0 +1,12 @@
1
+ require 'tdameritrade/operations/base_operation'
2
+
3
+ module TDAmeritrade; module Operations
4
+ class GetWatchlists < BaseOperation
5
+
6
+ def call(account_id: )
7
+ response = perform_api_get_request(url: "https://api.tdameritrade.com/v1/accounts/#{account_id}/watchlists")
8
+ parse_api_response(response)
9
+ end
10
+
11
+ end
12
+ end; end
@@ -0,0 +1,33 @@
1
+ require 'tdameritrade/operations/base_operation'
2
+ require 'tdameritrade/operations/support/build_watchlist_items'
3
+
4
+ module TDAmeritrade; module Operations
5
+ class ReplaceWatchlist < BaseOperation
6
+ include Support::BuildWatchlistItems
7
+
8
+ def call(account_id, watchlist_id, watchlist_name, symbols_to_add=[])
9
+ symbols_to_add = symbols_to_add.is_a?(String) ? [symbols_to_add] : symbols_to_add
10
+ body = {
11
+ "name": watchlist_name,
12
+ "watchlistId": watchlist_id,
13
+ "watchlistItems": build_watchlist_items(symbols_to_add)
14
+ }.to_json
15
+
16
+ uri = URI("https://api.tdameritrade.com/v1/accounts/#{account_id}/watchlists/#{watchlist_id}")
17
+ request = Net::HTTP::Put.new(
18
+ uri.path,
19
+ 'authorization' => "Bearer #{client.access_token}",
20
+ 'content-type' => 'application/json'
21
+ )
22
+ request.body = body
23
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) }
24
+
25
+ if response_success?(response)
26
+ true
27
+ else
28
+ parse_api_response(response)
29
+ end
30
+ end
31
+
32
+ end
33
+ end; end
@@ -0,0 +1,21 @@
1
+ module TDAmeritrade; module Operations; module Support
2
+ module BuildWatchlistItems
3
+
4
+ # This gem only supports EQUITY type, even though there is a lot more you can do with the API
5
+ def build_watchlist_items(symbol_list)
6
+ symbol_list.map do |symbol|
7
+ {
8
+ "quantity": 0,
9
+ "averagePrice": 0,
10
+ "commission": 0,
11
+ "purchasedDate": (Date.today).strftime('%Y-%m-%d'),
12
+ "instrument": {
13
+ "symbol": symbol,
14
+ "assetType": "EQUITY"
15
+ }
16
+ }
17
+ end
18
+ end
19
+
20
+ end
21
+ end; end; end
@@ -0,0 +1,33 @@
1
+ require 'tdameritrade/operations/base_operation'
2
+ require 'tdameritrade/operations/support/build_watchlist_items'
3
+
4
+ module TDAmeritrade; module Operations
5
+ class UpdateWatchlist < BaseOperation
6
+ include Support::BuildWatchlistItems
7
+
8
+ def call(account_id, watchlist_id, watchlist_name, symbols_to_add=[])
9
+ symbols_to_add = symbols_to_add.is_a?(String) ? [symbols_to_add] : symbols_to_add
10
+ body = {
11
+ "name": watchlist_name,
12
+ "watchlistId": watchlist_id,
13
+ "watchlistItems": build_watchlist_items(symbols_to_add)
14
+ }.to_json
15
+
16
+ uri = URI("https://api.tdameritrade.com/v1/accounts/#{account_id}/watchlists/#{watchlist_id}")
17
+ request = Net::HTTP::Patch.new(
18
+ uri.path,
19
+ 'authorization' => "Bearer #{client.access_token}",
20
+ 'content-type' => 'application/json'
21
+ )
22
+ request.body = body
23
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) }
24
+
25
+ if response_success?(response)
26
+ true
27
+ else
28
+ parse_api_response(response)
29
+ end
30
+ end
31
+
32
+ end
33
+ end; end
@@ -0,0 +1,50 @@
1
+ require 'tdameritrade/error'
2
+
3
+ module TDAmeritrade
4
+ module Util
5
+ module_function
6
+
7
+ def handle_api_error(response)
8
+ # "Individual App's transactions per seconds restriction, please update to commercial apps for unrestricted tps"
9
+ if response.code == 429
10
+ raise TDAmeritrade::Error::RateLimitError.new(response.body)
11
+ elsif response.code == 401
12
+ raise TDAmeritrade::Error::NotAuthorizedError.new(response.body)
13
+ end
14
+
15
+ error_message = JSON.parse(response.body)['error']
16
+ raise TDAmeritrade::Error::TDAmeritradeError.new("#{response.code}: #{error_message}")
17
+ rescue JSON::ParserError
18
+ raise TDAmeritrade::Error::TDAmeritradeError.new(
19
+ "Unable to parse error response from TD Ameritrade API: #{response.body}"
20
+ )
21
+ end
22
+
23
+ def parse_json_response(response)
24
+ handle_api_error(response) unless response_success?(response)
25
+
26
+ stripped_text = response.body.strip
27
+ sanitized_text = stripped_text[0] == "\\" ? stripped_text[1..-1] : stripped_text
28
+ json = JSON.parse(sanitized_text)
29
+
30
+ case json
31
+ when Array
32
+ json.map { |item| Hashie::Mash.new(item) } # assuming that we're only dealing with arrays of hashes
33
+ when Hash
34
+ Hashie::Mash.new(json)
35
+ else
36
+ json
37
+ end
38
+
39
+ rescue JSON::ParserError
40
+ raise TDAmeritrade::Error::TDAmeritradeError.new(
41
+ "Unable to parse response from TD Ameritrade API: #{sanitized_text}"
42
+ )
43
+ end
44
+ alias :parse_api_response :parse_json_response
45
+
46
+ def response_success?(response)
47
+ response.code.to_s =~ /^2\d\d/
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module TDAmeritrade
2
+ VERSION = '1.3.0.20210215'
3
+ end
@@ -0,0 +1,110 @@
1
+ require 'pry'
2
+ require 'webmock/rspec'
3
+
4
+ Dir[File.join(File.dirname(__FILE__), 'support', '/**/*.rb')].each { |f| require(f) }
5
+
6
+ WebMock.disable_net_connect!
7
+
8
+ # This file was generated by the `rspec --init` command. Conventionally, all
9
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
10
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
11
+ # this file to always be loaded, without a need to explicitly require it in any
12
+ # files.
13
+ #
14
+ # Given that it is always loaded, you are encouraged to keep this file as
15
+ # light-weight as possible. Requiring heavyweight dependencies from this file
16
+ # will add to the boot time of your test suite on EVERY test run, even for an
17
+ # individual file that may not need all of that loaded. Instead, consider making
18
+ # a separate helper file that requires the additional dependencies and performs
19
+ # the additional setup, and require it from the spec files that actually need
20
+ # it.
21
+ #
22
+ # The `.rspec` file also contains a few flags that are not defaults but that
23
+ # users commonly want.
24
+ #
25
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
26
+ RSpec.configure do |config|
27
+ # rspec-expectations config goes here. You can use an alternate
28
+ # assertion/expectation library such as wrong or the stdlib/minitest
29
+ # assertions if you prefer.
30
+ config.expect_with :rspec do |expectations|
31
+ # This option will default to `true` in RSpec 4. It makes the `description`
32
+ # and `failure_message` of custom matchers include text for helper methods
33
+ # defined using `chain`, e.g.:
34
+ # be_bigger_than(2).and_smaller_than(4).description
35
+ # # => "be bigger than 2 and smaller than 4"
36
+ # ...rather than:
37
+ # # => "be bigger than 2"
38
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
39
+ end
40
+
41
+ # rspec-mocks config goes here. You can use an alternate test double
42
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
43
+ config.mock_with :rspec do |mocks|
44
+ # Prevents you from mocking or stubbing a method that does not exist on
45
+ # a real object. This is generally recommended, and will default to
46
+ # `true` in RSpec 4.
47
+ mocks.verify_partial_doubles = true
48
+ end
49
+
50
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
51
+ # have no way to turn it off -- the option exists only for backwards
52
+ # compatibility in RSpec 3). It causes shared context metadata to be
53
+ # inherited by the metadata hash of host groups and examples, rather than
54
+ # triggering implicit auto-inclusion in groups with matching metadata.
55
+ config.shared_context_metadata_behavior = :apply_to_host_groups
56
+
57
+ # The settings below are suggested to provide a good initial experience
58
+ # with RSpec, but feel free to customize to your heart's content.
59
+ =begin
60
+ # This allows you to limit a spec run to individual examples or groups
61
+ # you care about by tagging them with `:focus` metadata. When nothing
62
+ # is tagged with `:focus`, all examples get run. RSpec also provides
63
+ # aliases for `it`, `describe`, and `context` that include `:focus`
64
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
65
+ config.filter_run_when_matching :focus
66
+
67
+ # Allows RSpec to persist some state between runs in order to support
68
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
69
+ # you configure your source control system to ignore this file.
70
+ config.example_status_persistence_file_path = "spec/examples.txt"
71
+
72
+ # Limits the available syntax to the non-monkey patched syntax that is
73
+ # recommended. For more details, see:
74
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
75
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
76
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
77
+ config.disable_monkey_patching!
78
+
79
+ # This setting enables warnings. It's recommended, but in some cases may
80
+ # be too noisy due to issues in dependencies.
81
+ config.warnings = true
82
+
83
+ # Many RSpec users commonly either run the entire suite or an individual
84
+ # file, and it's useful to allow more verbose output when running an
85
+ # individual spec file.
86
+ if config.files_to_run.one?
87
+ # Use the documentation formatter for detailed output,
88
+ # unless a formatter has already been configured
89
+ # (e.g. via a command-line flag).
90
+ config.default_formatter = 'doc'
91
+ end
92
+
93
+ # Print the 10 slowest examples and example groups at the
94
+ # end of the spec run, to help surface which specs are running
95
+ # particularly slow.
96
+ config.profile_examples = 10
97
+
98
+ # Run specs in random order to surface order dependencies. If you find an
99
+ # order dependency and want to debug it, you can fix the order by providing
100
+ # the seed, which is printed after each run.
101
+ # --seed 1234
102
+ config.order = :random
103
+
104
+ # Seed global randomization in this process using the `--seed` CLI option.
105
+ # Setting this allows you to use `--seed` to deterministically reproduce
106
+ # test failures related to randomization by passing the same `--seed` value
107
+ # as the one that triggered the failure.
108
+ Kernel.srand config.seed
109
+ =end
110
+ end
@@ -0,0 +1,12 @@
1
+ shared_context 'authenticated client' do
2
+ let!(:client) do
3
+ # If you turn off WebMock by including the 'webmock_off' shared context in your tests, you can hit the
4
+ # live API with a real ping. Just be sure to replace these values here with actual connection tokens.
5
+ TDAmeritrade::Client.new(
6
+ client_id: 'RUBYAPITEST@AMER.OAUTHAP',
7
+ redirect_uri: 'http://localhost:3000',
8
+ access_token: 'test_access_token',
9
+ refresh_token: 'test_refresh_token'
10
+ )
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), 'tdameritrade_api_mock_base.rb')
2
+
3
+ module TDAmeritrade; module Spec; module Mocks;
4
+ class MockGetInstrumentFundamentals < TDAmeritradeMockBase
5
+
6
+ def self.mock_find(request: { query: {}, headers: {} }, response: { status: 200, body: '' })
7
+ return if webmock_off?
8
+
9
+ url_params = build_url_params(request)
10
+ WebMock
11
+ .stub_request(:get, "#{API_BASE_URL}/instruments#{url_params}")
12
+ .with(request)
13
+ .to_return(response)
14
+ end
15
+
16
+ end
17
+ end; end; end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), 'tdameritrade_api_mock_base.rb')
2
+
3
+ module TDAmeritrade; module Spec; module Mocks;
4
+ class MockGetPriceHistory < TDAmeritradeMockBase
5
+
6
+ def self.mock_find(symbol: nil, request: { query: {}, headers: {} }, response: { status: 200, body: '' })
7
+ return if webmock_off?
8
+
9
+ url_params = build_url_params(request)
10
+ WebMock
11
+ .stub_request(:get, "#{API_BASE_URL}/marketdata/#{symbol}/pricehistory#{url_params}")
12
+ .with(request)
13
+ .to_return(response)
14
+ end
15
+
16
+ end
17
+ end; end; end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), 'tdameritrade_api_mock_base.rb')
2
+
3
+ module TDAmeritrade; module Spec; module Mocks;
4
+ class MockGetQuotes < TDAmeritradeMockBase
5
+
6
+ def self.mock_find(request: { query: {}, headers: {} }, response: { status: 200, body: '' })
7
+ return if webmock_off?
8
+
9
+ url_params = build_url_params(request)
10
+ WebMock
11
+ .stub_request(:get, "#{API_BASE_URL}/marketdata/quotes#{url_params}")
12
+ .with(request)
13
+ .to_return(response)
14
+ end
15
+
16
+ end
17
+ end; end; end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), 'tdameritrade_api_mock_base.rb')
2
+
3
+ module TDAmeritrade; module Spec; module Mocks;
4
+ class MockWatchlists < TDAmeritradeMockBase
5
+
6
+ def self.mock_create(account_id, request: { headers: {} }, response: { status: 200, body: '' } )
7
+ return if webmock_off?
8
+
9
+ WebMock
10
+ .stub_request(:post, "#{API_BASE_URL}/accounts/#{account_id}/watchlists")
11
+ .with(request)
12
+ .to_return(response)
13
+ end
14
+
15
+ def self.mock_find_for_account(account_id, request: { headers: {} }, response: { status: 200, body: '' })
16
+ return if webmock_off?
17
+
18
+ WebMock
19
+ .stub_request(:get, "#{API_BASE_URL}/accounts/#{account_id}/watchlists")
20
+ .with(request)
21
+ .to_return(response)
22
+ end
23
+
24
+ def self.mock_replace(account_id, watchlist_id, request: { headers: {} }, response: { status: 200, body: '' })
25
+ return if webmock_off?
26
+
27
+ WebMock
28
+ .stub_request(:put, "#{API_BASE_URL}/accounts/#{account_id}/watchlists/#{watchlist_id}")
29
+ .with(request)
30
+ .to_return(response)
31
+ end
32
+
33
+ def self.mock_update(account_id, watchlist_id, request: { headers: {} }, response: { status: 200, body: '' })
34
+ return if webmock_off?
35
+
36
+ WebMock
37
+ .stub_request(:patch, "#{API_BASE_URL}/accounts/#{account_id}/watchlists/#{watchlist_id}")
38
+ .with(request)
39
+ .to_return(response)
40
+ end
41
+
42
+ end
43
+ end; end; end