Class: TDAmeritrade::Operations::ReplaceWatchlist

Inherits:
BaseOperation
  • Object
show all
Includes:
Support::BuildWatchlistItems
Defined in:
lib/tdameritrade/operations/replace_watchlist.rb

Constant Summary

Constants inherited from BaseOperation

BaseOperation::HTTP_DEBUG_OUTPUT

Instance Attribute Summary

Attributes inherited from BaseOperation

#client

Instance Method Summary collapse

Methods included from Support::BuildWatchlistItems

#build_watchlist_items

Methods inherited from BaseOperation

#initialize

Methods included from Util

handle_api_error, parse_json_response, response_success?

Constructor Details

This class inherits a constructor from TDAmeritrade::Operations::BaseOperation

Instance Method Details

#call(account_id, watchlist_id, watchlist_name, symbols_to_add = []) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tdameritrade/operations/replace_watchlist.rb', line 8

def call(, watchlist_id, watchlist_name, symbols_to_add=[])
  symbols_to_add = symbols_to_add.is_a?(String) ? [symbols_to_add] : symbols_to_add
  body = {
    "name": watchlist_name,
    "watchlistId": watchlist_id,
    "watchlistItems": build_watchlist_items(symbols_to_add)
  }.to_json

  uri = URI("https://api.tdameritrade.com/v1/accounts/#{account_id}/watchlists/#{watchlist_id}")
  request = Net::HTTP::Put.new(
    uri.path,
    'authorization' => "Bearer #{client.access_token}",
    'content-type' => 'application/json'
  )
  request.body = body
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) }

  if response_success?(response)
    true
  else
    parse_api_response(response)
  end
end