Class: Tvdbjson::Episode

Inherits:
Object
  • Object
show all
Extended by:
Requestable
Includes:
Hashable
Defined in:
lib/tvdbjson/episode.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Requestable

build_uri, send_authenticated_request

Methods included from Hashable

#to_hash

Constructor Details

#initialize(options = {}) ⇒ Episode

Returns a new instance of Episode.



9
10
11
12
13
14
15
16
17
# File 'lib/tvdbjson/episode.rb', line 9

def initialize(options = {})
  @id             = options[:id]
  @name           = options[:name]
  @series_id      = options[:series_id]
  @episode_number = options[:episode_number]
  @season_number  = options[:season_number]
  @first_aired    = options[:first_aired]
  @overview       = options[:overview]
end

Instance Attribute Details

#episode_numberObject

Returns the value of attribute episode_number.



7
8
9
# File 'lib/tvdbjson/episode.rb', line 7

def episode_number
  @episode_number
end

#first_airedObject

Returns the value of attribute first_aired.



7
8
9
# File 'lib/tvdbjson/episode.rb', line 7

def first_aired
  @first_aired
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/tvdbjson/episode.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/tvdbjson/episode.rb', line 7

def name
  @name
end

#overviewObject

Returns the value of attribute overview.



7
8
9
# File 'lib/tvdbjson/episode.rb', line 7

def overview
  @overview
end

#season_numberObject

Returns the value of attribute season_number.



7
8
9
# File 'lib/tvdbjson/episode.rb', line 7

def season_number
  @season_number
end

#series_idObject

Returns the value of attribute series_id.



7
8
9
# File 'lib/tvdbjson/episode.rb', line 7

def series_id
  @series_id
end

Class Method Details

.all_episodes(options = {}, authentication) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tvdbjson/episode.rb', line 19

def self.all_episodes(options = {}, authentication)
  raise Tvdbjson::RequiredSeriesIdMissingError if !options[:series_id]
  raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)

  hash = {
    :method       => "GET",
    :uri          => "/series/#{options[:series_id]}/episodes",
    :params       => { "page" => 1 }
  }

  # Create an empty array for all episodes
  arr = []

  # Receive the first page of results
  response = send_authenticated_request(hash, authentication)

  # Store the Page count to retrieve additional pages in the future
  page_count = get_page_count_from_response(response)

  # Whilst we have the response object for page 1, let's add it to our array
  arr.concat( Episode.from_response(response,options[:series_id]) )

  # Now to process the additional pages (starting with page 2)
  (2..page_count).each do |page_number|
    hash = {
      :method       => "GET",
      :uri          => "/series/#{options[:series_id]}/episodes",
      :params       => { "page" => page_number }
    }
    response = send_authenticated_request(hash, authentication)
    arr.concat( Episode.from_response(response,options[:series_id]) )
  end

  arr

end

.from_response(response, series_id) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tvdbjson/episode.rb', line 71

def self.from_response(response, series_id)
  results_array = []

  begin
    response.parsed_response["data"].each do |record|
      hash                  = {}
      hash[:id]             = record["id"]
      hash[:name]           = record["episodeName"]
      hash[:series_id]      = series_id
      hash[:episode_number] = record["airedEpisodeNumber"]
      hash[:season_number]  = record["airedSeasonID"]
      hash[:first_aired]    = record["firstAired"]
      hash[:overview]       = record["overview"]

      result = Episode.new(hash)
      results_array << result
    end
  rescue NoMethodError
    # Means an empty result set was found, don't do anything special
    # here as this method will return an empty array.
  end
  results_array
end

.get_page_count_from_response(response) ⇒ Object



95
96
97
# File 'lib/tvdbjson/episode.rb', line 95

def self.get_page_count_from_response(response)
  response.parsed_response["links"]["last"]
end

.search_by_season_and_episode(options = {}, authentication) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tvdbjson/episode.rb', line 56

def self.search_by_season_and_episode(options = {}, authentication)
  raise Tvdbjson::RequiredSeriesIdMissingError if !options[:series_id]
  raise Tvdbjson::RequiredSearchParamMissingError if !options[:season_number]
  raise Tvdbjson::RequiredSearchParamMissingError if !options[:episode_number]
  raise Tvdbjson::AuthenticationObjectMissingError unless authentication.kind_of?(Tvdbjson::Authentication)

  hash = {
    :method       => "GET",
    :uri          => "/series/#{options[:series_id]}/episodes/query",
    :after_action => "Tvdbjson::Episode.from_response(response, #{options[:series_id]})",
    :params       => { "airedSeason" => options[:season_number], "airedEpisode" => options[:episode_number] }
  }
  send_authenticated_request(hash, authentication)
end