Class: Accern::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/accern/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout: $stdout, stdin: $stdin, args: [], feed: Alpha) ⇒ Cli

Returns a new instance of Cli.



7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/accern/cli.rb', line 3

def args
  @args
end

#config_pathObject (readonly)

Returns the value of attribute config_path.



3
4
5
# File 'lib/accern/cli.rb', line 3

def config_path
  @config_path
end

#feedObject (readonly)

Returns the value of attribute feed.



3
4
5
# File 'lib/accern/cli.rb', line 3

def feed
  @feed
end

#filetypeObject (readonly)

Returns the value of attribute filetype.



3
4
5
# File 'lib/accern/cli.rb', line 3

def filetype
  @filetype
end

#indexesObject (readonly)

Returns the value of attribute indexes.



3
4
5
# File 'lib/accern/cli.rb', line 3

def indexes
  @indexes
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/accern/cli.rb', line 3

def options
  @options
end

#stdinObject (readonly)

Returns the value of attribute stdin.



3
4
5
# File 'lib/accern/cli.rb', line 3

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



3
4
5
# File 'lib/accern/cli.rb', line 3

def stdout
  @stdout
end

#tickersObject (readonly)

Returns the value of attribute tickers.



3
4
5
# File 'lib/accern/cli.rb', line 3

def tickers
  @tickers
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/accern/cli.rb', line 3

def token
  @token
end

#valid_typesObject (readonly)

Returns the value of attribute valid_types.



3
4
5
# File 'lib/accern/cli.rb', line 3

def valid_types
  @valid_types
end

Instance Method Details

#ask_for_filetypeObject



36
37
38
39
# 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_tokenObject



32
33
34
# File 'lib/accern/cli.rb', line 32

def ask_for_token
  ask_question('Please enter your API Token: ', :token)
end

#load_configObject



41
42
43
44
45
46
47
48
49
# 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_optionsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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

#startObject



20
21
22
23
24
25
26
27
28
29
30
# 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