Permalink
Browse files

fixing dal

  • Loading branch information...
srmrco committed Mar 23, 2015
1 parent c8db80e commit d53061a75e44c86f2e6d0fb9c7b45ff79f632bee
@@ -16,6 +16,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5D6C294B-BD74-430B-B71E-4F5777070807}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Unit", "Tests\Tests.Unit\Tests.Unit.csproj", "{B72659D8-AFB6-4180-B248-658B5951B5C4}"
ProjectSection(ProjectDependencies) = postProject
{10FDA89D-C911-458C-8E3B-B6ACEC9701C5} = {10FDA89D-C911-458C-8E3B-B6ACEC9701C5}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuoteService", "Services\QuoteService\QuoteService.csproj", "{10FDA89D-C911-458C-8E3B-B6ACEC9701C5}"
EndProject
@@ -33,9 +33,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
</ItemGroup>
<ItemGroup>
<Compile Include="Interfaces\IQueryBuilder.cs" />
<Compile Include="Interfaces\IQuoteDataProvider.cs" />
@@ -47,6 +44,17 @@
<Compile Include="Services\Rbc\QueryBuilder.cs" />
<Compile Include="Services\Rbc\QuoteDataProvider.cs" />
<Compile Include="Services\Rbc\QuoteService.cs" />
<Compile Include="Utils\ConvertHelper.cs" />
<Compile Include="Utils\ExtensionMethods.cs" />
<Compile Include="Utils\RequestHelper.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack">
<HintPath>..\..\packages\HtmlAgilityPack.1.4.9\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid+MonoTouch\HtmlAgilityPack.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -6,13 +6,29 @@ namespace QuoteService.Services.Rbc
{
public class QueryBuilder : IQueryBuilder
{
private const string DEFAULT_SORT_BY = "DT_LAST_PUBLICATE";
private const string DEFAULT_SORT_DIRECTION = "DESC";
private const string BaseURL = "http://quote.rbc.ru/cgi-bin/front/content/cash_currency_rates/";
public City City { get; set; }
public Currency Currency { get; set; }
public string Build()
{
throw new NotImplementedException();
var rand = new Random();
var url = string.Format("?sortf={0}&sortd={1}&city={2}&currency={3}&summa=&period=60&pagerLimiter=70&pageNumber=1&r=0.0000{4}",
DEFAULT_SORT_BY,
DEFAULT_SORT_DIRECTION,
(int)City,
(int)Currency,
rand.Next(893933499)
);
return BaseURL + url;
}
}
}
@@ -1,5 +1,7 @@
using System;
using System.Net.Http;
using QuoteService.Interfaces;
using QuoteService.Utils;
namespace QuoteService.Services.Rbc
{
@@ -19,7 +21,23 @@ public QuoteDataProvider(IQueryBuilder queryBuilder)
public string GetQuoteDocumentText()
{
throw new NotImplementedException();
var result = string.Empty;
var buildUrl = QueryBuilder.Build();
Uri resourceUri;
if (!RequestHelper.TryGetUri(buildUrl, out resourceUri))
return null;
using (var client = new HttpClient())
{
var response = client.GetAsync(resourceUri).Result;
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
}
}
return result;
}
}
}
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;
using QuoteService.Interfaces;
using QuoteService.Models;
using QuoteService.Utils;
namespace QuoteService.Services.Rbc
{
@@ -21,7 +24,48 @@ public QuoteService(IQuoteDataProvider provider)
public IEnumerable<ExchangeData> GetExchangeRates()
{
throw new NotImplementedException();
var result = new List<ExchangeData>();
var dataText = DataProvider.GetQuoteDocumentText();
if (string.IsNullOrEmpty(dataText))
return result;
var doc = new HtmlDocument();
doc.LoadHtml(dataText);
var table = doc.DocumentNode.Descendants("table")
.Where(o => o.Attributes["class"] != null && o.Attributes["class"].Value == "common")
.FirstOrDefault(o => o.ParentNode.Name == "div" && o.ParentNode.Attributes["id"] != null && o.ParentNode.Attributes["id"].Value == "container_table_rates");
if (table != null)
{
foreach (var row in table.Descendants("tr"))
{
var cells = row.Descendants("td").ToList();
if (cells.Count <= 2)
continue;
var dataItem = new ExchangeData();
dataItem.Name = cells.ExtractTextForHtmlNodeByClass("name");
dataItem.BuyRate = ConvertHelper.ConvertDecimal(cells.ExtractTextForHtmlNodeByClass("pok"));
dataItem.SellRate = ConvertHelper.ConvertDecimal(cells.ExtractTextForHtmlNodeByClass("prod"));
dataItem.AvailableAmount = ConvertHelper.ConvertDecimal(cells.ExtractTextForHtmlNodeByClass("sum"));
var date = cells.GetHtmlNodeByClass("time");
if (date.Attributes["title"] != null)
dataItem.DateCreated = ConvertHelper.ConvertDate(date.Attributes["title"].Value);
if (!string.IsNullOrEmpty(date.InnerText))
{
var time = ConvertHelper.ConvertTime(date.InnerText);
if (time != TimeSpan.MinValue)
dataItem.DateCreated = dataItem.DateCreated.AddTicks(time.Ticks);
}
result.Add(dataItem);
}
}
return result;
}
}
}
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QuoteService.Utils
{
public class ConvertHelper
{
public static readonly CultureInfo DefaultCultureInfo = new CultureInfo("ru-RU");
public static decimal? ConvertDecimal(string text)
{
if (string.IsNullOrEmpty(text))
return null;
return Decimal.Parse(text, new NumberFormatInfo { NumberDecimalSeparator = "." });
}
public static DateTime ConvertDate(string date)
{
var result = DateTime.MinValue;
if (string.IsNullOrEmpty(date))
return result;
DateTime.TryParse(date, DefaultCultureInfo, DateTimeStyles.None, out result);
return result;
}
public static TimeSpan ConvertTime(string time)
{
var result = TimeSpan.MinValue;
if (string.IsNullOrEmpty(time))
return result;
TimeSpan.TryParse(time, DefaultCultureInfo, out result);
return result;
}
}
}
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using HtmlAgilityPack;
namespace QuoteService.Utils
{
public static class HtmlNodeListExtensionMethods
{
public static HtmlNode GetHtmlNodeByClass<T>(this IList<T> list, string className) where T : HtmlNode
{
return list.FirstOrDefault(c => c.Attributes["class"] != null && c.Attributes["class"].Value == className);
}
public static string ExtractTextForHtmlNodeByClass<T>(this IList<T> list, string className) where T : HtmlNode
{
var item = list.GetHtmlNodeByClass(className);
return item != null ? WebUtility.HtmlDecode(item.InnerText) : string.Empty;
}
}
}
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QuoteService.Utils
{
public class RequestHelper
{
internal static bool TryGetUri(string uriString, out Uri uri)
{
// Note that this app has both "Internet (Client)" and "Home and Work Networking" capabilities set,
// since the user may provide URIs for servers located on the internet or intranet. If apps only
// communicate with servers on the internet, only the "Internet (Client)" capability should be set.
// Similarly if an app is only intended to communicate on the intranet, only the "Home and Work
// Networking" capability should be set.
if (!Uri.TryCreate(uriString.Trim(), UriKind.Absolute, out uri))
{
return false;
}
if (uri.Scheme != "http" && uri.Scheme != "https")
{
return false;
}
return true;
}
}
}
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="portable-net451+win81+wpa81" />
</packages>
Oops, something went wrong.

0 comments on commit d53061a

Please sign in to comment.