Class: Sem4rCli::CliSem
- Inherits:
-
OptParseCommand::CliMain
- Object
- OptParseCommand::CliMain
- Sem4rCli::CliSem
- Defined in:
- lib/sem4r_cli/cli_sem.rb
Overview
CliSem Driver
parses common
finds command and instantiate command
passes the unparsed parameter to command
leaves control to command
Class Method Summary collapse
Instance Method Summary collapse
-
#account ⇒ Object
select account according to the command line arguments return [Sem4r::Account].
-
#adwords ⇒ Object
initialize adwords according to command line options return [Sem4r::Adwords].
- #defaults(options) ⇒ Object
- #option_parser(options) ⇒ Object
- #read_password_from_terminal ⇒ Object
Class Method Details
.command ⇒ Object
51 52 53 |
# File 'lib/sem4r_cli/cli_sem.rb', line 51 def self.command "sem4r" end |
.description ⇒ Object
55 56 57 58 59 60 |
# File 'lib/sem4r_cli/cli_sem.rb', line 55 def self.description "Sem is a simple command line interface using the sem4r library.\n" + "It's alpha software, please don't use in production or use it at your risk!\n" + "Code https://github.com/sem4r/sem4r. Feedback to [email protected]\n" + "Further information: http://www.sem4r.com\n\n" end |
Instance Method Details
#account ⇒ Object
select account according to the command line arguments return [Sem4r::Account]
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/sem4r_cli/cli_sem.rb', line 239 def account return @account if @account adwords # initialize sem4r lib adwords if @options.ask_password or !@adwords.has_password? pwd = read_password_from_terminal @adwords.password = pwd @adwords.save_passwords end # # select account for command # if @options.client_email account = @adwords.account.client_accounts.find { |c| c.client_email =~ /#{@options.client_email}/ } if account.nil? puts "client account not found" else puts "using client #{account.client_email}" end else account = @adwords.account end @account = account end |
#adwords ⇒ Object
initialize adwords according to command line options return [Sem4r::Adwords]
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/sem4r_cli/cli_sem.rb', line 173 def adwords return @adwords if @adwords # # select profile # = {} if @options.config_name [:config_file] = @options.config_name end @adwords = Sem4r::Adwords.new(@options.profile, ) if @options.verbose puts "using #{@adwords.profile} profile" puts "config file is #{@adwords.config_file}" end # # Extracts dump soap options # if @options.default_logging config_dir = File.join(ENV['HOME'], ".sem4r") unless File.exists?(config_dir) puts "Directory #{config_dir} not exists" FileUtils.mkdir(config_dir) end dir = File.join(config_dir, Time.new.strftime("%Y%m%d-soap-dump")) @options.dump_soap_to_directory = dir file = File.join(config_dir, Time.new.strftime("%Y%m%d-sem4r-log")) @options.logger = file end if @options.dump_soap_to_file filename = @options.dump_soap_to_file o = {:file => filename} @adwords.(o) end if @options.dump_soap_to_directory dir = @options.dump_soap_to_directory o = {:directory => dir, :format => true} @adwords.(o) end if @options.verbose and adwords.dump_soap? puts "Logging soap conversation to '#{adwords.dump_soap_where}'" end if !@adwords.dump_soap? puts "it is highly recommended to activate the dump soap log" end # # Extracts log options # # filename = File.join( tmp_dirname, File.basename(example_file).sub(/\.rb$/, ".log") ) if @options.logger # filename = "sem.log" @adwords.logger = @options.logger end if @adwords.logger puts "Logger is active" if @options.verbose end @adwords end |
#defaults(options) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sem4r_cli/cli_sem.rb', line 66 def defaults() OpenStruct.new({:verbose => true, :force => false, :default_logging => true, # :dump_soap_to_file => true, # :dump_soap_to_directory => true, :profile => 'sandbox', :ask_password => false }) end |
#option_parser(options) ⇒ Object
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 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/sem4r_cli/cli_sem.rb', line 77 def option_parser() parser = super() parser.separator "execute COMMAND to an adwords account using google adwords api" parser.separator "To view help and options for a particular command, use 'sem COMMAND -h'" # # common options # parser.on("-v", "--[no-]verbose", "run verbosely") do |v| .verbose = v end parser.on("-q", "--quiet", "quiet mode as --no-verbose") do |v| .verbose = false end # # Logging # parser.separator "" parser.separator "logging options: " parser.separator "" parser.on("--log FILE", "log sem4r messages to file") do |v| .logger = v end parser.on("--dump-file FILE", "dump soap conversation to file") do |v| .dump_soap_to_file = v end str = "dump soap conversation to directory: each \n" str << (" " * 37) + "request/response is in a single file" parser.on("--dump-dir DIRECTORY", str) do |v| .dump_soap_to_directory = v end # # Profile - Configuration # parser.separator "" parser.separator "profile and credentials options: " parser.separator " credentials options overwrite profile attributes" parser.separator "" str = "file where profiles are defined\n" str << (" " * 37) + "(default $HOME/.sem4r/sem4r.yaml)" parser.on("--config CONFIG", str) do |config| .config_name = config end parser.on("-p", "--profile PROFILE", "select profile (default is sandbox)") do |profile| .profile = profile end # email parser.on("--email EMAIL", "email of adwords account") do |email| .email = email end # password parser.on("-a", "--ask-password", "ask password on terminal") do .ask_password = true end parser.on("--password PASSWORD", "password of adwords account") do |password| .password = password if password end # developer token parser.on("--token TOKEN", "developer token to access adwords api") do |token| .developer_token = token end # client account parser.on("-c", "--client EMAIL", "email for client account") do |email| .client_email = email end parser.separator "" parser end |
#read_password_from_terminal ⇒ Object
165 166 167 |
# File 'lib/sem4r_cli/cli_sem.rb', line 165 def read_password_from_terminal ask("Enter your password: ") { |q| q.echo = "x" } end |