Class: SparkApi::CLI::ConsoleCLI

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

Constant Summary collapse

OPTIONS_ENV =
{
  :endpoint => "API_ENDPOINT",
  :no_verify => "NO_VERIFY",
  # OAUTH2 Options
  :access_uri  => "ACCESS_URI",
  :authorization_uri => "AUTHORIZATION_URI",
  :redirect_uri => "REDIRECT_URI",
  :code => "CODE",
  :username=> "USERNAME",
  :password=> "PASSWORD",
  :client_id=> "CLIENT_ID",
  :client_secret=> "CLIENT_SECRET",
  # API AUTH Options
  :api_key => "API_KEY", 
  :api_secret => "API_SECRET",
  :api_user => "API_USER",
  # OTHER
  :debug=> "DEBUG",
  :console => "SPARK_API_CONSOLE"  # not a public option, meant to distinguish bin/spark_api and script/console
}

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



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

def self.execute(stdout, arguments=[])
  options = setup_options(stdout,arguments)
  libs =  " -r irb/completion"
  # Perhaps use a console_lib to store any extra methods I may want available in the cosole
  libs << (options[:oauth2] ? setup_oauth2 : setup_api_auth)
  
  bundler = (options[:console] ? "bundle exec" : "")  
  cmd = "#{export_env(options)} #{bundler} #{irb} #{libs} --simple-prompt"
  puts "Loading spark_api gem..."
  exec "#{cmd}"
end

.export_env(options) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/spark_api/cli.rb', line 158

def self.export_env(options)
  run_env = ""
  OPTIONS_ENV.each do |k,v|
    run_env << " #{v}=\"#{options[k]}\"" unless options[k].nil?
  end
  run_env
end

.irbObject



49
50
51
# File 'lib/spark_api/cli.rb', line 49

def self.irb()
  RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
end

.parse_file_options(file) ⇒ Object



167
168
169
170
# File 'lib/spark_api/cli.rb', line 167

def self.parse_file_options(file)
  yaml = SparkApi::Configuration::YamlConfig.new(file)
  return {:oauth2 => yaml.oauth2}.merge(yaml.client_keys.merge(yaml.oauth2_keys))
end

.setup_api_authObject



150
151
152
# File 'lib/spark_api/cli.rb', line 150

def self.setup_api_auth
  " -r #{File.dirname(__FILE__) + '/../../lib/spark_api/cli/api_auth.rb'}"
end

.setup_oauth2Object



154
155
156
# File 'lib/spark_api/cli.rb', line 154

def self.setup_oauth2
  " -r #{File.dirname(__FILE__) + '/../../lib/spark_api/cli/oauth2.rb'}"
end

.setup_options(stdout, arguments) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/spark_api/cli.rb', line 54

