tvdb2 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: baf228f33001b998ffe4e51e2f5eb125e82207a9
4
- data.tar.gz: ec4b8845798bc1479584c053eb8cf6ab5974fb37
3
+ metadata.gz: 24469dc02a86ad88f6ea8383a9dced2cff7743d7
4
+ data.tar.gz: 47bd77a6f6a72891d0f4104a9c2f9b6295ae2681
5
5
  SHA512:
6
- metadata.gz: 011fa05b1d23010ee94b08900a036f5ee650d36b2d0505ee05a0f38c25947d939f5be036111d740dc99bde64c164d6fb1875e133aa1e36ade391f4523a21cd47
7
- data.tar.gz: bd63f54b18a16de2c6deaeabe57ab1971b13566c7b536e2b88dd58ced9ad264ab9705e4638ec5d18f9f0434d52c4cd52389a226d247d5ec7aa7f3c25d6809b5b
6
+ metadata.gz: 1fc56697b7e19c29835735c0ae75a2ff8a503f8d30c816bf46991d7ba3737cead0b1c96472c10fafe296a15e744122b3a2596deaa62be2c75658cb7a0e6687ff
7
+ data.tar.gz: cc1c1e3cd58c5dcdb8911a2fe3d833bee8445515cb383ad11eaab6618b43541aa79eb112a1cbb8f2ba87b2fc6f9a172d5a955d40ad8546ff3ca2750f9246d384
data/README.md CHANGED
@@ -89,13 +89,13 @@ images = got.season_images(season: 2)
89
89
  puts images.first.url
90
90
 
91
91
  # Switch language
92
+ ep = got['3x9']
92
93
  client.language = 'it'
93
- ep = got['3x9'] # retrieve the episodes with the new language
94
- puts ep.name
94
+ puts ep.name # print episode title in italian
95
95
  client.language = 'en'
96
+ puts ep.name # print episode title in english
96
97
  # or you can swith language only in a block
97
98
  client.with_language(:it) do
98
- ep = got['3x9']
99
99
  puts ep.name
100
100
  end
101
101
  ```
@@ -39,7 +39,7 @@ module Tvdb2
39
39
  # https://thetvdb.com/?tab=apiregister). Required.
40
40
  # @param [Symbol, String] language the language in which you want get data.
41
41
  # You can change later. Optional. Default is `nil` that is `EN`.
42
- def initialize(apikey:, language: nil)
42
+ def initialize(apikey:, language: 'en')
43
43
  @language = language
44
44
  response = post('/login', apikey: apikey)
45
45
  raise RequestError.new(response) if response.code != 200
@@ -60,12 +60,11 @@ module Tvdb2
60
60
  #
61
61
  # @example
62
62
  # got = client.best_search('Game of Thrones')
63
+ # ep = got['3x9']
63
64
  # client.with_language(:it) do |c|
64
- # ep = got['1x1'] # Get episode data in italian
65
- # puts ep.name # print the title of episode 1x1 in italian
65
+ # puts ep.name # print the title of episode 3x9 in italian
66
66
  # end
67
- # ep = got['1x1'] # Get episode data in default language
68
- # puts ep.name # print the title of episode 1x1 in english
67
+ # puts ep.name # print the title of episode 3x9 in english
69
68
  #
70
69
  # @param [Symbol, String] locale the language in which you want get data.
71
70
  # @yield block called with the selected language.
@@ -98,12 +97,14 @@ module Tvdb2
98
97
  # :nodoc:
99
98
  # language param is required to invalidate memoist cache on different language
100
99
  def get(path, params = {}, language = @language)
100
+ puts ">>> GET API REQUEST to '#{path}' with language '#{language}'" if ENV['DEBUG']
101
101
  self.class.get(URI.escape(path), headers: build_headers, query: params)
102
102
  end
103
103
  memoize :get
104
104
 
105
105
  # :nodoc:
106
106
  def post(path, params = {}, language = @language)
107
+ puts ">>> POST API REQUEST to '#{path}' with language '#{language}'" if ENV['DEBUG']
107
108
  self.class.post(URI.escape(path), headers: build_headers, body: params.to_json)
108
109
  end
109
110
  memoize :post
@@ -147,5 +148,5 @@ module Tvdb2
147
148
  end
148
149
  end
149
150
 
150
- # Alias of {Client}
151
+ # Alias of {Tvdb2::Client}
151
152
  TVDB = Tvdb2::Client
@@ -3,60 +3,97 @@ module Tvdb2
3
3
  # This class rappresent a series episode retrieved from TVDB api.
4
4
  class Episode
5
5
 
6
- # All episode fields returned from TVDB api.
7
- FIELDS = [
8
- :absoluteNumber, :airedEpisodeNumber, :airedSeason,
6
+ # Fields returned from api endpoint `GET /series/{id}/episodes`
7
+ INDEX_FIELDS = [
8
+ :absoluteNumber, :airedEpisodeNumber, :airedSeason,:dvdEpisodeNumber,
9
+ :dvdSeason, :episodeName, :firstAired, :id, :lastUpdated, :overview
10
+ ]
11
+
12
+ # Other fields with {INDEX_FIELDS} returned from api endpoint `GET
13
+ # /episodes/{id}`
14
+ SHOW_FIELDS = [
15
+ :airsAfterSeason, :airsBeforeEpisode, :airsBeforeSeason, :direcotor,
16
+ :directors, :dvdChapter, :dvdDiscid, :filename, :guestStars, :imdbId,
17
+ :lastUpdatedBy, :productionCode, :seriesId, :showUrl, :siteRating,
18
+ :siteRatingCount, :thumbAdded, :thumbAuthor, :thumbHeight, :thumbWidth,
19
+ :writers
20
+ ]
21
+
22
+ # All possible data fields returned from api for a series.
23
+ FIELDS = INDEX_FIELDS + SHOW_FIELDS
24
+
25
+ attr_reader :absoluteNumber, :airedEpisodeNumber, :airedSeason,
9
26
  :airsAfterSeason, :airsBeforeEpisode, :airsBeforeSeason, :direcotor,
