Skip to content

Commit 0080c85

Browse files
braahyanbenjben
authored andcommitted
DotNet: support YAML directly (closes #185)
1 parent e194984 commit 0080c85

16 files changed

+830
-4224
lines changed

dotnet/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ var referer = Parser.Parse(new Uri(refererUrl), pageUrl);
2525
Console.WriteLine(r.Medium); // => "Search"
2626
Console.WriteLine(r.Source); // => "Google"
2727
Console.WriteLine(r.Term); // => "gateway oracle cards denise linn"
28+
29+
30+
// usage with custom supplied databases and new Medium Values:
31+
private enum TestEnum
32+
{
33+
// Order is important, most important medium's first
34+
Search = 0,
35+
Paid,
36+
Social,
37+
Email,
38+
Unknown,
39+
Internal,
40+
Foobar
41+
}
42+
referrerUrl = "https://thrivehive.com";
43+
44+
var parser = new Parser<MyEnum>(new[] {@"
45+
foobar:
46+
Thrivehive:
47+
domains:
48+
- thrivehive.com"});
49+
50+
var referer = Parser.Parse(new Uri(refererUrl), pageUrl);
51+
52+
Console.WriteLine(r.Medium); // => "Foobar"
53+
Console.WriteLine(r.Source); // => "Thrivehive"
54+
2855
```
2956

3057
### Installation

dotnet/RefererParser.Tests/ParserTests.cs

Lines changed: 122 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
using Newtonsoft.Json.Linq;
2-
using RefererParser;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Diagnostics;
1+
using System;
62
using System.IO;
73
using System.Linq;
8-
using System.Text;
4+
using Newtonsoft.Json.Linq;
95
using Xunit;
106

117
namespace RefererParser.Tests
128
{
139
public class ParserTests
1410
{
15-
[Fact]
16-
public void LoadCatalog()
17-
{
18-
Assert.True(Referers.Catalog["test"] == null);
19-
}
20-
2111
[Fact]
2212
public void TestReferers()
2313
{
@@ -126,5 +116,125 @@ public void TestFalsePositivies()
126116
Assert.Equal(sample.Source, result.Source ?? string.Empty);
127117
}
128118
}
119+
120+
private enum TestEnum
121+
{
122+
// Order is important, most important medium's first
123+
Search = 0,
124+
Paid,
125+
Social,
126+
Email,
127+
Unknown,
128+
Internal,
129+
Foobar
130+
}
131+
132+
[Fact]
133+
public void TestCustomEnum()
134+
{
135+
var parser = new Parser<TestEnum>();
136+
var set = new[]
137+
{
138+
new
139+
{
140+
Name = "Unknown Google service",
141+
Url = "http://xxx.google.com",
142+
Medium = TestEnum.Search,
143+
Source = "Google",
144+
Term = string.Empty
145+
},
146+
new
147+
{
148+
Name = "Unknown Yahoo! service",
149+
Url = "http://yyy.yahoo.com",
150+
Medium = TestEnum.Search,
151+
Source = "Yahoo!",
152+
Term = string.Empty
153+
},
154+
new
155+
{
156+
Name = "Non-search Google Drive link",
157+
Url = "http://www.google.com/url?q=http://www.whatismyreferer.com/&sa=D&usg=ALhdy2_qs3arPmg7E_e2aBkj6K0gHLa5rQ",
158+
Medium = TestEnum.Search,
159+
Source = "Google",
160+
Term = "http://www.whatismyreferer.com/",
161+
},
162+
};
163+
164+
foreach (var sample in set)
165+
{
166+
var result = parser.ParseReferer(new Uri(sample.Url), "www.snowplowanalytics.com");
167+
Assert.NotNull(result);
168+
Assert.Equal(sample.Source, result.Source ?? string.Empty);
169+
}
170+
}
171+
172+
[Fact]
173+
public void TestCustomSrcList()
174+
{
175+
var parser = new Parser<TestEnum>(new[] {@"
176+
foobar:
177+
Thrivehive:
178+
domains:
179+
- thrivehive.com"});
180+
var set = new[]
181+
{
182+
new
183+
{
184+
Name = "Thrivehive",
185+
Url = "https://thrivehive.com",
186+
Medium = TestEnum.Foobar,
187+
Source = "Thrivehive"
188+
},
189+
};
190+
191+
foreach (var sample in set)
192+
{
193+
var result = parser.ParseReferer(new Uri(sample.Url), "www.snowplowanalytics.com");
194+
Assert.NotNull(result);
195+
Assert.Equal(sample.Source, result.Source ?? string.Empty);
196+
}
197+
}
198+
199+
[Fact]
200+
public void TestCustomSrcListWithStandardEnum()
201+
{
202+
var parser = new Parser<RefererMedium>(new[] {@"
203+
Search:
204+
Thrivehive:
205+
domains:
206+
- thrivehive.com"});
207+
var set = new[]
208+
{
209+
new
210+
{
211+
Name = "Thrivehive",
212+
Url = "https://thrivehive.com",
213+
Medium = TestEnum.Foobar,
214+
Source = "Thrivehive"
215+
},
216+
};
217+
218+
foreach (var sample in set)
219+
{
220+
var result = parser.ParseReferer(new Uri(sample.Url), "www.snowplowanalytics.com");
221+
Assert.NotNull(result);
222+
Assert.Equal(sample.Source, result.Source ?? string.Empty);
223+
}
224+
}
225+
226+
private enum TestInvalidEnum
227+
{
228+
}
229+
230+
231+
[Fact]
232+
public void TestInvalidCustomEnum()
233+
{
234+
var exception = Assert.Throws<ArgumentException>(() => new Parser<TestInvalidEnum>());
235+
Assert.Contains("Search", exception.Message);
236+
Assert.Contains("Internal", exception.Message);
237+
Assert.Contains("Unknown", exception.Message);
238+
}
129239
}
130240
}

dotnet/RefererParser.Tests/RefererParser.Tests.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
36-
<SpecificVersion>False</SpecificVersion>
37-
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net40\Newtonsoft.Json.dll</HintPath>
35+
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
36+
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
3837
</Reference>
3938
<Reference Include="System" />
4039
<Reference Include="System.Core" />
@@ -46,6 +45,9 @@
4645
<Reference Include="xunit">
4746
<HintPath>..\packages\xunit.1.9.1\lib\net20\xunit.dll</HintPath>
4847
</Reference>
48+
<Reference Include="YamlDotNet, Version=4.3.1.0, Culture=neutral, PublicKeyToken=ec19458f3c15af5e">
49+
<HintPath>..\packages\YamlDotNet.Signed.4.3.1\lib\net35\YamlDotNet.dll</HintPath>
50+
</Reference>
4951
</ItemGroup>
5052
<ItemGroup>
5153
<Compile Include="ParserTests.cs" />
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net40" />
3+
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net40" />
44
<package id="xunit" version="1.9.1" targetFramework="net40" />
5+
<package id="YamlDotNet.Signed" version="4.3.1" targetFramework="net40" />
56
</packages>

dotnet/RefererParser.Tests/referer-tests.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,13 @@
230230
"source": null,
231231
"term": null,
232232
"known": false
233+
},
234+
{
235+
"spec": "Google Paid",
236+
"uri": "https://tpc.googlesyndication.com/safeframe/1-0-23/html/container.html",
237+
"medium": "paid",
238+
"source": "Google",
239+
"term": null,
240+
"known": false
233241
}
234242
]

dotnet/RefererParser.nuspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<package>
22
<metadata>
33
<id>RefererParser</id>
4-
<version>1.0.0.2</version>
5-
<authors>iPerform Software BV</authors>
4+
<version>1.0.0.3</version>
5+
<authors>iPerform Software BV, ThriveHive</authors>
66
<description>A .NET implementation of the Snowplow referer-parser project</description>
77
<language>en-US</language>
88
<releaseNotes>New referers have been added and fixed problem with parsing of empty query paremeters on certain referers</releaseNotes>
@@ -11,7 +11,7 @@
1111
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
1212
<tags>RefererParser ReferrerParser Referrer Referer Parser Snowplow</tags>
1313
<dependencies>
14-
<dependency id="Newtonsoft.Json"/>
14+
<dependency id="YamlDotNet.Signed" version="4.3.1" />
1515
</dependencies>
1616
</metadata>
1717
<files>

0 commit comments

Comments
 (0)