Class: SparkApi::CLI::ConsoleCLI

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

Constant Summary collapse

OPTIONS_ENV =
{
  :endpoint => "API_ENDPOINT",
  :ssl_verify => "SSL_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",
  :middleware => "SPARK_MIDDLEWARE",
  :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



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

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



163
164
165
166
167
168
169
# File 'lib/spark_api/cli.rb', line 163

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



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

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

.parse_file_options(file) ⇒ Object



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

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



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

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

.setup_oauth2Object



159
160
161
# File 'lib/spark_api/cli.rb', line 159

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

.setup_options(stdout, arguments) ⇒ Object



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
149
150
151
152
153
# File 'lib/spark_api/cli.rb', line 55

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]],
    :ssl_verify => ENV.fetch(OPTIONS_ENV[:ssl_verify], true),
    :console => ENV[OPTIONS_ENV[:console]],
    :middleware => ENV[OPTIONS_ENV[:middleware]]
  }
  cli_options = {}
  file_options = {}
  parser = OptionParser.new do |opts|
    opts.banner = "      \#{version}\n      SparkApi Client Console - http://sparkplatform.com/docs/overview/api\n      \n      Usage: \#{File.basename($0)} [options]\n      \n      Environment Variables: some options (as indicated below), will default to values of keys set in the environment. \n  \n      Options are:\n    BANNER\n    opts.separator \"\"\n    opts.on(\"-e\",\"--endpoint ENDPOINT\",\n            \"URI of the API.\",\n            \"Default: ENV['\#{OPTIONS_ENV[:endpoint]}'] or \#{SparkApi::Configuration::DEFAULT_ENDPOINT}\") { |arg| cli_options[:endpoint] = arg }\n\n    # OAUTH2\n    opts.on(\"-o\",\"--oauth2\",\n            \"Run the API using OAuth2 credentials.  The client defaults to using the Spark API authentication mode for access. \",\n            \"See http://sparkplatform.com/docs/authentication/authentication for more information on authentication types.\",\n            \"Default: false\") { |arg| cli_options[:oauth2] = arg }\n    opts.on(\"--client_id CLIENT_ID\",\n            \"OAuth2 client id\",\n            \"Default: ENV['\#{OPTIONS_ENV[:client_id]}']\") { |arg| cli_options[:client_id] = arg }\n    opts.on(\"--client_secret CLIENT_SECRET\",\n            \"OAuth2 client secret\",\n            \"Default: ENV['\#{OPTIONS_ENV[:client_secret]}']\") { |arg| cli_options[:client_secret] = arg }\n    opts.on(\"-u\",\"--username USERNAME\",\n            \"OAuth2 username\",\n            \"Default: ENV['\#{OPTIONS_ENV[:username]}']\") { |arg| cli_options[:username] = arg }\n    opts.on(\"-p\",\"--password PASSWORD\",\n            \"OAuth2 password\",\n            \"Default: ENV['\#{OPTIONS_ENV[:password]}']\") { |arg| cli_options[:password] = arg }\n    opts.on(\"--access_uri ACCESS_URI\",\n            \"OAuth2 path for granting access to the application using one of the supported grant types.\",\n            \"Default: ENV['\#{OPTIONS_ENV[:access_uri]}'] or \#{SparkApi::Configuration::DEFAULT_ACCESS_URI}\") { |arg| cli_options[:access_uri] = arg }\n    opts.on(\"--redirect_uri REDIRECT_URI\",\n            \"OAuth2 application redirect for the client id. This needs to match whatever value is saved for the application's client_id\",\n            \"Default: ENV['\#{OPTIONS_ENV[:redirect_uri]}'] or \#{SparkApi::Configuration::DEFAULT_REDIRECT_URI}\") { |arg| cli_options[:redirect_uri] = arg }\n    opts.on(\"--authorization_uri AUTHORIZATION_URI\",\n            \"OAuth2 authorization endpoint for a user. This is where the user should go to sign in and authorize client id.\",\n            \"Default: ENV['\#{OPTIONS_ENV[:authorization_uri]}'] or \#{SparkApi::Configuration::DEFAULT_AUTH_ENDPOINT}\") { |arg| cli_options[:authorization_uri] = arg }\n    opts.on(\"--code CODE\",\n            \"OAuth2 authorization code used for granting application access to the API for a user\") { |arg| cli_options[:code] = arg }\n    \n    # API AUTH\n    opts.on(\"--api_key API_KEY\",\n            \"Authentication key for running the api using the default api authentication\",\n            \"Default: ENV['\#{OPTIONS_ENV[:api_key]}']\") { |arg| cli_options[:api_key] = arg }\n    opts.on(\"--api_secret API_SECRET\",\n            \"API secret for the api key\",\n             \"Default: ENV['\#{OPTIONS_ENV[:api_secret]}']\") { |arg| cli_options[:api_secret] = arg }\n    opts.on(\"--api_user API_USER\",\n            \"ID of the Spark user to run the client as.\",\n            \"Default: ENV['\#{OPTIONS_ENV[:api_user]}']\") { |arg| cli_options[:api_user] = arg }\n    opts.on(\"--middleware SPARK_MIDDLEWARE\",\n            \"spark_api for accessing spark, reso_api for accessing reso adapter\",\n            \"Default: spark_api\") { |arg| cli_options[:middleware] = arg }\n\n    # General           \n    opts.on(\"-f\", \"--file FILE\",\n            \"Load configuration for yaml file.\") { |arg| file_options = parse_file_options(arg) }\n    opts.on(\"--no_verify\",\n            \"Disable SSL Certificate verification. This is useful for development servers.\") { |arg| cli_options[:ssl_verify] = !arg }\n    opts.on(\"-d\", \"--debug\",\n            \"Show detailed request logging information.\") { |arg| cli_options[:debug] = arg }\n    opts.on(\"-v\", \"--version\",\n            \"Show client version.\") { stdout.puts version; exit }\n    opts.on(\"-h\", \"--help\",\n            \"Show this help message.\") { stdout.puts opts; exit }\n    opts.parse!(arguments)\n  \n  end\n  options = env_options.merge(file_options.merge(cli_options))\n  return options\nend\n".gsub(/^          /,'')

.versionObject



177
178
179
# File 'lib/spark_api/cli.rb', line 177

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