Example #1
0
        public async Task Test_RatingObjectJsonWriter_WriteObject_StringWriter_Only_Distribution_Property()
        {
            ITraktRating traktRating = new TraktRating
            {
                Distribution = new Dictionary <string, int>
                {
                    ["1"]  = 1,
                    ["2"]  = 2,
                    ["3"]  = 3,
                    ["4"]  = 4,
                    ["5"]  = 5,
                    ["6"]  = 6,
                    ["7"]  = 7,
                    ["8"]  = 8,
                    ["9"]  = 9,
                    ["10"] = 10
                }
            };

            using (var stringWriter = new StringWriter())
            {
                var    traktJsonWriter = new RatingObjectJsonWriter();
                string json            = await traktJsonWriter.WriteObjectAsync(stringWriter, traktRating);

                json.Should().Be(@"{""distribution"":{""1"":1,""2"":2,""3"":3,""4"":4,""5"":5,""6"":6,""7"":7,""8"":8,""9"":9,""10"":10}}");
            }
        }
Example #2
0
 public async Task Test_RatingObjectJsonWriter_WriteObject_StringWriter_Exceptions()
 {
     var                   traktJsonWriter = new RatingObjectJsonWriter();
     ITraktRating          traktRating     = new TraktRating();
     Func <Task <string> > action          = () => traktJsonWriter.WriteObjectAsync(default(StringWriter), traktRating);
     await action.Should().ThrowAsync <ArgumentNullException>();
 }
        public async Task Test_RatingObjectJsonWriter_WriteObject_JsonWriter_Only_Distribution_Property_Incomplete()
        {
            ITraktRating traktRating = new TraktRating
            {
                Distribution = new Dictionary <string, int>
                {
                    ["1"] = 1,
                    ["2"] = 2,
                    ["3"] = 3,
                    ["5"] = 5,
                    ["6"] = 6,
                    ["8"] = 8,
                    ["9"] = 9
                }
            };

            using (var stringWriter = new StringWriter())
                using (var jsonWriter = new JsonTextWriter(stringWriter))
                {
                    var traktJsonWriter = new RatingObjectJsonWriter();
                    await traktJsonWriter.WriteObjectAsync(jsonWriter, traktRating);

                    stringWriter.ToString().Should().Be(@"{""distribution"":{""1"":1,""2"":2,""3"":3,""4"":0,""5"":5,""6"":6,""7"":0,""8"":8,""9"":9,""10"":0}}");
                }
        }
        public async Task Test_RatingObjectJsonWriter_WriteObject_JsonWriter_Complete()
        {
            ITraktRating traktRating = new TraktRating
            {
                Rating       = 7.4561f,
                Votes        = 2453,
                Distribution = new Dictionary <string, int>
                {
                    ["1"]  = 1,
                    ["2"]  = 2,
                    ["3"]  = 3,
                    ["4"]  = 4,
                    ["5"]  = 5,
                    ["6"]  = 6,
                    ["7"]  = 7,
                    ["8"]  = 8,
                    ["9"]  = 9,
                    ["10"] = 10
                }
            };

            using (var stringWriter = new StringWriter())
                using (var jsonWriter = new JsonTextWriter(stringWriter))
                {
                    var traktJsonWriter = new RatingObjectJsonWriter();
                    await traktJsonWriter.WriteObjectAsync(jsonWriter, traktRating);

                    stringWriter.ToString().Should().Be(@"{""rating"":7.4561,""votes"":2453," +
                                                        @"""distribution"":{""1"":1,""2"":2,""3"":3,""4"":4,""5"":5,""6"":6,""7"":7,""8"":8,""9"":9,""10"":10}}");
                }
        }
        public void TestTraktRatingDefaultConstructor()
        {
            var rating = new TraktRating();

            rating.Rating.Should().NotHaveValue();
            rating.Votes.Should().NotHaveValue();
            rating.Distribution.Should().BeNull();
        }
        public void Test_TraktRating_Default_Constructor()
        {
            var traktRating = new TraktRating();

            traktRating.Rating.Should().BeNull();
            traktRating.Votes.Should().BeNull();
            traktRating.Distribution.Should().BeNull();
        }
        public void Test_RatingObjectJsonWriter_WriteObject_JsonWriter_Exceptions()
        {
            var          traktJsonWriter = new RatingObjectJsonWriter();
            ITraktRating traktRating     = new TraktRating();
            Func <Task>  action          = () => traktJsonWriter.WriteObjectAsync(default(JsonTextWriter), traktRating);

            action.Should().Throw <ArgumentNullException>();
        }
Example #8
0
        public async Task Test_RatingObjectJsonWriter_WriteObject_Object_Only_Rating_Property()
        {
            ITraktRating traktRating = new TraktRating
            {
                Rating = 7.4561f
            };

            var    traktJsonWriter = new RatingObjectJsonWriter();
            string json            = await traktJsonWriter.WriteObjectAsync(traktRating);

            json.Should().Be(@"{""rating"":7.4561}");
        }
Example #9
0
        public async Task Test_RatingObjectJsonWriter_WriteObject_Object_Only_Votes_Property()
        {
            ITraktRating traktRating = new TraktRating
            {
                Votes = 2453
            };

            var    traktJsonWriter = new RatingObjectJsonWriter();
            string json            = await traktJsonWriter.WriteObjectAsync(traktRating);

            json.Should().Be(@"{""votes"":2453}");
        }
Example #10
0
        public override async Task <ITraktRating> ReadObjectAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
        {
            if (jsonReader == null)
            {
                return(await Task.FromResult(default(ITraktRating)));
            }

            if (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.StartObject)
            {
                ITraktRating traktRating = new TraktRating();

                while (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.PropertyName)
                {
                    var propertyName = jsonReader.Value.ToString();

                    switch (propertyName)
                    {
                    case JsonProperties.RATING_PROPERTY_NAME_RATING:
                    {
                        var value = await JsonReaderHelper.ReadFloatValueAsync(jsonReader, cancellationToken);

                        if (value.First)
                        {
                            traktRating.Rating = value.Second;
                        }

                        break;
                    }

                    case JsonProperties.RATING_PROPERTY_NAME_VOTES:
                        traktRating.Votes = await jsonReader.ReadAsInt32Async(cancellationToken);

                        break;

                    case JsonProperties.RATING_PROPERTY_NAME_DISTRIBUTION:
                        traktRating.Distribution = await JsonReaderHelper.ReadDistributionAsync(jsonReader, cancellationToken);

                        break;

                    default:
                        await JsonReaderHelper.ReadAndIgnoreInvalidContentAsync(jsonReader, cancellationToken);

                        break;
                    }
                }

                return(traktRating);
            }

            return(await Task.FromResult(default(ITraktRating)));
        }
        public async Task Test_RatingObjectJsonWriter_WriteObject_JsonWriter_Only_Votes_Property()
        {
            ITraktRating traktRating = new TraktRating
            {
                Votes = 2453
            };

            using (var stringWriter = new StringWriter())
                using (var jsonWriter = new JsonTextWriter(stringWriter))
                {
                    var traktJsonWriter = new RatingObjectJsonWriter();
                    await traktJsonWriter.WriteObjectAsync(jsonWriter, traktRating);

                    stringWriter.ToString().Should().Be(@"{""votes"":2453}");
                }
        }
        public async Task Test_RatingObjectJsonWriter_WriteObject_JsonWriter_Only_Rating_Property()
        {
            ITraktRating traktRating = new TraktRating
            {
                Rating = 7.4561f
            };

            using (var stringWriter = new StringWriter())
                using (var jsonWriter = new JsonTextWriter(stringWriter))
                {
                    var traktJsonWriter = new RatingObjectJsonWriter();
                    await traktJsonWriter.WriteObjectAsync(jsonWriter, traktRating);

                    stringWriter.ToString().Should().Be(@"{""rating"":7.4561}");
                }
        }
        /// <summary>Rate an episode by ID</summary>
        /// <param name="episodeId">The episode ID</param>
        /// <param name="episodeIdType">The episode ID type</param>
        /// <param name="rating">The rating</param>
        /// <param name="ratedAt">The UTC date when the rating was made</param>
        /// <returns>See summary</returns>
        public async Task <TraktAddResponse> AddRatingByEpisodeIdAsync(string episodeId, TraktTextEpisodeIdType episodeIdType, TraktRating rating, DateTime?ratedAt = null)
        {
            var obj = TraktEpisodeFactory.FromId <TraktEpisodeWithRatingsMetadata>(episodeId, episodeIdType);

            obj.Rating  = rating;
            obj.RatedAt = ratedAt;
            return(await AddRatingsAsync(obj));
        }
        /// <summary>Rate a show by ID</summary>
        /// <param name="showId">The show ID</param>
        /// <param name="showIdType">The show ID type</param>
        /// <param name="rating">The rating</param>
        /// <param name="ratedAt">The UTC date when the rating was made</param>
        /// <param name="seasonNumbers">If set, the action will be applied to the specified season numbers instead of the show itself</param>
        /// <returns>See summary</returns>
        public async Task <TraktAddResponse> AddRatingByShowIdAsync(int showId, TraktNumericShowIdType showIdType, TraktRating rating, DateTime?ratedAt = null, IEnumerable <int> seasonNumbers = null)
        {
            var obj = TraktShowFactory.FromId <TraktShowWithRatingsMetadata>(showId, showIdType);

            obj.Rating  = rating;
            obj.RatedAt = ratedAt;
            if (seasonNumbers != null)
            {
                obj.Seasons = seasonNumbers.Select(s => new TraktSeasonWithRatingsMetadata {
                    SeasonNumber = s
                }).ToList();
            }
            return(await AddRatingsAsync(obj));
        }
        /// <summary>Rate a movie by ID</summary>
        /// <param name="movieId">The movie ID</param>
        /// <param name="movieIdType">The movie ID type</param>
        /// <param name="rating">The rating</param>
        /// <param name="ratedAt">The UTC date when the rating was made</param>
        /// <returns>See summary</returns>
        public async Task <TraktAddResponse> AddRatingByMovieIdAsync(int movieId, TraktNumericMovieIdType movieIdType, TraktRating rating, DateTime?ratedAt = null)
        {
            var obj = TraktMovieFactory.FromId <TraktMovieWithRatingsMetadata>(movieId, movieIdType);

            obj.Rating  = rating;
            obj.RatedAt = ratedAt;
            return(await AddRatingsAsync(obj));
        }
Example #16
0
 /// <summary>Get a user's ratings filtered by type. You can optionally filter for a specific rating between 1 and 10.</summary>
 /// <param name="rating">The rating</param>
 /// <param name="extended">Changes which properties are populated for standard media objects. By default, minimal data is returned. Change this if additional fields are required in the returned data.</param>
 /// <returns>See summary</returns>
 public async Task <IEnumerable <TraktRatingsSeasonsResponseItem> > GetSeasonRatingsAsync(TraktRating rating = TraktRating.Unspecified, TraktExtendedOption extended = TraktExtendedOption.Unspecified)
 {
     return(await SendAsync(new TraktSyncRatingsSeasonsRequest(Client) { Rating = rating, Extended = extended }));
 }
Example #17
0
 /// <summary>Get a user's ratings filtered by type. You can optionally filter for a specific rating between 1 and 10.</summary>
 /// <param name="username">The user's Trakt username</param>
 /// <param name="rating">The rating</param>
 /// <param name="extended">Changes which properties are populated for standard media objects. By default, minimal data is returned. Change this if additional fields are required in the returned data.</param>
 /// <returns>See summary</returns>
 public async Task <IEnumerable <TraktRatingsEpisodesResponseItem> > GetEpisodeRatingsAsync(string username = _me, TraktRating rating = TraktRating.Unspecified, TraktExtendedOption extended = TraktExtendedOption.Unspecified)
 {
     return(await SendAsync(new TraktUsersRatingsEpisodesRequest(Client) {
         Username = username,
         Rating = rating,
         Extended = extended
     }));
 }