class Accern::Cli

Attributes

args[R]
config_path[R]
feed[R]
filetype[R]
indexes[R]
options[R]
stdin[R]
stdout[R]
tickers[R]
token[R]
valid_types[R]

Public Class Methods

new(stdout: $stdout, stdin: $stdin, args: [], feed: Alpha) click to toggle source
# File lib/accern/cli.rb, line 7
def initialize(stdout: $stdout, stdin: $stdin, args: [], feed: Alpha)
  @stdout = stdout
  @stdin = stdin
  @args = args
  @options = {}
  @filetype = 'json'
  @valid_types = %w(json csv)
  @config_path = "#{ENV['HOME']}/.accern.rc.yml"
  @feed = feed
  @tickers = []
  @indexes = []
end

Public Instance Methods

ask_for_filetype() click to toggle source
# File lib/accern/cli.rb, line 36
def ask_for_filetype
  ask_question('Please enter the file type (JSON or CSV): ', :filetype)
  valid_filetype?
end
ask_for_token() click to toggle source
# File lib/accern/cli.rb, line 32
def ask_for_token
  ask_question('Please enter your API Token: ', :token)
end
load_config() click to toggle source
# File lib/accern/cli.rb, line 41
def load_config
  if File.exist?(config_path)
    config = YAML.load_file(config_path)
    @token = config[:token]
    @filetype = config[:filetype]
  else
    ask_for_info
  end
end
parse_options() click to toggle source
# File lib/accern/cli.rb, line 51
def parse_options
  parser = OptionParser.new do |opts|
    opts.banner = 'A command line interface for the Accern API'

    opts.on('--init', 'Display the getting started prompts.') do
      options[:init] = true
    end

    opts.on('--version', 'Show version') do
      options[:version] = Accern::VERSION
      puts Accern::VERSION
      exit
    end

    opts.on("--ticker NAME", "Filters document by ticker") do |tic|

      options[:ticker] = tic.to_s.downcase.split(',')
      @tickers += options[:ticker]
    end

    opts.on("--ticker-file PATH", "Receives the path to a file that has tickers") do |t_path|
      options[:ticker_path] = t_path
      @tickers += read_file(t_path)
    end

    index_options = ['sp500', 'russell1000', 'russell3000', 'wilshire5000', 'barrons400', 'dow30']
    opts.on("--index TYPE", "Filters document by index") do |i|
      options[:index] = i.to_s.downcase.split(',')
      options[:index] = sanitizes_input(options[:index])
      raise OptionParser::InvalidArgument.new("Invalid index options") unless (options[:index] - index_options).empty?
      @indexes += options[:index]
    end

    opts.on("--index-file PATH", "get filter indexes from file") do |i_path|
      parsed = read_file(i_path)
      parsed = sanitizes_input(parsed)
      raise OptionParser::InvalidArgument.new("Invalid index options") unless (parsed - index_options).empty?
      @indexes += parsed
    end

  end

  parser.parse!(args)
  @tickers = sanitizes_input(@tickers)
end
start() click to toggle source
# File lib/accern/cli.rb, line 20
def start
  load_config
  parse_options

  if options[:init]
    ask_for_info
  else
    feed.new(token: token, format: filetype.to_sym, ticker: tickers.join(','), index: indexes.join(','))
        .download_loop(path: './feed.jsonl')
  end
end

Private Instance Methods

ask_for_info() click to toggle source
# File lib/accern/cli.rb, line 103
def ask_for_info
  ask_for_token
  # ask_for_filetype
  save_config
end
ask_question(text, field) click to toggle source
# File lib/accern/cli.rb, line 109
def ask_question(text, field)
  stdout.print text
  instance_variable_set(
    "@#{field}", stdin.gets.to_s.chomp.downcase.delete('.', '')
  )
end
read_file(path) click to toggle source
# File lib/accern/cli.rb, line 132
def read_file(path)
  return [] unless File.exist?(path)
  File.readlines(path).map { |x| x.downcase.chomp }
end
sanitizes_input(arg) click to toggle source
# File lib/accern/cli.rb, line 99
def sanitizes_input(arg)
   arg.map {|t| t.gsub(/\W/, '')}
end
save_config() click to toggle source
# File lib/accern/cli.rb, line 122
def save_config
  config = {
    token: token,
    filetype: filetype
  }

  File.write(config_path, config.to_yaml)
  stdout.puts 'Your client is now configured and settings saved to ~/.accern.rc.yml.'
end
valid_filetype?() click to toggle source
# File lib/accern/cli.rb, line 116
def valid_filetype?
  return if valid_types.include?(filetype)
  stdout.puts 'Invalid file type, defaulting to JSON'
  @filetype = 'json'
end