Skip to content

Commit

Permalink
Seemingly working language downloads extraction that doesn't use 3rd …
Browse files Browse the repository at this point in the history
…party deserialization
  • Loading branch information
boggydigital committed Apr 1, 2016
1 parent e8c072e commit cbdc5ec
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class TokenExtractorController : ITokenExtractorController
public IEnumerable<string> ExtractMultiple(string data)
{
// extracting login token that is 43 characters (letters, numbers, - ...)
Regex regex = new Regex(@"[\w-]{43}");
const string tokenPattern = @"[\w-]{43}";
var regex = new Regex(tokenPattern);
var match = regex.Match(data);
while (match.Success)
{
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using GOG.Interfaces;
using System;
using System.Linq;
using System.Collections.Generic;
using GOG.Interfaces;
using System.Text.RegularExpressions;

using GOG.Model;

namespace GOG.Controllers
{
Expand All @@ -8,6 +14,7 @@ public class GameDetailsDownloadsController :
private const string downloadsStart = "[[";
private const string downloadsEnd = "]]";
private const string nullString = "null";
private const string emptyString = "";

public string ExtractSingle(string input)
{
Expand All @@ -27,9 +34,58 @@ public string ExtractSingle(string input)
return result;
}

public string Sanitize(string input1, string input2)
public string SanitizeSingle(string inputString, string sanitizedValue)
{
return inputString.Replace(sanitizedValue, nullString);
}

public string SanitizeMultiple(string inputString, IEnumerable<string> sanitizedValues)
{
foreach (var sanitizedValue in sanitizedValues)
inputString = inputString.Replace(sanitizedValue, emptyString);

return inputString;
}

public IEnumerable<string> ExtractMultiple(string data)
{
const string languagePattern = @"\[""[\w\\]*"",";
var regex = new Regex(languagePattern);

var match = regex.Match(data);
while (match.Success)
{
yield return match.Value.Substring(1);
match = match.NextMatch();
}
}

public GameDetails ExtractLanguageDownloads(
GameDetails details,
OperatingSystemsDownloads[][] downloads,
IEnumerable<string> languages)
{
return input1.Replace(input2, nullString);
if (downloads?.Length != languages?.Count())
throw new InvalidOperationException("Extracted different number of downloads and languages.");

details.LanguageDownloads = new List<OperatingSystemsDownloads>();

for (var ii = 0; ii < languages.Count(); ii++)
{
var download = downloads[ii]?[0];
if (download == null)
throw new InvalidOperationException("Extracted downloads doesn't contain expected element");

var language = SanitizeMultiple(
languages.ElementAt(ii),
new string[2] { "\"", "," });

download.Language = language;

details?.LanguageDownloads.Add(download);
}

return details;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
namespace GOG.Interfaces
using System.Collections.Generic;

using GOG.Model;

namespace GOG.Interfaces
{
public interface IExtractSingleDelegate
public interface IExtractSingleDelegate<Input, Output>
{
Output ExtractSingle(Input input);
}

public interface ISanitazeSingleDelegate
{
string SanitizeSingle(string input1, string input2);
}

public interface ISanitazeMultipleDelegate
{
string ExtractSingle(string input);
string SanitizeMultiple(string input1, IEnumerable<string> input2);
}

public interface ISanitazeDelegate
public interface IExtractLanguageDownloads
{
string Sanitize(string input1, string input2);
GameDetails ExtractLanguageDownloads(
GameDetails details,
OperatingSystemsDownloads[][] downloads,
IEnumerable<string> languages);
}

public interface IGameDetailsDownloadsController:
IExtractSingleDelegate,
ISanitazeDelegate
IExtractSingleDelegate<string, string>,
IExtractMultipleDelegate<string, string>,
ISanitazeSingleDelegate,
ISanitazeMultipleDelegate,
IExtractLanguageDownloads
{
// ...
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace GOG.Interfaces
{
public interface IExtractMultipleDelegate
public interface IExtractMultipleDelegate<Input, Output>
{
IEnumerable<string> ExtractMultiple(string data);
IEnumerable<Output> ExtractMultiple(Input data);
}

public interface ITokenExtractorController:
IExtractMultipleDelegate
IExtractMultipleDelegate<string, string>
{
// ...
}
Expand Down

0 comments on commit cbdc5ec

Please sign in to comment.