Example #1
0
        public static string PrintAllCrops()
        {
            string ret    = "";
            var    values = Enum.GetValues(typeof(Crop));

            foreach (Crop crop in values)
            {
                if (crop.Equals(values.GetValue(values.Length - 1)))
                {
                    ret += $"or {CropExtensions.toString(crop)}";
                }
                else
                {
                    ret += $"{CropExtensions.toString(crop)}, ";
                }
            }
            return(ret);
        }
        private string GetPrice(string messagetext, string countryId)
        {
            var reply = $"Commodity price not found. Try {CropExtensions.PrintAllCrops()}";

            if (string.IsNullOrEmpty(messagetext))
            {
                return(reply);
            }

            var list = messagetext.Split(":");

            if (list.Length > 1)
            {
                var      cropName = list[1].Replace("[", "").Replace("]", "").Trim();
                CropInfo info     = infoReader.GetCropInfo(cropName, countryId);
                reply = info.toString();
            }

            return(reply);
        }
        public CropInfo GetCropInfo(string crop, string countryId)
        {
            float  conversion = 1f;
            string currency   = DefaultCurrency;

            if (!string.IsNullOrEmpty(countryId))
            {
                var    countryInfo   = countryInfos[countryId];
                var    key           = $"USD_{countryInfo.currencyId}";
                string conversionUrl = $"http://free.currencyconverterapi.com/api/v5/convert?q={key}&compact=y";

                using (WebClient wc = new WebClient())
                {
                    var json = wc.DownloadString(conversionUrl);
                    var jobj = JObject.Parse(json);
                    conversion = float.Parse(jobj[key]["val"].ToString());
                }
                currency = countryInfo.currencySymbol;
            }

            try
            {
                var convertedCrop = crops[crop.ToLower()];
                convertedCrop.avgPrice       = convertedCrop.avgPrice * conversion;
                convertedCrop.lowPrice       = convertedCrop.lowPrice * conversion;
                convertedCrop.highPrice      = convertedCrop.highPrice * conversion;
                convertedCrop.currencySymbol = currency;
                return(convertedCrop);
            } catch (Exception e)
            {
                throw new Exception($"Prices for {crop.ToUpperInvariant()} not available. Please try {CropExtensions.PrintAllCrops()}.");
            }
        }