Class: Accern::Cli
- Inherits:
-
Object
- Object
- Accern::Cli
- Defined in:
- lib/accern/cli.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#feed ⇒ Object
readonly
Returns the value of attribute feed.
-
#filetype ⇒ Object
readonly
Returns the value of attribute filetype.
-
#indexes ⇒ Object
readonly
Returns the value of attribute indexes.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
-
#tickers ⇒ Object
readonly
Returns the value of attribute tickers.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#valid_types ⇒ Object
readonly
Returns the value of attribute valid_types.
Instance Method Summary collapse
- #ask_for_filetype ⇒ Object
- #ask_for_token ⇒ Object
-
#initialize(stdout: $stdout, stdin: $stdin, args: [], feed: Alpha) ⇒ Cli
constructor
A new instance of Cli.
- #load_config ⇒ Object
- #parse_options ⇒ Object
- #start ⇒ Object
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def args @args end |
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def config_path @config_path end |
#feed ⇒ Object (readonly)
Returns the value of attribute feed.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def feed @feed end |
#filetype ⇒ Object (readonly)
Returns the value of attribute filetype.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def filetype @filetype end |
#indexes ⇒ Object (readonly)
Returns the value of attribute indexes.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def indexes @indexes end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def @options end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def stdout @stdout end |
#tickers ⇒ Object (readonly)
Returns the value of attribute tickers.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def tickers @tickers end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
3 4 5 |
# File 'lib/accern/cli.rb', line 3 def token @token end |
#valid_types ⇒ Object (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_filetype ⇒ Object
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_token ⇒ Object
32 33 34 |
# File 'lib/accern/cli.rb', line 32 def ask_for_token ask_question('Please enter your API Token: ', :token) end |
#load_config ⇒ Object
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_options ⇒ Object
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 parser = OptionParser.new do |opts| opts. = 'A command line interface for the Accern API' opts.on('--init', 'Display the getting started prompts.') do [:init] = true end opts.on('--version', 'Show version') do [:version] = Accern::VERSION puts Accern::VERSION exit end opts.on("--ticker NAME", "Filters document by ticker") do |tic| [:ticker] = tic.to_s.downcase.split(',') @tickers += [:ticker] end opts.on("--ticker-file PATH", "Receives the path to a file that has tickers") do |t_path| [:ticker_path] = t_path @tickers += read_file(t_path) end = ['sp500', 'russell1000', 'russell3000', 'wilshire5000', 'barrons400', 'dow30'] opts.on("--index TYPE", "Filters document by index") do |i| [:index] = i.to_s.downcase.split(',') [:index] = sanitizes_input([:index]) raise OptionParser::InvalidArgument.new("Invalid index options") unless ([:index] - ).empty? @indexes += [: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 - ).empty? @indexes += parsed end end parser.parse!(args) @tickers = sanitizes_input(@tickers) end |
#start ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/accern/cli.rb', line 20 def start load_config if [: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 |