Beispiel #1
0
        public void Place_Ctor_Parses_JsonData()
        {
            const string ExpectedPolyLine = "yzceFfenjVSv[wQjxAgEzYs]bt@oNfJ_UvL_kAzc@omGfc@uW\u007BB\u007DoDsyEg@kC\u007BdC\u007DiSfI\u007Dh@fIaFvn@qUzj@ySx|OdxBbrBt`@hF|`ARfzF?zhE";
            var placeJson = JsonMapper.ToObject(PlaceJson);

            var place = new Place(placeJson);

            Assert.IsNotNull(place.ContainedWithin);
            Assert.AreEqual("United States", place.ContainedWithin.Country);

            Assert.AreEqual("neighborhood", place.PlaceType);
            Assert.AreEqual("United States", place.Country);
            Assert.AreEqual("SoMa", place.Name);
            Assert.AreEqual("SoMa, San Francisco", place.FullName);

            Assert.IsNotNull(place.Attributes);
            var attrDict = place.Attributes;
            Assert.AreEqual(3, attrDict.Count);
            Assert.AreEqual(attrDict["street_address"], "795 Folsom St");
            Assert.AreEqual(attrDict["623:id"], "210176");
            Assert.AreEqual(attrDict["twitter"], "JoeMayo");

            Assert.IsNotNull(place.BoundingBox);
            Assert.AreEqual("Polygon", place.BoundingBox.Type);

            Assert.AreEqual("US", place.CountryCode);
            Assert.AreEqual("http://api.twitter.com/1/geo/id/2b6ff8c22edd9576.json", place.Url);
            Assert.AreEqual("2b6ff8c22edd9576", place.ID);

            Assert.IsNotNull(place.PolyLines);
            Assert.IsNotNull(place.PolyLines.SingleOrDefault());
            Assert.AreEqual(ExpectedPolyLine, place.PolyLines.First());
        }
        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 Place_Can_Serialize()
        {
            var place = new Place();
            var stringBuilder = new StringBuilder();

            var writer = XmlWriter.Create(stringBuilder);
            var xmlSerializer = new XmlSerializer(typeof(Place));
            xmlSerializer.Serialize(writer, place);
        }
Beispiel #4
0
        public void Place_Ctor_Returns_On_Null_JsonData()
        {
            var place = new Place(null);

            Assert.IsNull(place.Country);
        }
Beispiel #5
0
        public Status(JsonData status)
        {
            if (status == null) return;

            Retweeted = status.GetValue<bool>("retweeted");
            Source = status.GetValue<string>("source");
            InReplyToScreenName = status.GetValue<string>("in_reply_to_screen_name");
            PossiblySensitive = status.GetValue<bool>("possibly_sensitive");
            RetweetedStatus = new Status(status.GetValue<JsonData>("retweeted_status"));
            var contributors = status.GetValue<JsonData>("contributors");
            Contributors =
                contributors == null ?
                    new List<Contributor>() :
                    (from JsonData contributor in contributors
                     select new Contributor(contributor))
                    .ToList();
            var coords = status.GetValue<JsonData>("coordinates");
            if (coords != null)
            {
                Coordinates = new Coordinate(coords.GetValue<JsonData>("coordinates"));
            }
            else
            {
                Coordinates = new Coordinate();
            }
            Place = new Place(status.GetValue<JsonData>("place"));
            User = new User(status.GetValue<JsonData>("user"));
            RetweetCount = status.GetValue<int>("retweet_count");
            StatusID = status.GetValue<string>("id_str");
            FavoriteCount = status.GetValue<int?>("favorite_count");
            Favorited = status.GetValue<bool>("favorited");
            InReplyToStatusID = status.GetValue<string>("in_reply_to_status_id_str");
            Source = status.GetValue<string>("source");
            CreatedAt = status.GetValue<string>("created_at").GetDate(DateTime.MaxValue);
            InReplyToUserID = status.GetValue<string>("in_reply_to_user_id_str");
            Truncated = status.GetValue<bool>("truncated");
            Text = status.GetValue<string>("text");
            Annotation = new Annotation(status.GetValue<JsonData>("annotation"));
            Entities = new Entities(status.GetValue<JsonData>("entities"));
            var currentUserRetweet = status.GetValue<JsonData>("current_user_retweet");
            if (currentUserRetweet != null)
            {
                CurrentUserRetweet = currentUserRetweet.GetValue<ulong>("id");
            }
            var scopes = status.GetValue<JsonData>("scopes");
            Scopes =
                scopes == null ? new Dictionary<string, string>() :
                (from key in (scopes as IDictionary<string, JsonData>).Keys as List<string>
                 select new
                 {
                     Key = key,
                     Value = scopes[key].ToString()
                 })
                .ToDictionary(
                    key => key.Key,
                    val => val.Value);
            WithheldCopyright = status.GetValue<bool>("withheld_copyright");
            var withheldCountries = status.GetValue<JsonData>("withheld_in_countries");
            WithheldInCountries =
                withheldCountries == null ? new List<string>() :
                (from JsonData country in status.GetValue<JsonData>("withheld_in_countries")
                 select country.ToString())
                .ToList();
            WithheldScope = status.GetValue<string>("withheld_scope");
            MetaData = new StatusMetaData(status.GetValue<JsonData>("metadata"));
            Lang = status.GetValue<string>("lang");
            string filterLvl = status.GetValue<string>("filter_level");
            try
            {
                FilterLevel =
                    filterLvl == null ? FilterLevel.None :
                    (FilterLevel)Enum.Parse(typeof(FilterLevel), filterLvl, ignoreCase: true);
            }
            catch (ArgumentException)
            {
                FilterLevel = FilterLevel.None;
            }
        }