10
27
  :directors, :dvdChapter, :dvdDiscid, :dvdEpisodeNumber, :dvdSeason,
11
28
  :episodeName, :filename, :firstAired, :guestStars, :id, :imdbId,
12
29
  :lastUpdated, :lastUpdatedBy, :overview, :productionCode, :seriesId,
13
30
  :showUrl, :siteRating, :siteRatingCount, :thumbAdded, :thumbAuthor,
14
- :thumbHeight, :thumbWidth, :writers, :errors
15
- ]
31
+ :thumbHeight, :thumbWidth, :writers
16
32
 
17
- attr_reader *FIELDS
33
+ INDEX_FIELDS.each do |field|
34
+ define_method field do
35
+ if !@completed.keys.include?(@client.language)
36
+ get_all_fields!
37
+ @completed[@client.language] = true
38
+ end
39
+ return instance_variable_get("@#{field}")
40
+ end
41
+ end
42
+
43
+ SHOW_FIELDS.each do |field|
44
+ define_method field do
45
+ if !@completed.keys.include?(@client.language) || !@completed[@client.language]
46
+ get_all_fields!
47
+ @completed[@client.language] = true
48
+ end
49
+ return instance_variable_get("@#{field}")
50
+ end
51
+ end
18
52
 
19
53
  alias_method :name, :episodeName
20
54
  alias_method :number, :airedEpisodeNumber
21
55
  alias_method :seasonNumber, :airedSeason
22
56
 
23
- # @param [Client] tvdb a TVDB api client.
57
+ # @param [Client] client a TVDB api client.
24
58
  # @param [Hash] data the data retrieved from api.
25
59
  #
26
- # @note The Episode object may not have all fields filled because it can be
27
- # initialized from not completed data like when is build from a call like
28
- # `series.episodes` (`GET /series/{id}/episodes`): in this case the api
29
- # call return a subset of all avaiable data for the episodes. To get the
30
- # complete data of a specific episode use `#episode!` method.
60
+ # @note The {Episode} object may not have all fields filled because it can
61
+ # be initialized from not completed data like when is build from the call
62
+ # {Series#episodes} (`GET /series/{id}/episodes`): in this case the api
63
+ # call return a subset of all avaiable data for the episodes
64
+ # ({INDEX_FIELDS}). But no warries! When you call a method to get one
65
+ # {SHOW_FIELDS} the library automatically call the endpoint `GET
66
+ # /episodes/{id}` to retrieve the missing fields.
31
67
  # @note You should never need to create this object manually.
32
- def initialize(tvdb, data = {})
33
- @tvdb = tvdb
68
+ def initialize(client, data = {})
69
+ @client = client
34
70
  FIELDS.each do |field|
35
71
  instance_variable_set("@#{field}", data[field.to_s])
36
72
  end
73
+ @completed = {@client.language => data.key?('seriesId')}
37
74
  end
