示例#1
0
        /// <summary>Retreives a joke from the supplied URL and name replaces when given a true argument</summary>
        /// <param name="useFakeName">A boolean to determine whether to call GetName()</param>
        private static async Task GetRandomJoke(Boolean withFakeName = false)
        {
            string formattedJoke = await JsonFeed.GetRandomJoke();

            if (withFakeName)
            {
                dynamic response = await GetName();

                // Replace first and last name separately as some jokes use first and/or last name separately
                // ex. They don't call him Chuck.... or ... Mr. Norris.
                string firstName = response["name"];
                string lastName  = response["surname"];

                // Replace does not play nice with direct object property
                formattedJoke = Regex.Replace(formattedJoke, "(?i)Chuck", firstName);
                formattedJoke = Regex.Replace(formattedJoke, "(?i)Norris", lastName);

                // Accommodate for female names, and his/her gender designation in the joke
                if (response["gender"] == "female")
                {
                    formattedJoke = formattedJoke.Replace(" He ", " She ");
                    formattedJoke = formattedJoke.Replace(" he ", " she ");
                    formattedJoke = formattedJoke.Replace(" His ", " Her ");
                    formattedJoke = formattedJoke.Replace(" his ", " her ");
                    formattedJoke = formattedJoke.Replace(" Him ", " Her ");
                    formattedJoke = formattedJoke.Replace(" him ", " her ");
                }
            }

            joke = formattedJoke;
        }