public static decimal GetDolarTodayRate() { string url = "https://s3.amazonaws.com/dolartoday/data.json"; var json_data = string.Empty; using (var w = new WebClient()) { // attempt to download JSON data as a string try { json_data = w.DownloadString(url); // if string with JSON data is not empty, deserialize it to class and return its instance //return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T(); JObject rss = JObject.Parse(json_data); string strTasa = (string)rss["USD"]["transferencia"]; decimal tasa = decimal.Parse(strTasa, new NumberFormatInfo() { NumberDecimalSeparator = "." }); return(tasa); } catch (Exception ex) { LogManager.Write(ex.Message); var curRate = new CurrencyRatesRepository().GetLastRegisteredRate(); return(curRate != null ? curRate.CurrencyRate : 0); } } }
public async Task GetCurrencyRate_WhenCodeIsNotFound_ExpectException() { // Arrange var sut = new CurrencyRatesRepository(_httpClientTestDouble.Object); const string code = "xyz"; // Act await sut.GetCurrencyRateByCode(code); }
public async Task GetCurrencyRates_WhenNoOfRatesIs33_Expect33Rates() { // Arrange var sut = new CurrencyRatesRepository(_httpClientTestDouble.Object); const int expected = 33; // Act var actual = await sut.GetCurrencyRates(); // Assert Assert.AreEqual(expected, actual.Count()); }
public async Task GetCurrencyRate_WhenCodeIsEUR_ExpectEuroRate() { // Arrange var sut = new CurrencyRatesRepository(_httpClientTestDouble.Object); const string code = "eur"; const float expected = 746.39f; // Act var actual = await sut.GetCurrencyRateByCode(code); // Assert Assert.AreEqual(expected, actual.Rate); }
//static readonly IBrokersRepository repository = new BrokersRepository(); public object GetAll() { var queryValues = Request.RequestUri.ParseQueryString(); int page = Convert.ToInt32(queryValues["page"]); int start = Convert.ToInt32(queryValues["start"]); int limit = Convert.ToInt32(queryValues["limit"]); int id = Convert.ToInt32(queryValues["id"]); int orden = Convert.ToInt32(queryValues["orden"]); #region Configuramos el orden de la consulta si se obtuvo como parametro string strOrder = !string.IsNullOrWhiteSpace(queryValues["sort"]) ? queryValues["sort"] : ""; strOrder = strOrder.Replace('[', ' '); strOrder = strOrder.Replace(']', ' '); Sort sort; if (!string.IsNullOrWhiteSpace(strOrder)) { sort = JsonConvert.DeserializeObject <Sort>(strOrder); } else { sort = new Sort(); } #endregion string query = !string.IsNullOrWhiteSpace(queryValues["query"]) ? queryValues["query"] : ""; try { object json; Models.CurrRate curRate = new CurrencyRatesRepository().GetLastRegisteredRate(); decimal tasa = (curRate != null) ? curRate.CurrencyRate : 0; if (curRate == null || DateTime.Now >= curRate.CurrencyDate.AddMinutes(5)) { tasa = Utils.GetDolarTodayRate(); RegisterRate(tasa); } json = new { total = 1, data = new { USD = tasa }, success = true }; return(json); } catch (Exception ex) { LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message); object error = new { message = ex.Message }; object json = new { message = ex.Message, success = false }; return(json); } }
public DbUnit(AppContext context) { Context = context ?? new AppContext(); CurrencyRates = new CurrencyRatesRepository(Context); Currencies = new CurrenciesRepository(Context); }