forked from Tatvic/RGoogleAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToUri.R
55 lines (52 loc) · 2.14 KB
/
ToUri.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#' Returns the URI constructed from the parameter settings. This also
#' URI-encodes all the values in each query parameter.
#'
#' @param query.builder Name of the Object of the Query Builder Class
#'
#' @param token Token object containing the OAuth2.0 Authentication details
#'
#' @keywords internal
#' @return
#' A full URI that can be used with the Google Analytics API.
ToUri <- function(query.builder,token) {
query <- c("start.date" = query.builder$start.date(),
"end.date" = query.builder$end.date(),
"dimensions" = query.builder$dimensions(),
"metrics" = query.builder$metrics(),
"segment" = query.builder$segment(),
"sort" = query.builder$sort(),
"filters" = query.builder$filters(),
"max.results" = query.builder$max.results(),
"start.index" = query.builder$start.index(),
"table.id" = query.builder$table.id(),
"access_token" = token$credentials$access_token)
uri <- "https://www.googleapis.com/analytics/v3/data/ga?"
for (name in names(query)) {
uri.name <- switch(name,
start.date = "start-date",
end.date = "end-date",
dimensions = "dimensions",
metrics = "metrics",
segment = "segment",
sort = "sort",
filters = "filters",
max.results = "max-results",
start.index = "start-index",
table.id = "ids",
access_token = "access_token")
if (!is.null(uri.name)) {
uri <- paste(uri,
URLencode(uri.name, reserved = TRUE),
"=",
URLencode(query[[name]], reserved = TRUE),
"&",
sep = "",
collapse = "")
}
}
# remove the last '&' that joins the query parameters together.
uri <- sub("&$", "", uri)
# remove any spaces that got added in from bad input.
uri <- gsub("\\s", "", uri)
return(uri)
}