Exemplo n.º 1
0
        /// <summary>
        /// Connect to Finance!Yahoo site and gets the info
        /// </summary>
        /// <param name="url">Finance!Yahoo URL with actual currencies as params</param>
        /// <exception cref="System.UnauthorizedAccessException">Thrown when the service is unavailable</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown when supplied currencies are not supported</exception>
        /// <returns>List of CurrencyData objects</returns>
        private List<CurrencyData> GetData(Uri url)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.Proxy = _proxy;
            req.Timeout = _timeout;
            req.ReadWriteTimeout = _readWriteTimeout;

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            try
            {
                Stream respStream = resp.GetResponseStream();
                try
                {
                    StreamReader respReader = new StreamReader(respStream, Encoding.ASCII);
                    try
                    {
                        char[] trimchars = new char[] { '"', '\'' };

                        List<CurrencyData> Result = new List<CurrencyData>();

                        while(respReader.Peek() > 0)
                        {
                            string[] fields = respReader.ReadLine().Split(new char[] { ',' });

                            if((fields.Length == 0) || (fields[(int)DataFieldNames.TradeDate].Trim(trimchars).Equals(NA, StringComparison.InvariantCultureIgnoreCase)))
                                throw new UnauthorizedAccessException("The currency data is not available!");

                            if(fields[(int)DataFieldNames.CurrencyCodes].Trim(trimchars).Equals(NA, StringComparison.InvariantCultureIgnoreCase))
                                throw new ArgumentOutOfRangeException("These Currencies are not supported!");

                            string from = fields[(int)DataFieldNames.CurrencyCodes].Trim(trimchars).Substring(0, 3);
                            string to = fields[(int)DataFieldNames.CurrencyCodes].Trim(trimchars).Substring(3, 3);

                            CurrencyData data = new CurrencyData(from, to);

                            CultureInfo culture = new CultureInfo("en-US", false);
                            if(fields[(int)DataFieldNames.CurrentRate].Equals(NA, StringComparison.InvariantCultureIgnoreCase))
                                data.Rate = 0;
                            else
                                data.Rate = Double.Parse(fields[(int)DataFieldNames.CurrentRate], culture);

                            string datetime = String.Format(CultureInfo.InvariantCulture,
                                "{0} {1}", fields[(int)DataFieldNames.TradeDate].Trim(trimchars),
                                fields[(int)DataFieldNames.TradeTime].Trim(trimchars));
                            data.TradeDate = DateTime.Parse(datetime, culture);

                            if(AdjustToLocalTime)
                            {
                                DateTime utcDateTime = DateTime.SpecifyKind(data.TradeDate.AddHours(5), DateTimeKind.Utc);
                                data.TradeDate = utcDateTime.ToLocalTime();
                                if(data.TradeDate.IsDaylightSavingTime())
                                {
                                    TimeSpan ts = new TimeSpan(1, 0, 0);
                                    data.TradeDate = data.TradeDate.Subtract(ts);
                                }
                            }

                            try
                            {
                                if(fields[(int)DataFieldNames.MinPrice].Equals(NA, StringComparison.InvariantCultureIgnoreCase))
                                    data.Min = 0;
                                else
                                    data.Min = Double.Parse(fields[(int)DataFieldNames.MinPrice], culture);
                            }
                            catch(Exception)
                            {
                                data.Min = 0;
                                throw;
                            }

                            try
                            {
                                if(fields[(int)DataFieldNames.MaxPrice].Equals(NA, StringComparison.InvariantCultureIgnoreCase))
                                    data.Max = 0;
                                else
                                    data.Max = Double.Parse(fields[(int)DataFieldNames.MaxPrice], culture);
                            }
                            catch(Exception)
                            {
                                data.Max = 0;
                                throw;
                            }

                            Result.Add(data);
                        }

                        return Result;
                    }
                    finally
                    {
                        respReader.Close();
                    }
                }
                finally
                {
                    respStream.Close();
                }
            }
            finally
            {
                resp.Close();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return CurrencyData by suplied Currency codes
        /// </summary>
        /// <param name="source">Three-chars Currency code</param>
        /// <param name="target">Three-chars Currency code</param>
        /// <returns>CurrencyData class contains exchange rate information</returns>
        public CurrencyData GetCurrencyData(string source, string target)
        {
            CurrencyData data = new CurrencyData(source, target);

            GetCurrencyData(ref data);

            return data;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks CurrencyData
        /// </summary>
        /// <param name="data">CurrencyData object to check</param>
        /// <exception cref="System.ArgumentNullException">Thrown when either BaseCode or TargetCode is empty, or BaseCode is equal to TargetCode</exception>
        private void CheckParams(CurrencyData data)
        {
            if(data.BaseCode.Length == 0)
                throw new ArgumentNullException("data.BaseCode", "Base currency code is not specified!");

            if(data.TargetCode.Length == 0)
                throw new ArgumentNullException("data.TargetCode", "Target currency code is not specified!");

            if(data.BaseCode.Equals(data.TargetCode))
                throw new ArgumentException("data.BaseCode, data.TargetCode", "Base currency code is equal to Target code!");
        }
Exemplo n.º 4
0
        /// <summary>
        /// Receive current Currency data by specified CurrencyData param
        /// </summary>
        /// <param name="data">Reference to a CurrencyData class containing the Currency codes</param>
        /// <seealso cref="Zayko.Finance.CurrencyData"/>
        public void GetCurrencyData(ref CurrencyData data)
        {
            CheckParams(data);

            // Create the URL:
            StringBuilder urlpart = new StringBuilder();

            urlpart.AppendFormat(CultureInfo.InvariantCulture, paretemplate, data.BaseCode,
                    data.TargetCode);

            List<CurrencyData> listData = GetData(new Uri(String.Format(CultureInfo.InvariantCulture, urltemplate, urlpart.ToString())));

            if((listData != null) && (listData.Count > 0))
                data = listData[0];
        }