def self.setup_options(stdout,arguments)
  env_options = {
    :oauth2            => false,
    :endpoint          => ENV[OPTIONS_ENV[:endpoint]],
    # OAUTH2 Options
    :access_uri  => ENV[OPTIONS_ENV[:access_uri]],
    :authorization_uri  => ENV[OPTIONS_ENV[:authorization_uri]],
    :redirect_uri  => ENV[OPTIONS_ENV[:redirect_uri]],
    :code  => ENV[OPTIONS_ENV[:code]],
    :username=> ENV[OPTIONS_ENV[:username]],
    :password=> ENV[OPTIONS_ENV[:password]],
    :client_id=> ENV[OPTIONS_ENV[:client_id]],
    :client_secret=> ENV[OPTIONS_ENV[:client_secret]],
    # API AUTH Options
    :api_key => ENV[OPTIONS_ENV[:api_key]], 
    :api_secret => ENV[OPTIONS_ENV[:api_secret]],
    :api_user => ENV[OPTIONS_ENV[:api_user]],
    :no_verify => ENV.fetch(OPTIONS_ENV[:no_verify], false),
    :console => ENV[OPTIONS_ENV[:console]]
  }
  cli_options = {}
  file_options = {}
  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      #{version}
      SparkApi Client Console - http://sparkplatform.com/docs/overview/api
      
      Usage: #{File.basename($0)} [options]
      
      Environment Variables: some options (as indicated below), will default to values of keys set in the environment. 
  
      Options are:
    BANNER
    opts.separator ""
    opts.on("-e","--endpoint ENDPOINT",
            "URI of the API.",
            "Default: ENV['#{OPTIONS_ENV[:endpoint]}'] or #{SparkApi::Configuration::DEFAULT_ENDPOINT}") { |arg| cli_options[:endpoint] = arg }

    # OAUTH2
    opts.on("-o","--oauth2",
            "Run the API using OAuth2 credentials.  The client defaults to using the Spark API authentication mode for access. ",
            "See http://sparkplatform.com/docs/authentication/authentication for more information on authentication types.",
            "Default: false") { |arg| cli_options[:oauth2] = arg }
    opts.on("--client_id CLIENT_ID",
            "OAuth2 client id",
            "Default: ENV['#{OPTIONS_ENV[:client_id]}']") { |arg| cli_options[:client_id] = arg }
    opts.on("--client_secret CLIENT_SECRET",
            "OAuth2 client secret",
            "Default: ENV['#{OPTIONS_ENV[:client_secret]}']") { |arg| cli_options[:client_secret] = arg }
    opts.on("-u","--username USERNAME",
            "OAuth2 username",
            "Default: ENV['#{OPTIONS_ENV[:username]}']") { |arg| cli_options[:username] = arg }
    opts.on("-p","--password PASSWORD",
            "OAuth2 password",
            "Default: ENV['#{OPTIONS_ENV[:password]}']") { |arg| cli_options[:password] = arg }
    opts.on("--access_uri ACCESS_URI",
            "OAuth2 path for granting access to the application using one of the supported grant types.",
            "Default: ENV['#{OPTIONS_ENV[:access_uri]}'] or #{SparkApi::Configuration::DEFAULT_ACCESS_URI}") { |arg| cli_options[:access_uri] = arg }
    opts.on("--redirect_uri REDIRECT_URI",
            "OAuth2 application redirect for the client id. This needs to match whatever value is saved for the application's client_id",
            "Default: ENV['#{OPTIONS_ENV[:redirect_uri]}'] or #{SparkApi::Configuration::DEFAULT_REDIRECT_URI}") { |arg| cli_options[:redirect_uri] = arg }
    opts.on("--authorization_uri AUTHORIZATION_URI",
            "OAuth2 authorization endpoint for a user. This is where the user should go to sign in and authorize client id.",
            "Default: ENV['#{OPTIONS_ENV[:authorization_uri]}'] or #{SparkApi::Configuration::DEFAULT_AUTH_ENDPOINT}") { |arg| cli_options[:authorization_uri] = arg }
    opts.on("--code CODE",
            "OAuth2 authorization code used for granting application access to the API for a user") { |arg| cli_options[:code] = arg }
    
    # API AUTH
    opts.on("--api_key API_KEY",
            "Authentication key for running the api using the default api authentication",
            "Default: ENV['#{OPTIONS_ENV[:api_key]}']") { |arg| cli_options[:api_key] = arg }
    opts.on("--api_secret API_SECRET",
            "API secret for the api key",
             "Default: ENV['#{OPTIONS_ENV[:api_secret]}']") { |arg| cli_options[:api_secret] = arg }
    opts.on("--api_user API_USER",
            "ID of the Spark user to run the client as.",
            "Default: ENV['#{OPTIONS_ENV[:api_user]}']") { |arg| cli_options[:api_user] = arg }
    
    # General           
    opts.on("-f", "--file FILE",
            "Load configuration for yaml file.") { |arg| file_options = parse_file_options(arg) }
    opts.on("--no_verify",
            "Disable SSL Certificate verification. This is useful for development servers.") { |arg| cli_options[:no_verify] = arg }
    opts.on("-d", "--debug",
            "Show detailed request logging information.") { |arg| cli_options[:debug] = arg }
    opts.on("-v", "--version",
            "Show client version.") { stdout.puts version; exit }
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)
  
  end
  options = env_options.merge(file_options.merge(cli_options))
  return options
end

.versionObject



172
173
174
# File 'lib/spark_api/cli.rb', line 172

def self.version
  "SparkApi v#{SparkApi::VERSION}"
end