lib/tvdb2/series.rb in tvdb2-0.1.0 vs lib/tvdb2/series.rb in tvdb2-0.1.1

- old
+ new

@@ -1,84 +1,106 @@ module Tvdb2 # This class rappresent a series retrieved from TVDB api. class Series - # All series fields returned from TVDB api. - FIELDS = [ - :added, :airsDayOfWeek, :airsTime, :aliases, :banner, :firstAired, :genre, - :id, :imdbId, :lastUpdated, :network, :networkId, :overview, :rating, - :runtime, :seriesId, :seriesName, :siteRating, :siteRatingCount, :status, - :zap2itId, :errors + # Fields returned from api endpoint `GET /search/series` (search) + INDEX_FIELDS = [ + :aliases, :banner, :firstAired, :id, :network, :overview, :seriesName, + :status ] - attr_reader *FIELDS + # Other fields with {INDEX_FIELDS} returned from api endpoint `GET + # /series/{id}` + SHOW_FIELDS = [ + :added, :airsDayOfWeek, :airsTime, :genre, :imdbId, :lastUpdated, + :networkId, :rating, :runtime, :seriesId, :siteRating, :siteRatingCount, + :zap2itId + ] + # All possible data fields returned from api for a series. + FIELDS = INDEX_FIELDS + SHOW_FIELDS + + attr_reader :added, :airsDayOfWeek, :airsTime, :aliases, :banner, + :firstAired, :genre, :id, :imdbId, :lastUpdated, :network, :networkId, + :overview, :rating, :runtime, :seriesId, :seriesName, :siteRating, + :siteRatingCount, :status, :zap2itId + # FIELDS.each do |field| + # attr_reader field + # end + + INDEX_FIELDS.each do |field| + define_method field do + if !@completed.keys.include?(@client.language) + get_all_fields! + @completed[@client.language] = true + end + return instance_variable_get("@#{field}") + end + end + + SHOW_FIELDS.each do |field| + define_method field do + if !@completed.keys.include?(@client.language) || !@completed[@client.language] + get_all_fields! + @completed[@client.language] = true + end + return instance_variable_get("@#{field}") + end + end + alias_method :name, :seriesName - # @param [Client] tvdb a TVDB api client. + # @param [Client] client a TVDB api client. # @param [Hash] data the data retrieved from api. # - # @note The {Series} object may not have all fields filled because it - # can be initialized from not completed data like when is build from a - # call like {Client#search} (`GET /search/series`): in this case the api - # call return a subset of all avaiable data for the series. To get the - # complete data of a specific episode use {Series#series!} method. + # @note The {Series} object may not have all fields filled because it can + # be initialized from not completed data like when is build from the call + # {Client#search} (`GET /search/series`): in this case the api call return + # a subset of all avaiable data for the series ({INDEX_FIELDS}). But no + # warries! When you call a method to get one {SHOW_FIELDS} the library + # automatically call the endpoint `GET /series/{id}` to retrieve the + # missing fields. # @note You should never need to create this object manually. - def initialize(tvdb, data = {}) - @tvdb = tvdb + def initialize(client, data = {}) + @client = client FIELDS.each do |field| instance_variable_set("@#{field}", data[field.to_s]) end + @completed = {@client.language => data.key?('added')} end - # Get all data for this series. Calling api endpoint `GET /series/{id}`. - # - # @return [Series] the {Series} object with all fields filled - # from the api response. - # @raise [RequestError] - def series! - if self.added.nil? - s = @tvdb.series(self.id) - FIELDS.each do |field| - instance_variable_set("@#{field}", s.send(field)) - end - end - return self - end - alias_method :get_data!, :series! - # @return [TvdbStruct] return the summary of the series. # @raise [RequestError] def series_summary - @tvdb.series_summary(self.id) + @client.series_summary(self.id) end # Retrieve the episodes of the series. # # @param params (see API#episodes) # @option params (see API#episodes) # @return [Array<Episode>] # @raise [RequestError] def episodes(params = {}) - return @tvdb.episodes(self.id, params) if params && params.key?(:page) + return @client.episodes(self.id, params) if params && params.key?(:page) episodes = [] page = 1 loop do params.merge!(page: page) - result = @tvdb.episodes(self.id, params) + result = @client.episodes(self.id, params) episodes += result page += 1 break if result.size < 100 end return episodes.sort_by{|x| [x.airedSeason, x.airedEpisodeNumber]} end # @return [Array<TvdbStruct>] the list of actors in the series. # @raise [RequestError] def actors - @tvdb.actors(self.id) + @client.actors(self.id) end # Get the episode of the series identified by the index. # @param [String, Integer] index the index of the episode to retrieve. Can be and # Integer (`absoluteNumber`) or a String @@ -103,11 +125,11 @@ # @!group Instance Method Summary to retrieve images # @return [Array<TvdbStruct>] the image summary of the series. # @raise [RequestError] def images_summary - @tvdb.images_summary(self.id) + @client.images_summary(self.id) end # Retrieve the images of the series. # # @param params (see API#images) @@ -117,11 +139,11 @@ # # @example # got = client.best_search('Game of Thrones') # puts got.images(keyType: 'poster').first.fileName_url # print the url of a poster of the series def images(params) - @tvdb.images(self.id, params) + @client.images(self.id, params) end # @return [Array<TvdbStruct>] the list of fanart images. # @raise [RequestError] def fanarts @@ -181,8 +203,24 @@ ps.shuffle! if random ps.first ? ps.first.url : nil end # @!endgroup + + private + + # Get all data fields for this series. Calling api endpoint `GET + # /series/{id}`. + # + # @return [Series] the {Series} object with all fields filled + # from the api response. + # @raise [RequestError] + def get_all_fields! + s = @client.series(@id) + FIELDS.each do |field| + instance_variable_set("@#{field}", s.instance_variable_get("@#{field}")) + end + return self + end end end