38
75
 
39
- # Get all data for this episode. Calling api endpoint `GET /episodes/{id}`.
76
+ # @return [String] the episode number with the "_x_" syntax:
77
+ # `"#{season_number}x#{episode_number}` (3x9)".
78
+ def x
79
+ "#{self.airedSeason}x#{self.airedEpisodeNumber}"
80
+ end
81
+
82
+ private
83
+
84
+ # Get all data fields for this episode. Calling api endpoint `GET
85
+ # /episodes/{id}`.
40
86
  #
41
87
  # @return [Episode] the episode object with all fields filled from
42
88
  # the api response.
43
89
  # @raise [RequestError]
44
- def episode!
45
- if self.lastUpdated.nil?
46
- e = @tvdb.episode(self.id)
47
- FIELDS.each do |field|
48
- instance_variable_set("@#{field}", e.send(field))
49
- end
90
+ def get_all_fields!
91
+ e = @client.episode(@id)
92
+ FIELDS.each do |field|
93
+ instance_variable_set("@#{field}", e.send(field))
50
94
  end
51
95
  return self
52
96
  end
53
- alias_method :get_data!, :episode!
54
-
55
- # @return [String] the episode number with the "_x_" syntax:
56
- # `"#{season_number}x#{episode_number}` (3x9)".
57
- def x
58
- "#{self.airedSeason}x#{self.airedEpisodeNumber}"
59
- end
60
97
 
61
98
  end
62
99
  end
@@ -3,54 +3,76 @@ module Tvdb2
3
3
  # This class rappresent a series retrieved from TVDB api.
4
4
  class Series
5
5
 
