public RelatedResults(JsonData resultsJson) { if (resultsJson == null) return; ResultAnnotations = new Annotation(resultsJson.GetValue<JsonData>("annotations")); Score = resultsJson.GetValue<double>("score"); Kind = resultsJson.GetValue<string>("kind"); JsonData value = resultsJson.GetValue<JsonData>("value"); ValueAnnotations = new Annotation(value.GetValue<JsonData>("annotations")); Retweeted = value.GetValue<bool>("retweeted"); InReplyToScreenName = value.GetValue<string>("in_reply_to_screen_name"); var contributors = value.GetValue<JsonData>("contributors"); Contributors = contributors == null ? new List<Contributor>() : (from JsonData contributor in contributors select new Contributor(contributor)) .ToList(); Coordinates = new Coordinate(value.GetValue<JsonData>("coordinates")); Place = new Place(value.GetValue<JsonData>("place")); User = new User(value.GetValue<JsonData>("user")); RetweetCount = value.GetValue<int>("retweet_count"); IDString = value.GetValue<string>("id_str"); InReplyToUserID = value.GetValue<ulong>("in_reply_to_user_id"); Favorited = value.GetValue<bool>("favorited"); InReplyToStatusIDString = value.GetValue<string>("in_reply_to_status_id_str"); InReplyToStatusID = value.GetValue<ulong>("in_reply_to_status_id"); Source = value.GetValue<string>("source"); CreatedAt = value.GetValue<string>("created_at").GetDate(DateTime.MaxValue); InReplyToUserIDString = value.GetValue<string>("in_reply_to_user_id_str"); Truncated = value.GetValue<bool>("truncated"); Geo = new Geo(value.GetValue<JsonData>("geo")); Text = value.GetValue<string>("text"); }
public void Geo_Ctor_Parses_JsonData() { var geoJson = JsonMapper.ToObject(ReverseResponse); var geo = new Geo(geoJson); Assert.IsNotNull(geo.Places); var places = geo.Places; Assert.AreEqual(4, places.Count); Assert.AreEqual("neighborhood", places.First().PlaceType); }
/// <summary> /// Transforms response into List of Geo. /// </summary> /// <param name="responseJson">Json with Twitter response</param> /// <returns>List of SavedSearch</returns> public List <T> ProcessResults(string responseJson) { if (string.IsNullOrWhiteSpace(responseJson)) { return(new List <T>()); } JsonData geoJson = JsonMapper.ToObject(responseJson); Geo geo; switch (Type) { case GeoType.ID: geo = new Geo { Places = new List <Place> { new Place(geoJson) } }; break; case GeoType.Reverse: case GeoType.Search: case GeoType.SimilarPlaces: geo = new Geo(geoJson); break; default: geo = new Geo(); break; } geo.Type = Type; geo.Accuracy = Accuracy; geo.Granularity = Granularity; geo.ID = ID; geo.Latitude = Latitude; geo.Longitude = Longitude; geo.IP = IP; geo.MaxResults = MaxResults; geo.Query = Query; geo.ContainedWithin = ContainedWithin; geo.Attribute = Attribute; geo.PlaceName = PlaceName; return(new List <Geo> { geo }.OfType <T>().ToList()); }
/// <summary> /// transforms XML into IList of SavedSearch /// </summary> /// <param name="responseXml">xml with Twitter response</param> /// <returns>List of SavedSearch</returns> public List <T> ProcessResults(string responseXml) { XElement twitterResponse = XElement.Parse(responseXml); List <XElement> responseItems = new List <XElement>(); // place_type under root means that it's an ID query if (twitterResponse.Element("place_type") != null) { responseItems.Add( new XElement("root", new XElement("contained_within", new XElement("item", twitterResponse.Elements())))); } else // reverse geocode query { responseItems = twitterResponse .Element("result") .Element("places") .Elements("item").ToList(); } var geo = new Geo { Type = Type, Accuracy = Accuracy, Granularity = Granularity, ID = ID, Latitude = Latitude, Longitude = Longitude, IP = IP, MaxResults = MaxResults, Query = Query, ContainedWithin = ContainedWithin, Attribute = Attribute, Places = (from pl in responseItems select Place.CreatePlace(pl.Element("contained_within").Element("item"))) .ToList() }; return(new List <Geo> { geo }.OfType <T>().ToList()); }
Geo HandleMultiplePlaceResponse(JsonData geoJson) { var geo = new Geo(geoJson) { Type = Type, Accuracy = Accuracy, Granularity = Granularity, ID = ID, Latitude = Latitude, Longitude = Longitude, IP = IP, MaxResults = MaxResults, Query = Query, ContainedWithin = ContainedWithin, Attribute = Attribute, PlaceName = PlaceName }; return(geo); }
public RelatedResults(JsonData resultsJson) { if (resultsJson == null) { return; } ResultAnnotations = new Annotation(resultsJson.GetValue <JsonData>("annotations")); Score = resultsJson.GetValue <double>("score"); Kind = resultsJson.GetValue <string>("kind"); JsonData value = resultsJson.GetValue <JsonData>("value"); ValueAnnotations = new Annotation(value.GetValue <JsonData>("annotations")); Retweeted = value.GetValue <bool>("retweeted"); InReplyToScreenName = value.GetValue <string>("in_reply_to_screen_name"); var contributors = value.GetValue <JsonData>("contributors"); Contributors = contributors == null ? new List <Contributor>() : (from JsonData contributor in contributors select new Contributor(contributor)) .ToList(); Coordinates = new Coordinate(value.GetValue <JsonData>("coordinates")); Place = new Place(value.GetValue <JsonData>("place")); User = new User(value.GetValue <JsonData>("user")); RetweetCount = value.GetValue <int>("retweet_count"); IDString = value.GetValue <string>("id_str"); InReplyToUserID = value.GetValue <ulong>("in_reply_to_user_id"); Favorited = value.GetValue <bool>("favorited"); InReplyToStatusIDString = value.GetValue <string>("in_reply_to_status_id_str"); InReplyToStatusID = value.GetValue <ulong>("in_reply_to_status_id"); Source = value.GetValue <string>("source"); CreatedAt = value.GetValue <string>("created_at").GetDate(DateTime.MaxValue); InReplyToUserIDString = value.GetValue <string>("in_reply_to_user_id_str"); Truncated = value.GetValue <bool>("truncated"); Geo = new Geo(value.GetValue <JsonData>("geo")); Text = value.GetValue <string>("text"); }
public void Geo_Ctor_Returns_On_Null_JsonData() { var geo = new Geo((JsonData)null); Assert.Null(geo.Places); }
/// <summary> /// Shreds an XML element into a Status object /// </summary> /// <param name="status">XML element with info</param> /// <returns>Newly populated status object</returns> public static Status CreateStatus(XElement status) { if (status == null) { return null; } var createdAtDate = status.Element("created_at") == null || status.Element("created_at").Value == string.Empty ? DateTime.MinValue : DateTime.ParseExact( status.Element("created_at").Value, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); var favorite = status.Element("favorited") == null || status.Element("favorited").Value == string.Empty ? "false" : status.Element("favorited").Value; var user = status.Element("user"); var retweet = status.Element("retweeted_status"); var retweetCount = status.Element("retweet_count") == null || status.Element("retweet_count").Value == string.Empty ? 0 : int.Parse(status.Element("retweet_count").Value.TrimEnd('+')); var retweeted = status.Element("retweeted") == null || status.Element("retweeted").Value == string.Empty ? false : bool.Parse(status.Element("retweeted").Value); var retweetDate = retweet == null ? null : retweet.Element("created_at").Value; var retweetedAtDate = String.IsNullOrEmpty(retweetDate) ? DateTime.MinValue : DateTime.ParseExact( retweetDate, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); List<Contributor> contributors = null; XElement contributorElement = status.Element("contributors"); if (contributorElement != null) { if (contributorElement.Elements("user").Count() > 0) { contributors = (from contr in contributorElement.Elements("user") select new Contributor { ID = contr.Element("id").Value, ScreenName = contr.Element("screen_name").Value }) .ToList(); } else { contributors = (from id in contributorElement.Elements("user_id") select new Contributor { ID = id.Value, ScreenName = string.Empty }) .ToList(); } } XNamespace geoRss = "http://www.georss.org/georss"; var geoStr = status.Element("geo") != null && status.Element("geo").Element(geoRss + "point") != null ? status.Element("geo").Element(geoRss + "point").Value : string.Empty; Geo geo = new Geo(); if (!string.IsNullOrEmpty(geoStr)) { var coordArr = geoStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { geo = new Geo { Latitude = tempLatitude, Longitude = tempLongitide }; } } var coordStr = status.Element("coordinates") != null && status.Element("coordinates").Element(geoRss + "point") != null ? status.Element("coordinates").Element(geoRss + "point").Value : string.Empty; Coordinate coord = new Coordinate(); if (!string.IsNullOrEmpty(coordStr)) { var coordArr = coordStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { coord = new Coordinate { Latitude = tempLatitude, Longitude = tempLongitide }; } } var place = Place.CreatePlace(status.Element("place")); var annotation = Annotation.CreateAnnotation(status.Element("annotation")); var entities = Entities.CreateEntities(status.Element("entities")); var newStatus = new Status { CreatedAt = createdAtDate, Favorited = bool.Parse(favorite), StatusID = status.GetString("id"), InReplyToStatusID = status.GetString("in_reply_to_status_id"), InReplyToUserID = status.GetString("in_reply_to_user_id"), Source = status.GetString("source"), Text = status.GetString("text"), Truncated = status.GetBool("truncated"), InReplyToScreenName = status.GetString("in_reply_to_screen_name"), Contributors = contributors, Geo = geo, Coordinates = coord, Place = place, Annotation = annotation, User = User.CreateUser(user), Entities = entities, Retweeted = retweeted, RetweetCount = retweetCount, Retweet = retweet == null ? null : new Retweet { ID = retweet.GetString("id"), CreatedAt = retweetedAtDate, Favorited = retweet.GetBool("favorited"), InReplyToScreenName = retweet.GetString("in_reply_to_screen_name"), InReplyToStatusID = retweet.GetString("in_reply_to_status_id"), InReplyToUserID = retweet.GetString("in_reply_to_user_id"), Source = retweet.GetString("source"), Text = retweet.GetString("text"), Retweeted = retweet.GetBool("retweeted"), RetweetCount = //retweet.GetInt("retweet_count"), retweet.Element("retweet_count") == null || retweet.Element("retweet_count").Value == string.Empty ? 0 : int.Parse(retweet.Element("retweet_count").Value.TrimEnd('+')), Truncated = retweet.GetBool("truncated", true), RetweetingUser = User.CreateUser(retweet.Element("user")) } }; return newStatus; }
/// <summary> /// Shreds an XML element into a Status object /// </summary> /// <param name="status">XML element with info</param> /// <returns>Newly populated status object</returns> public Status CreateStatus(XElement status) { if (status == null) { return null; } var dateParts = status.Element("created_at").Value.Split(' '); var createdAtDate = dateParts.Count() > 1 ? DateTime.Parse( string.Format("{0} {1} {2} {3} GMT", dateParts[1], dateParts[2], dateParts[5], dateParts[3]), CultureInfo.InvariantCulture) : DateTime.MinValue; var user = status.Element("user"); var retweet = status.Element("retweeted_status"); var rtDateParts = retweet == null ? null : retweet.Element("created_at").Value.Split(' '); var retweetedAtDate = retweet == null ? DateTime.MinValue : DateTime.Parse( string.Format("{0} {1} {2} {3} GMT", rtDateParts[1], rtDateParts[2], rtDateParts[5], rtDateParts[3]), CultureInfo.InvariantCulture); List<string> contributorIDs = null; if (status.Element("contributors") != null) { contributorIDs = (from id in status.Element("contributors").Elements("user_id") select id.Value) .ToList(); } XNamespace geoRss = "http://www.georss.org/georss"; var geoStr = status.Element("geo") != null && status.Element("geo").Element(geoRss + "point") != null ? status.Element("geo").Element(geoRss + "point").Value : string.Empty; Geo geo = new Geo(); if (!string.IsNullOrEmpty(geoStr)) { var coordArr = geoStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { geo = new Geo { Latitude = tempLatitude, Longitude = tempLongitide }; } } var coordStr = status.Element("coordinates") != null && status.Element("coordinates").Element(geoRss + "point") != null ? status.Element("coordinates").Element(geoRss + "point").Value : string.Empty; Coordinate coord = new Coordinate(); if (!string.IsNullOrEmpty(coordStr)) { var coordArr = coordStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { coord = new Coordinate { Latitude = tempLatitude, Longitude = tempLongitide }; } } var place = new Place().CreatePlace(status.Element("place")); var usr = new User(); var newStatus = new Status { CreatedAt = createdAtDate, Favorited = bool.Parse( string.IsNullOrEmpty(status.Element("favorited").Value) ? "true" : status.Element("favorited").Value), StatusID = status.Element("id").Value, InReplyToStatusID = status.Element("in_reply_to_status_id").Value, InReplyToUserID = status.Element("in_reply_to_user_id").Value, Source = status.Element("source").Value, Text = status.Element("text").Value, Truncated = bool.Parse(status.Element("truncated").Value), InReplyToScreenName = status.Element("in_reply_to_screen_name") == null ? string.Empty : status.Element("in_reply_to_screen_name").Value, ContributorIDs = contributorIDs, Geo = geo, Coordinates = coord, Place = place, User = usr.CreateUser(user), Retweet = retweet == null ? null : new Retweet { ID = retweet.Element("id").Value, CreatedAt = retweetedAtDate, Favorited = bool.Parse( string.IsNullOrEmpty(retweet.Element("favorited").Value) ? "true" : retweet.Element("favorited").Value), InReplyToScreenName = retweet.Element("in_reply_to_screen_name").Value, InReplyToStatusID = retweet.Element("in_reply_to_status_id").Value, InReplyToUserID = retweet.Element("in_reply_to_user_id").Value, Source = retweet.Element("source").Value, Text = retweet.Element("text").Value, Truncated = bool.Parse( string.IsNullOrEmpty(retweet.Element("truncated").Value) ? "true" : retweet.Element("truncated").Value), RetweetingUser = usr.CreateUser(retweet.Element("user")) } }; return newStatus; }
/// <summary> /// Shreds an XML element into a Status object /// </summary> /// <param name="status">XML element with info</param> /// <returns>Newly populated status object</returns> public static Status CreateStatus(XElement status) { if (status == null) { return(null); } var createdAtDate = status.Element("created_at") == null || status.Element("created_at").Value == string.Empty ? DateTime.MinValue : DateTime.ParseExact( status.Element("created_at").Value, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); var favorite = status.Element("favorited") == null || status.Element("favorited").Value == string.Empty ? "false" : status.Element("favorited").Value; var user = status.Element("user"); var retweet = status.Element("retweeted_status"); var retweetCount = status.Element("retweet_count") == null || status.Element("retweet_count").Value == string.Empty ? 0 : int.Parse(status.Element("retweet_count").Value.TrimEnd('+')); var retweeted = status.Element("retweeted") == null || status.Element("retweeted").Value == string.Empty ? false : bool.Parse(status.Element("retweeted").Value); var retweetDate = retweet == null ? null : retweet.Element("created_at").Value; var retweetedAtDate = String.IsNullOrEmpty(retweetDate) ? DateTime.MinValue : DateTime.ParseExact( retweetDate, "ddd MMM dd HH:mm:ss %zzzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); List <Contributor> contributors = null; XElement contributorElement = status.Element("contributors"); if (contributorElement != null) { if (contributorElement.Elements("user").Count() > 0) { contributors = (from contr in contributorElement.Elements("user") select new Contributor { ID = contr.Element("id").Value, ScreenName = contr.Element("screen_name").Value }) .ToList(); } else { contributors = (from id in contributorElement.Elements("user_id") select new Contributor { ID = id.Value, ScreenName = string.Empty }) .ToList(); } } XNamespace geoRss = "http://www.georss.org/georss"; var geoStr = status.Element("geo") != null && status.Element("geo").Element(geoRss + "point") != null? status.Element("geo").Element(geoRss + "point").Value: string.Empty; Geo geo = new Geo(); if (!string.IsNullOrEmpty(geoStr)) { var coordArr = geoStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { geo = new Geo { Latitude = tempLatitude, Longitude = tempLongitide }; } } var coordStr = status.Element("coordinates") != null && status.Element("coordinates").Element(geoRss + "point") != null? status.Element("coordinates").Element(geoRss + "point").Value: string.Empty; Coordinate coord = new Coordinate(); if (!string.IsNullOrEmpty(coordStr)) { var coordArr = coordStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { coord = new Coordinate { Latitude = tempLatitude, Longitude = tempLongitide }; } } var place = Place.CreatePlace(status.Element("place")); var annotation = Annotation.CreateAnnotation(status.Element("annotation")); var entities = Entities.CreateEntities(status.Element("entities")); var newStatus = new Status { CreatedAt = createdAtDate, Favorited = bool.Parse(favorite), StatusID = status.GetString("id"), InReplyToStatusID = status.GetString("in_reply_to_status_id"), InReplyToUserID = status.GetString("in_reply_to_user_id"), Source = status.GetString("source"), Text = status.GetString("text"), Truncated = status.GetBool("truncated"), InReplyToScreenName = status.GetString("in_reply_to_screen_name"), Contributors = contributors, Geo = geo, Coordinates = coord, Place = place, Annotation = annotation, User = User.CreateUser(user), Entities = entities, Retweeted = retweeted, RetweetCount = retweetCount, Retweet = retweet == null ? null : new Retweet { ID = retweet.GetString("id"), CreatedAt = retweetedAtDate, Favorited = retweet.GetBool("favorited"), InReplyToScreenName = retweet.GetString("in_reply_to_screen_name"), InReplyToStatusID = retweet.GetString("in_reply_to_status_id"), InReplyToUserID = retweet.GetString("in_reply_to_user_id"), Source = retweet.GetString("source"), Text = retweet.GetString("text"), Retweeted = retweet.GetBool("retweeted"), RetweetCount = //retweet.GetInt("retweet_count"), retweet.Element("retweet_count") == null || retweet.Element("retweet_count").Value == string.Empty ? 0 : int.Parse(retweet.Element("retweet_count").Value.TrimEnd('+')), Truncated = retweet.GetBool("truncated", true), RetweetingUser = User.CreateUser(retweet.Element("user")) } }; return(newStatus); }
/// <summary> /// Shreds an XML element into a Status object /// </summary> /// <param name="status">XML element with info</param> /// <returns>Newly populated status object</returns> public Status CreateStatus(XElement status) { if (status == null) { return(null); } var dateParts = status.Element("created_at").Value.Split(' '); var createdAtDate = dateParts.Count() > 1 ? DateTime.Parse( string.Format("{0} {1} {2} {3} GMT", dateParts[1], dateParts[2], dateParts[5], dateParts[3]), CultureInfo.InvariantCulture) : DateTime.MinValue; var user = status.Element("user"); var retweet = status.Element("retweeted_status"); var rtDateParts = retweet == null ? null : retweet.Element("created_at").Value.Split(' '); var retweetedAtDate = retweet == null ? DateTime.MinValue : DateTime.Parse( string.Format("{0} {1} {2} {3} GMT", rtDateParts[1], rtDateParts[2], rtDateParts[5], rtDateParts[3]), CultureInfo.InvariantCulture); List <string> contributorIDs = null; if (status.Element("contributors") != null) { contributorIDs = (from id in status.Element("contributors").Elements("user_id") select id.Value) .ToList(); } XNamespace geoRss = "http://www.georss.org/georss"; var geoStr = status.Element("geo") != null && status.Element("geo").Element(geoRss + "point") != null? status.Element("geo").Element(geoRss + "point").Value: string.Empty; Geo geo = new Geo(); if (!string.IsNullOrEmpty(geoStr)) { var coordArr = geoStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { geo = new Geo { Latitude = tempLatitude, Longitude = tempLongitide }; } } var coordStr = status.Element("coordinates") != null && status.Element("coordinates").Element(geoRss + "point") != null? status.Element("coordinates").Element(geoRss + "point").Value: string.Empty; Coordinate coord = new Coordinate(); if (!string.IsNullOrEmpty(coordStr)) { var coordArr = coordStr.Split(' '); double tempLatitude = 0; double tempLongitide = 0; if (double.TryParse(coordArr[Coordinate.LatitudePos], out tempLatitude) && double.TryParse(coordArr[Coordinate.LongitudePos], out tempLongitide)) { coord = new Coordinate { Latitude = tempLatitude, Longitude = tempLongitide }; } } var place = new Place().CreatePlace(status.Element("place")); var usr = new User(); var newStatus = new Status { CreatedAt = createdAtDate, Favorited = bool.Parse( string.IsNullOrEmpty(status.Element("favorited").Value) ? "true" : status.Element("favorited").Value), StatusID = status.Element("id").Value, InReplyToStatusID = status.Element("in_reply_to_status_id").Value, InReplyToUserID = status.Element("in_reply_to_user_id").Value, Source = status.Element("source").Value, Text = status.Element("text").Value, Truncated = bool.Parse(status.Element("truncated").Value), InReplyToScreenName = status.Element("in_reply_to_screen_name") == null ? string.Empty : status.Element("in_reply_to_screen_name").Value, ContributorIDs = contributorIDs, Geo = geo, Coordinates = coord, Place = place, User = usr.CreateUser(user), Retweet = retweet == null ? null : new Retweet { ID = retweet.Element("id").Value, CreatedAt = retweetedAtDate, Favorited = bool.Parse( string.IsNullOrEmpty(retweet.Element("favorited").Value) ? "true" : retweet.Element("favorited").Value), InReplyToScreenName = retweet.Element("in_reply_to_screen_name").Value, InReplyToStatusID = retweet.Element("in_reply_to_status_id").Value, InReplyToUserID = retweet.Element("in_reply_to_user_id").Value, Source = retweet.Element("source").Value, Text = retweet.Element("text").Value, Truncated = bool.Parse( string.IsNullOrEmpty(retweet.Element("truncated").Value) ? "true" : retweet.Element("truncated").Value), RetweetingUser = usr.CreateUser(retweet.Element("user")) } }; return(newStatus); }