48
49
50
51
52
53
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
|
# File 'lib/gaapi/main.rb', line 48
def process_options
options = {}
credential_file = File.join(Dir.home, ".gaapi/ga-api-key")
parsed_options = OptionParser.new do |opts|
opts.banner = "Usage: [options] VIEW_ID"
opts.accept(Date)
opts.on("-a TOKEN",
"--access-token TOKEN",
"An access token obtained from https://developers.google.com/oauthplayground.") do |access_token|
options[:access_token] = access_token
end
opts.on("--csv",
"Output result as a csv file.") do
options[:output_format] = :csv
end
opts.on("-c CREDENTIALS",
"--credentials CREDENTIALS",
"Location of the credentials file. Default: `#{credential_file}`.") do |credentials|
credential_file = credentials
end
opts.on("-d", "--debug", "Print debugging information.") do
options[:debug] = true
end
opts.on("-e",
"--end-date END_DATE",
Date,
"Report including END_DATE (yyyy-mm-dd).") do |end_date|
options[:end_date] = end_date
end
opts.on("-n", "--dry-run", "Don't actually send the query to Google.") do
options[:dry_run] = true
end
opts.on("-q QUERYFILE",
"--query-file QUERYFILE",
"File containing the query. Default STDIN.") do |query_file|
options[:query_file] = File.open(query_file)
end
opts.on("-s",
"--start-date START_DATE",
Date,
"Report including START_DATE (yyyy-mm-dd).") do |start_date|
options[:start_date] = start_date
end
end
parsed_options.parse!
unless ARGV.size == 1
$stderr.puts("gaapi: You must provide a view ID.\n" + parsed_options.to_s) return nil
end
options[:view_id] = ARGV[0]
options[:access_token] ||= GAAPI::AccessToken.new(credential_file)
if options[:end_date].nil? && !options[:start_date].nil?
options[:end_date] = options[:start_date]
elsif options[:start_date].nil? && !options[:end_date].nil?
options[:start_date] = options[:end_date]
elsif options[:start_date].nil? && options[:end_date].nil?
options[:end_date] = options[:start_date] = (Date.today - 1).to_s
end
options
end
|