6
- # All series fields returned from TVDB api.
7
- FIELDS = [
8
- :added, :airsDayOfWeek, :airsTime, :aliases, :banner, :firstAired, :genre,
9
- :id, :imdbId, :lastUpdated, :network, :networkId, :overview, :rating,
10
- :runtime, :seriesId, :seriesName, :siteRating, :siteRatingCount, :status,
11
- :zap2itId, :errors
6
+ # Fields returned from api endpoint `GET /search/series` (search)
7
+ INDEX_FIELDS = [
8
+ :aliases, :banner, :firstAired, :id, :network, :overview, :seriesName,
9
+ :status
12
10
  ]
13
11
 
14
- attr_reader *FIELDS
12
+ # Other fields with {INDEX_FIELDS} returned from api endpoint `GET
13
+ # /series/{id}`
14
+ SHOW_FIELDS = [
15
+ :added, :airsDayOfWeek, :airsTime, :genre, :imdbId, :lastUpdated,
16
+ :networkId, :rating, :runtime, :seriesId, :siteRating, :siteRatingCount,
17
+ :zap2itId
18
+ ]
19
+
20
+ # All possible data fields returned from api for a series.
21
+ FIELDS = INDEX_FIELDS + SHOW_FIELDS
22
+
23
+ attr_reader :added, :airsDayOfWeek, :airsTime, :aliases, :banner,
24
+ :firstAired, :genre, :id, :imdbId, :lastUpdated, :network, :networkId,
25
+ :overview, :rating, :runtime, :seriesId, :seriesName, :siteRating,
26
+ :siteRatingCount, :status, :zap2itId
27
+ # FIELDS.each do |field|
28
+ # attr_reader field
29
+ # end
30
+
31
+ INDEX_FIELDS.each do |field|
32
+ define_method field do
33
+ if !@completed.keys.include?(@client.language)
34
+ get_all_fields!
35
+ @completed[@client.language] = true
36
+ end
37
+ return instance_variable_get("@#{field}")
38
+ end
39
+ end
40
+
41
+ SHOW_FIELDS.each do |field|
42
+ define_method field do
43
+ if !@completed.keys.include?(@client.language) || !@completed[@client.language]
44
+ get_all_fields!
45
+ @completed[@client.language] = true
46
+ end
47
+ return instance_variable_get("@#{field}")
48
+ end
49
+ end
15
50
 
16
51
  alias_method :name, :seriesName
17
52
 
18
- # @param [Client] tvdb a TVDB api client.
53
+ # @param [Client] client a TVDB api client.
19
54
  # @param [Hash] data the data retrieved from api.
20
55
  #
21
- # @note The {Series} object may not have all fields filled because it
22
- # can be initialized from not completed data like when is build from a
23
- # call like {Client#search} (`GET /search/series`): in this case the api
24
- # call return a subset of all avaiable data for the series. To get the
25
- # complete data of a specific episode use {Series#series!} method.
56
+ # @note The {Series} object may not have all fields filled because it can
57
+ # be initialized from not completed data like when is build from the call
58
+ # {Client#search} (`GET /search/series`): in this case the api call return
59
+ # a subset of all avaiable data for the series ({INDEX_FIELDS}). But no
60
+ # warries! When you call a method to get one {SHOW_FIELDS} the library
61
+ # automatically call the endpoint `GET /series/{id}` to retrieve the
62
+ # missing fields.
26
63
  # @note You should never need to create this object manually.
27
- def initialize(tvdb, data = {})
28
- @tvdb = tvdb
64
+ def initialize(client, data = {})
65
+ @client = client
29
66
  FIELDS.each do |field|
30
67
  instance_variable_set("@#{field}", data[field.to_s])
31
68
  end
69
+ @completed = {@client.language => data.key?('added')}
32
70
  end
33
71
 
34
- # Get all data for this series. Calling api endpoint `GET /series/{id}`.
35
- #
36
- # @return [Series] the {Series} object with all fields filled
37
- # from the api response.
38
- # @raise [RequestError]
39
- def series!
40
- if self.added.nil?
41
- s = @tvdb.series(self.id)
42
- FIELDS.each do |field|
43
- instance_variable_set("@#{field}", s.send(field))
44
- end
45
- end
46
- return self
47
- end
48
- alias_method :get_data!, :series!
49
-
50
72
  # @return [TvdbStruct] return the summary of the series.
51
73
  # @raise [RequestError]
52
74
  def series_summary
53
- @tvdb.series_summary(self.id)
75
+ @client.series_summary(self.id)
54
76
  end
55
77
 
56
78
  # Retrieve the episodes of the series.
@@ -60,12 +82,12 @@ module Tvdb2
60
82
  # @return [Array<Episode>]
61
83
  # @raise [RequestError]
62
84
  def episodes(params = {})
63
- return @tvdb.episodes(self.id, params) if params && params.key?(:page)
85
+ return @client.episodes(self.id, params) if params && params.key?(:page)
64
86
  episodes = []
65
87
  page = 1
66
88
  loop do
67
89
  params.merge!(page: page)
68
- result = @tvdb.episodes(self.id, params)
90
+ result = @client.episodes(self.id, params)
69
91
  episodes += result
70
92
  page += 1
71
93
  break if result.size < 100
@@ -76,7 +98,7 @@ module Tvdb2
76
98
  # @return [Array<TvdbStruct>] the list of actors in the series.
77
99
  # @raise [RequestError]
78
100
  def actors
79
- @tvdb.actors(self.id)
101
+ @client.actors(self.id)
80
102
  end
81
103
 
82
104
  # Get the episode of the series identified by the index.
@@ -105,7 +127,7 @@ module Tvdb2
105
127
  # @return [Array<TvdbStruct>] the image summary of the series.
106
128
  # @raise [RequestError]
107
129
  def images_summary
108
- @tvdb.images_summary(self.id)
130
+ @client.images_summary(self.id)
109
131
  end
110
132
 
111
133
  # Retrieve the images of the series.
@@ -119,7 +141,7 @@ module Tvdb2
119
141
  # got = client.best_search('Game of Thrones')
120
142
  # puts got.images(keyType: 'poster').first.fileName_url # print the url of a poster of the series
121
143
  def images(params)
122
- @tvdb.images(self.id, params)
144
+ @client.images(self.id, params)
123
145
  end
124
146
 
125
147
  # @return [Array<TvdbStruct>] the list of fanart images.
@@ -184,5 +206,21 @@ module Tvdb2
184
206
 
185
207
  # @!endgroup
186
208
 
209
+ private
210
+
211
+ # Get all data fields for this series. Calling api endpoint `GET
212
+ # /series/{id}`.
213
+ #
214
+ # @return [Series] the {Series} object with all fields filled
215
+ # from the api response.
216
+ # @raise [RequestError]
217
+ def get_all_fields!
218
+ s = @client.series(@id)
219
+ FIELDS.each do |field|
220
+ instance_variable_set("@#{field}", s.instance_variable_get("@#{field}"))
221
+ end
222
+ return self
223
+ end
224
+
187
225
  end
188
226
  end
@@ -1,4 +1,4 @@
1
1
  module Tvdb2
2
2
  # The library version.
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tvdb2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - pioz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-04 00:00:00.000000000 Z
11
+ date: 2017-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler