Skip to content

Commit

Permalink
fixing dal
Browse files Browse the repository at this point in the history
  • Loading branch information
srmrco committed Mar 23, 2015
1 parent c8db80e commit d53061a
Show file tree
Hide file tree
Showing 12 changed files with 1,081 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CashQuotes.sln
Expand Up @@ -16,6 +16,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5D6C294B-BD74-430B-B71E-4F5777070807}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5D6C294B-BD74-430B-B71E-4F5777070807}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.Unit", "Tests\Tests.Unit\Tests.Unit.csproj", "{B72659D8-AFB6-4180-B248-658B5951B5C4}" 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 EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuoteService", "Services\QuoteService\QuoteService.csproj", "{10FDA89D-C911-458C-8E3B-B6ACEC9701C5}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuoteService", "Services\QuoteService\QuoteService.csproj", "{10FDA89D-C911-458C-8E3B-B6ACEC9701C5}"
EndProject EndProject
Expand Down
14 changes: 11 additions & 3 deletions Services/QuoteService/QuoteService.csproj
Expand Up @@ -33,9 +33,6 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
</ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Interfaces\IQueryBuilder.cs" /> <Compile Include="Interfaces\IQueryBuilder.cs" />
<Compile Include="Interfaces\IQuoteDataProvider.cs" /> <Compile Include="Interfaces\IQuoteDataProvider.cs" />
Expand All @@ -47,6 +44,17 @@
<Compile Include="Services\Rbc\QueryBuilder.cs" /> <Compile Include="Services\Rbc\QueryBuilder.cs" />
<Compile Include="Services\Rbc\QuoteDataProvider.cs" /> <Compile Include="Services\Rbc\QuoteDataProvider.cs" />
<Compile Include="Services\Rbc\QuoteService.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> </ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" /> <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. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
18 changes: 17 additions & 1 deletion Services/QuoteService/Services/Rbc/QueryBuilder.cs
Expand Up @@ -6,13 +6,29 @@ namespace QuoteService.Services.Rbc
{ {
public class QueryBuilder : IQueryBuilder 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 City City { get; set; }


public Currency Currency { get; set; } public Currency Currency { get; set; }


public string Build() 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;
} }
} }
} }
20 changes: 19 additions & 1 deletion Services/QuoteService/Services/Rbc/QuoteDataProvider.cs
@@ -1,5 +1,7 @@
using System; using System;
using System.Net.Http;
using QuoteService.Interfaces; using QuoteService.Interfaces;
using QuoteService.Utils;


namespace QuoteService.Services.Rbc namespace QuoteService.Services.Rbc
{ {
Expand All @@ -19,7 +21,23 @@ public QuoteDataProvider(IQueryBuilder queryBuilder)


public string GetQuoteDocumentText() 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;
} }
} }
} }
46 changes: 45 additions & 1 deletion Services/QuoteService/Services/Rbc/QuoteService.cs
@@ -1,7 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;
using QuoteService.Interfaces; using QuoteService.Interfaces;
using QuoteService.Models; using QuoteService.Models;
using QuoteService.Utils;


namespace QuoteService.Services.Rbc namespace QuoteService.Services.Rbc
{ {
Expand All @@ -21,7 +24,48 @@ public QuoteService(IQuoteDataProvider provider)


public IEnumerable<ExchangeData> GetExchangeRates() 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;
} }
} }
} }
45 changes: 45 additions & 0 deletions Services/QuoteService/Utils/ConvertHelper.cs
@@ -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;
}

}
}
23 changes: 23 additions & 0 deletions Services/QuoteService/Utils/ExtensionMethods.cs
@@ -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;
}

}
}
31 changes: 31 additions & 0 deletions Services/QuoteService/Utils/RequestHelper.cs
@@ -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;
}
}
}
4 changes: 4 additions & 0 deletions Services/QuoteService/packages.config
@@ -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>

0 comments on commit d53061a

Please sign in to comment.