Beispiel #6
0
        /// <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;
        }
Beispiel #7
0
        public Status(JsonElement status)
        {
            if (status.IsNull())
            {
                return;
            }

            status.TryGetProperty("retweeted_status", out JsonElement retweetedStatus);
            RetweetedStatus     = new Status(retweetedStatus);
            Retweeted           = !retweetedStatus.IsNull();
            Source              = status.GetString("source");
            InReplyToScreenName = status.GetString("in_reply_to_screen_name");
            PossiblySensitive   = status.GetBool("possibly_sensitive");
            IsQuotedStatus      = status.GetBool("is_quote_status");
            QuotedStatusID      = status.GetUlong("quoted_status_id");
            status.TryGetProperty("quoted_status", out JsonElement quotedStatusValue);
            QuotedStatus = new Status(quotedStatusValue);
            status.TryGetProperty("contributors", out JsonElement contributors);
            Contributors =
                contributors.IsNull() ?
                new List <Contributor>() :
                (from contributor in contributors.EnumerateArray()
                 select new Contributor(contributor))
                .ToList();
            status.TryGetProperty("coordinates", out JsonElement coords);
            if (!coords.IsNull())
            {
                coords.TryGetProperty("coordinates", out JsonElement coordsValue);
                Coordinates = new Coordinate(coordsValue);
            }
            else
            {
                Coordinates = new Coordinate();
            }
            status.TryGetProperty("place", out JsonElement placeValue);
            Place             = new Place(placeValue);
            RetweetCount      = status.GetInt("retweet_count");
            StatusID          = status.GetUlong("id");
            FavoriteCount     = status.GetInt("favorite_count");
            Favorited         = status.GetBool("favorited");
            InReplyToStatusID = status.GetUlong("in_reply_to_status_id");
            CreatedAt         = status.GetDate("created_at", DateTime.MaxValue);
            InReplyToUserID   = status.GetUlong("in_reply_to_user_id");
            Truncated         = status.GetBool("truncated");
            status.TryGetProperty("display_text_range", out JsonElement displayTextRange);
            if (!displayTextRange.IsNull())
            {
                JsonElement[] displayTextIndices = displayTextRange.EnumerateArray().ToArray();
                DisplayTextRange = new List <int> {
                    displayTextIndices[0].GetInt32(), displayTextIndices[1].GetInt32()
                };
            }
            TweetMode tweetMode;

            Enum.TryParse(value: status.GetString("tweet_mode"), ignoreCase: true, result: out tweetMode);
            TweetMode = tweetMode;
            Text      = status.GetString("text");
            FullText  = status.GetString("full_text");
            status.TryGetProperty("extended_tweet", out JsonElement extendedTweetValue);
            ExtendedTweet = new Status(extendedTweetValue);
            status.TryGetProperty("annotation", out JsonElement annotationValue);
            Annotation = new Annotation(annotationValue);
            status.TryGetProperty("entities", out JsonElement entitiesValue);
            Entities = new Entities(entitiesValue);
            status.TryGetProperty("extended_entities", out JsonElement extendedEntitiesValue);
            ExtendedEntities = new Entities(extendedEntitiesValue);
            status.TryGetProperty("current_user_retweet", out JsonElement currentUserRetweet);
            if (!currentUserRetweet.IsNull())
            {
                CurrentUserRetweet = currentUserRetweet.GetProperty("id").GetUInt64();
            }
            // TODO: We need a good example of a scope so we can write a test.
            //JsonElement scopes = status.GetProperty("scopes");
            //Scopes =
            //    scopes.IsNull() ? new Dictionary<string, string>() :
            //    (from key in (scopes as IDictionary<string, JsonData>).Keys as List<string>
            //     select new
            //     {
            //         Key = key,
            //         Value = scopes[key].ToString()
            //     })
            //    .ToDictionary(
            //        key => key.Key,
            //        val => val.Value);
            WithheldCopyright = status.GetBool("withheld_copyright");
            status.TryGetProperty("withheld_in_countries", out JsonElement withheldCountries);
            if (withheldCountries.IsNull())
            {
                WithheldInCountries = new List <string>();
            }
            else
            {
                WithheldInCountries =
                    (from country in withheldCountries.EnumerateArray()
                     select country.GetString())
                    .ToList();
            }
            WithheldScope = status.GetString("withheld_scope");
            status.TryGetProperty("metadata", out JsonElement metadataValue);
            MetaData = new StatusMetaData(metadataValue);
            Lang     = status.GetString("lang");
            FilterLevel filterLevel;

            Enum.TryParse(value: status.GetString("filter_level"), ignoreCase: true, result: out filterLevel);
            FilterLevel = filterLevel;
            status.TryGetProperty("user", out JsonElement userValue);
            User  = new User(userValue);
            Users = new List <ulong>();
        }