Class: PromptCloudApiArgParser

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePromptCloudApiArgParser

Returns a new instance of PromptCloudApiArgParser.


284
285
286
# File 'lib/promptcloud_data_api.rb', line 284

def initialize()
	super
end

Class Method Details

.parse(args, defaults = {}, mandatory = []) ⇒ Object


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/promptcloud_data_api.rb', line 323

def self.parse(args,defaults={},mandatory=[])
	options= {}
	options= options.merge(defaults)
	opts=OptionParser.new do |opts|
		opts.banner = "Usage: #{$0} [options] "
		
		opts.on("-v","--api_version VERSION",String, "to get data from different api version(available versions are v1 and v2, the defalut version is v1)") do |v|
			options[:api_version] = v
		end

		opts.on("-u","--user USER",String, "data api user id(provided by PromptCloud)") do |v|
			options[:user] = v
		end

		opts.on("-p","--pass PASSWORD",String, "data api password(provised by PromptCloud, used for api v1)") do |v|
			options[:pass] = v
		end

		opts.on("-k","--client_auth_key AUTHKEY",String, "data api client auth key(provided by PromptCloud, used for api v2)") do |v|
			options[:client_auth_key] = v
		end
		
		opts.on("-i","--perform_initial_setup", "to perform initial setup") do |v|
			options[:perform_initial_setup] = v
		end
		
		opts.on("--display_info", "to display config info") do |v|
			options[:display_info] = v
		end

		opts.on("--apiconf APICONFPATH",String, "to override the config file path(config file stores information like client_id, password, client_auth_key, downloadir etc)") do |v|
			options[:apiconf] = v
		end

		opts.on("--download_dir DOWNLOAD_DIRECTORY",String, "to override the download directory(which contains downloaded data files)") do |v|
			options[:download_dir] = v
		end

		opts.on("--promptcloudhome PROMPTCLOUDHOME",String, "to override the promptcloudhome dir(~/promptcloud)") do |v|
			options[:promptcloudhome] = v
		end

		opts.on("-t","--timestamp TIMESTAMP",Integer, "to query promptcloud api for files newer than or equal to given timestamp") do |v|
			options[:timestamp] = v
		end
		
		opts.on("--days DAYS",Integer, "to download the data of last few days") do |v|
			options[:days] = v
		end
		
		opts.on("--hours DAYS",Integer, "to download the data of last few hours") do |v|
			options[:hours] = v
		end
		
		opts.on("--minutes MINUTES",Integer, "to download the data last few minutes") do |v|
			options[:minutes] = v
		end
		
		opts.on("--queried_timestamp_file queried TIMESTAMPFILE",String, "to override the last timestamp file(contains last queried timestamp)") do |v|
			options[:queried_timestamp_file] = v
		end

		opts.on("--category CATEGORY ",String, "to query promptcloud api for files of the given category(if files of different verticals are placed in different directory under client's parent directory, then files of specific directory can be obtained by specifying that directory name in category option)") do |v|
			options[:category] = v
		end
		
		opts.on("--site SITE_NAME",String, "to query promptcloud api for files of the given site") do |v|
			options[:site] = v
		end
		
		opts.on("--loop", "download new data files and keep looking for new one(i.e it doesn't exit, if no new feed is found it will sleep, minimun sleep time is 10 secs and max sleep time is 300 secs)") do |v|
			options[:loop] = v
		end

		opts.on("--noloop", "download new data files and and exit, this is the default behaviour") do |v|
			options[:noloop] = v
		end

		opts.on("--bcp", "to download data from PromptCloud backup server(high availability server, should use if main data api server is unreachable)") do |v|
			options[:bcp] = v
		end
		
		opts.on_tail("-h", "--help", "Show this message") do
			puts opts
			usage_notes
			exit(-1)
		end
	end

	opts.parse!(args)
	if validate(options,mandatory)
		return options
	else
		$stderr.puts "#{[email protected]} Invalid/no args, see use -h command for help"
		exit(-1)
	end
end

.usage_notesObject


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/promptcloud_data_api.rb', line 298

def self.usage_notes
	script_name=$0
	$stderr.puts <<END
Example :
# Initial setup(default config)
       ruby #{script_name} --perform_initial_setup --user <username> --pass <password>                              # API v1 requires valid userid and password
ruby #{script_name} --api_version v2  --perform_initial_setup --user <username> --client_auth_kay <auth key> # API v2 requires valid user id and authentication key

# Download data 
ruby #{script_name}                               # to download data of last 2 days (default)
ruby #{script_name} --timestamp <timestamp>       # to use of timestamp param
ruby #{script_name} --site <test_site> --days 7   # to download data of the site test_site uploaded in last 7 days
ruby #{script_name} --category blog --hours 10    # to download data of the category blog uploaded in last 10 hours
ruby #{script_name} --minutes 20                  # to download data uploaded in last 20 minutes
ruby #{script_name} --bcp                         # to download data from bcp(PromptCloud backup server)
ruby #{script_name} --loop                        # to download data continuously, it will automatically check our API for new data

# To use own config
ruby #{script_name} --apiconf <apiconf file pull path>             # to override apiconf file	
ruby #{script_name} --download_dir <download directory full path>  # to override download directory 
ruby #{script_name} --promptcloudhome <promptcloudhome full path>  # to override promptcloudhome home
END
end

.validate(options, mandatory) ⇒ Object


288
289
290
291
292
293
294
295
296
# File 'lib/promptcloud_data_api.rb', line 288

def self.validate(options,mandatory)
	options[:api_version] = "v1" if not options[:api_version] # default version
	options[:api_version] = options[:api_version].downcase
	if not ["v1","v2"].include? options[:api_version]
		$stderr.puts "#{options[:api_version]} is not a valid api version. Please pass v1 or v2.(v1 is the default)"
		return false
	end
	return true
end