Class: Sem4rCli::CliSem

Inherits:
OptParseCommand::CliMain
  • Object
show all
Defined in:
lib/sem4r_cli/cli_sem.rb

Overview

CliSem Driver

parses common options
finds command and instantiate command
passes the unparsed parameter to command
leaves control to command

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commandObject



51
52
53
# File 'lib/sem4r_cli/cli_sem.rb', line 51

def self.command
  "sem4r"
end

.descriptionObject



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

.versionObject



62
63
64
# File 'lib/sem4r_cli/cli_sem.rb', line 62

def self.version
  Sem4r::VERSION
end

Instance Method Details

#accountObject

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 
  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
     = @adwords..client_accounts.find { |c| c.client_email =~ /#{@options.client_email}/ }
    if .nil?
      puts "client account not found"
    else
      puts "using client #{.client_email}"
    end
  else
     = @adwords.
  end
  @account = 
end

#adwordsObject

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
  #
  options = {}
  if @options.config_name
    options[:config_file] = @options.config_name
  end
  @adwords = Sem4r::Adwords.new(@options.profile, options)
  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.dump_soap_options(o)
  end
  if @options.dump_soap_to_directory
    dir = @options.dump_soap_to_directory
    o   = {:directory => dir, :format => true}
    @adwords.dump_soap_options(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(options)
  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(options)
  parser = super(options)

  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|
    options.verbose = v
  end

  parser.on("-q", "--quiet", "quiet mode as --no-verbose") do |v|
    options.verbose = false
  end

  #
  # Logging
  #
  parser.separator ""
  parser.separator "logging options: "
  parser.separator ""

  parser.on("--log FILE", "log sem4r messages to file") do |v|
    options.logger = v
  end

  parser.on("--dump-file FILE", "dump soap conversation to file") do |v|
    options.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|
    options.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|
    options.config_name = config
  end

  parser.on("-p", "--profile PROFILE", "select profile (default is sandbox)") do |profile|
    options.profile = profile
  end

  # email
  parser.on("--email EMAIL",
            "email of adwords account") do |email|
    options.email = email
  end

  # password
  parser.on("-a", "--ask-password",
            "ask password on terminal") do
    options.ask_password = true
  end

  parser.on("--password PASSWORD",
            "password of adwords account") do |password|
    options.password = password if password
  end

  # developer token
  parser.on("--token TOKEN",
            "developer token to access adwords api") do |token|
    options.developer_token = token
  end

  # client account
  parser.on("-c", "--client EMAIL",
            "email for client account") do |email|
    options.client_email = email
  end
  parser.separator ""
  parser
end

#read_password_from_terminalObject



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