Skip to content

Commit b7af543

Browse files
committed
Added some extension methods for geo
1 parent dce09c0 commit b7af543

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

Source/Shared/EventBuilder.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,7 @@ public EventBuilder SetMessage(string message) {
9494
/// </summary>
9595
/// <param name="coordinates">The event coordinates.</param>
9696
public EventBuilder SetGeo(string coordinates) {
97-
if (String.IsNullOrWhiteSpace(coordinates)) {
98-
Target.Geo = null;
99-
return this;
100-
}
101-
102-
if (coordinates.Contains(",") || coordinates.Contains(".") || coordinates.Contains(":"))
103-
Target.Geo = coordinates;
104-
else
105-
throw new ArgumentException("Must be either lat,lon or an IP address.", "coordinates");
106-
97+
Target.SetGeo(coordinates);
10798
return this;
10899
}
109100

Source/Shared/Extensions/EventExtensions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ public static void SetUserDescription(this Event ev, UserDescription description
182182
ev.Data[Event.KnownDataKeys.UserDescription] = description;
183183
}
184184

185+
/// <summary>
186+
/// Sets the event geo coordinates. Can be either "lat,lon" or an IP address that will be used to auto detect the geo coordinates.
187+
/// </summary>
188+
/// <param name="coordinates">The event coordinates.</param>
189+
public static void SetGeo(this Event ev, string coordinates) {
190+
if (String.IsNullOrWhiteSpace(coordinates)) {
191+
ev.Geo = null;
192+
return;
193+
}
194+
195+
if (coordinates.Contains(",") || coordinates.Contains(".") || coordinates.Contains(":"))
196+
ev.Geo = coordinates;
197+
else
198+
throw new ArgumentException("Must be either lat,lon or an IP address.", "coordinates");
199+
}
200+
201+
202+
/// <summary>
203+
/// Sets the event geo coordinates.
204+
/// </summary>
205+
/// <param name="latitude">The event latitude.</param>
206+
/// <param name="longitude">The event longitude.</param>
207+
public static void SetGeo(this Event ev, double latitude, double longitude) {
208+
if (latitude < -90.0 || latitude > 90.0)
209+
throw new ArgumentOutOfRangeException("latitude", "Must be a valid latitude value between -90.0 and 90.0.");
210+
if (longitude < -180.0 || longitude > 180.0)
211+
throw new ArgumentOutOfRangeException("longitude", "Must be a valid longitude value between -180.0 and 180.0.");
212+
213+
ev.Geo = latitude.ToString("#0.0#######") + "," + longitude.ToString("#0.0#######");
214+
}
215+
185216
/// <summary>
186217
/// Sets the manual stacking key
187218
/// </summary>

Source/Tests/Plugins/PluginTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public void ConfigurationDefaults_SerializedProperties() {
103103
Assert.Equal("blake", context.Event.GetUserIdentity().Identity);
104104
Assert.Equal("blake", context.Event.GetUserIdentity(serializer).Identity);
105105
Assert.Equal("1.0", context.Event.Data[Event.KnownDataKeys.Version]);
106+
107+
context.Event.SetUserIdentity(new UserInfo("blake"));
108+
Assert.Equal("blake", context.Event.GetUserIdentity().Identity);
109+
Assert.Equal("blake", context.Event.GetUserIdentity(serializer).Identity);
106110
}
107111

108112
[Fact]

0 commit comments

Comments
 (0)