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.



6
7
8
9
10
11
12
13
14
15
# File 'lib/accern/cli.rb', line 6

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
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

#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

#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



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

def ask_for_filetype
  ask_question('Please enter the file type (JSON or CSV): ', :filetype)
  valid_filetype?
end

#ask_for_tokenObject



29
30
31
# File 'lib/accern/cli.rb', line 29

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

#load_configObject



38
39
40
41
42
43
44
45
46
# File 'lib/accern/cli.rb', line 38

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/accern/cli.rb', line 48

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
  end

  parser.parse!(args)
end

#startObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/accern/cli.rb', line 17

def start
  load_config
  parse_options

  if options[:init]
    ask_for_info
  else
    feed.new(token: token, format: filetype.to_sym)
        .download_loop('./feed.jsonl')
  end
end