Class: TDAmeritrade::Operations::CreateWatchlist

Inherits:
BaseOperation show all
Includes:
Support::BuildWatchlistItems
Defined in:
lib/tdameritrade/operations/create_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_name, symbols) ⇒ Object



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

def call(, watchlist_name, symbols)
  body = {
    "name": watchlist_name,
    "watchlistItems": build_watchlist_items(symbols)
  }.to_json
  uri = URI("https://api.tdameritrade.com/v1/accounts/#{}/watchlists")
  request = Net::HTTP::Post.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