gaapi 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/gaapi +1 -2
- data/lib/gaapi/access_token.rb +6 -3
- data/lib/gaapi/main.rb +7 -7
- data/lib/gaapi/query.rb +2 -2
- data/lib/gaapi/response.rb +3 -1
- data/lib/gaapi/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c7d609f4d8f0c6c08af4d55afaf5f0b55f29f04ee94cd5d8b3acad4cbcbd8c2
|
4
|
+
data.tar.gz: d3c177187c100d3eeb148250be4369cfe1bcd93fc757f9549b6e25bc793a304f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e35b82cfa37eccb1381b79c074898311d2510e99afa6a19b57d83ff2a4c3e967eec516ada71c740afe8620113cfd54ee1b05910f921cdbb55cc58d2b6c3c08fd
|
7
|
+
data.tar.gz: 24806c984d60d18cec432a8f9858f6badf6882ea4ab08679256111abcbf082f83713de296662cf694237f2d8c95742d23366c834a24eacd213ece72ce676df16
|
data/bin/gaapi
CHANGED
data/lib/gaapi/access_token.rb
CHANGED
@@ -4,12 +4,15 @@ module GAAPI
|
|
4
4
|
# An access token generated from a credential file provided by Google Analystics.
|
5
5
|
# The credential file is suitable for using in applications where humans aren't
|
6
6
|
# involved, such as scheduled jobs. To obtain a credential file, follow the instructions
|
7
|
-
#
|
7
|
+
# at https://developers.google.com/identity/protocols/OAuth2ServiceAccount.
|
8
8
|
class AccessToken
|
9
9
|
# Get a new access token. The actual token is lazily-generated, so no call is
|
10
|
-
# made to Google Analytics until #token is called.
|
10
|
+
# made to Google Analytics until {#token} is called.
|
11
11
|
# @param credential_file [String] File name of a credential file provided by
|
12
|
-
# Google Analytics.
|
12
|
+
# Google Analytics. If `credential_file` is missing or `nil`, this method
|
13
|
+
# looks for credentials in `~/.gaapi/ga-api-key`.
|
14
|
+
# @raise [StandardError] Throws an exception if the credential file is readable
|
15
|
+
# or writable by other than the current user.
|
13
16
|
def initialize(credential_file = nil)
|
14
17
|
@credential_file = credential_file || File.expand_path("~/.gaapi/ga-api-key")
|
15
18
|
stat = File::Stat.new(@credential_file)
|
data/lib/gaapi/main.rb
CHANGED
@@ -7,7 +7,7 @@ module GAAPI
|
|
7
7
|
class << self
|
8
8
|
def call
|
9
9
|
begin
|
10
|
-
return
|
10
|
+
return false if (options = process_options).nil?
|
11
11
|
|
12
12
|
puts "options: #{options.inspect}" if options[:debug]
|
13
13
|
|
@@ -20,10 +20,10 @@ module GAAPI
|
|
20
20
|
puts "query: #{query.inspect}" if options[:debug]
|
21
21
|
rescue StandardError => e
|
22
22
|
$stderr.puts e.message # rubocop:disable Style/StderrPuts
|
23
|
-
return
|
23
|
+
return false
|
24
24
|
end
|
25
25
|
|
26
|
-
return
|
26
|
+
return true if options[:dry_run]
|
27
27
|
|
28
28
|
result = query.execute
|
29
29
|
puts "result: #{result.inspect}" if options[:debug]
|
@@ -32,7 +32,7 @@ module GAAPI
|
|
32
32
|
# Show the output unformatted, because we don't know what we're going
|
33
33
|
# to get back.
|
34
34
|
puts result.body
|
35
|
-
return
|
35
|
+
return false
|
36
36
|
end
|
37
37
|
|
38
38
|
case options[:output_format]
|
@@ -42,7 +42,7 @@ module GAAPI
|
|
42
42
|
puts result.pp
|
43
43
|
end
|
44
44
|
|
45
|
-
|
45
|
+
true
|
46
46
|
end
|
47
47
|
|
48
48
|
def process_options
|
@@ -76,7 +76,7 @@ module GAAPI
|
|
76
76
|
opts.on("-e",
|
77
77
|
"--end-date END_DATE",
|
78
78
|
Date,
|
79
|
-
"Report including END_DATE.") do |end_date|
|
79
|
+
"Report including END_DATE (yyyy-mm-dd).") do |end_date|
|
80
80
|
options[:end_date] = end_date
|
81
81
|
end
|
82
82
|
|
@@ -93,7 +93,7 @@ module GAAPI
|
|
93
93
|
opts.on("-s",
|
94
94
|
"--start-date START_DATE",
|
95
95
|
Date,
|
96
|
-
"Report including START_DATE.") do |start_date|
|
96
|
+
"Report including START_DATE (yyyy-mm-dd).") do |start_date|
|
97
97
|
options[:start_date] = start_date
|
98
98
|
end
|
99
99
|
end
|
data/lib/gaapi/query.rb
CHANGED
@@ -14,7 +14,7 @@ module GAAPI
|
|
14
14
|
# @param view_id [String] The view ID of the property for which to submit the
|
15
15
|
# query.
|
16
16
|
def initialize(query_string, view_id, access_token, start_date, end_date)
|
17
|
-
@access_token = access_token
|
17
|
+
@access_token = access_token.to_s
|
18
18
|
query_string = JSON.parse(query_string) unless query_string.is_a?(Hash)
|
19
19
|
@query = {}
|
20
20
|
@query["reportRequests"] = query_string["reportRequests"].map do |report_request|
|
@@ -29,7 +29,7 @@ module GAAPI
|
|
29
29
|
end
|
30
30
|
|
31
31
|
# Send the requested query to Google Analytics and return the response.
|
32
|
-
# @return [
|
32
|
+
# @return [GAAPI::Response] The response from the request.
|
33
33
|
def execute
|
34
34
|
uri = URI.parse("https://analyticsreporting.googleapis.com/v4/reports:batchGet")
|
35
35
|
https = Net::HTTP.new(uri.host, uri.port)
|
data/lib/gaapi/response.rb
CHANGED
@@ -7,11 +7,13 @@ module GAAPI
|
|
7
7
|
attr_reader :response
|
8
8
|
|
9
9
|
# Raw body of the response. Typically only used for diagnostic purposes.
|
10
|
+
# @return [String] The unformatted body of the response.
|
10
11
|
def body
|
11
12
|
response.body
|
12
13
|
end
|
13
14
|
|
14
|
-
# Raw code of the response. Typically only used for diagnostic purposes.
|
15
|
+
# Raw HTTP status code of the response. Typically only used for diagnostic purposes.
|
16
|
+
# @return [String] The HTTP response code.
|
15
17
|
def code
|
16
18
|
response.code
|
17
19
|
end
|
data/lib/gaapi/version.rb
CHANGED