예제 #1
0
 /// <summary>
 /// Converts the string representation of the sort options. A return value indicates
 /// whether the conversion succeeded.
 /// </summary>
 /// <param name="str">A string containing the sort options to convert.</param>
 /// <param name="options">The converted options if successful.</param>
 /// <returns><var>true</var> if str was converted successfully; otherwise, <var>false</var>.</returns>
 public static bool TryParse(string str, out AnalyticsSortOptions options)
 {
     try {
         options = Parse(str);
         return(true);
     } catch {
         options = null;
         return(false);
     }
 }
예제 #2
0
        /// <summary>
        /// Converts the string representation of the sort options.
        /// </summary>
        /// <param name="str">A string containing the sort options to convert.</param>
        /// <returns>The converted sort options.</returns>
        public static AnalyticsSortOptions Parse(string str)
        {
            // If the strign is NULL, we also return NULL
            if (str == null)
            {
                return(null);
            }

            AnalyticsSortOptions options = new AnalyticsSortOptions();

            // The sort fields are separated by commas
            string[] b = str.Split(new [] { "," }, StringSplitOptions.RemoveEmptyEntries);

            for (int j = 0; j < b.Length; j++)
            {
                string piece = b[j];

                AnalyticsSortOrder order = AnalyticsSortOrder.Ascending;
                if (piece.StartsWith("-"))
                {
                    order = AnalyticsSortOrder.Descending;
                    piece = piece.Substring(1);
                }

                // Does the piece have the characteristics of a metric or dimension?
                Match m = Regex.Match(piece, "^(ga:[a-zA-Z]+)$");
                if (!m.Success)
                {
                    throw new Exception("Unable to parse metric or dimension '" + piece + "'");
                }

                AnalyticsMetric    metric;
                AnalyticsDimension dimension;
                if (AnalyticsMetric.TryParse(m.Groups[1].Value, out metric))
                {
                    options.Add(metric, order);
                }
                else if (AnalyticsDimension.TryParse(m.Groups[1].Value, out dimension))
                {
                    options.Add(dimension, order);
                }
                else
                {
                    // Currently the parsing will only work if the specified dimension or
                    // metric name matches a defined constant, so if Google adds a new
                    // dimension or metric, the constants must be updated before the parsing
                    // will work. I'm not sure how often Google adds new dimensions or metric,
                    // so perhaps this isn't a problem
                    throw new Exception("Unable to parse metric or dimension '" + m.Groups[1].Value + "'");
                }
            }

            return(options);
        }