Class: TheTvdb::Gateway

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/the_tvdb/gateway.rb

Constant Summary collapse

ENDPOINT =
'http://www.thetvdb.com/api/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Gateway

Returns a new instance of Gateway.



28
29
30
31
32
33
# File 'lib/the_tvdb/gateway.rb', line 28

def initialize(api_key = nil)
  @api_key = config.api_key || ENV['TVDBKEY']
  raise 'No API key was provided. Please set one as TheTvdb::Configuration.apikey or as an environment variable (e.g.: `export TVDBKEY=1234567898765432`).' if !@api_key
  @mirror = get_mirror
  @api_path = "#{@mirror}/api/#{@api_key}"
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



26
27
28
# File 'lib/the_tvdb/gateway.rb', line 26

def api_key
  @api_key
end

#api_pathObject

Returns the value of attribute api_path.



26
27
28
# File 'lib/the_tvdb/gateway.rb', line 26

def api_path
  @api_path
end

#last_updatedObject

Returns the value of attribute last_updated.



26
27
28
# File 'lib/the_tvdb/gateway.rb', line 26

def last_updated
  @last_updated
end

#mirrorObject

Returns the value of attribute mirror.



26
27
28
# File 'lib/the_tvdb/gateway.rb', line 26

def mirror
  @mirror
end

Instance Method Details

#configObject



12
13
14
# File 'lib/the_tvdb/gateway.rb', line 12

def config
  TheTvdb.configuration
end

#data_pathObject



19
20
21
# File 'lib/the_tvdb/gateway.rb', line 19

def data_path
  config.data_path
end

#endpointObject



37
38
39
# File 'lib/the_tvdb/gateway.rb', line 37

def endpoint
  ENDPOINT
end

#episodes_pathObject



22
23
24
# File 'lib/the_tvdb/gateway.rb', line 22

def episodes_path
  config.episodes_path
end

#get_episode_details(episodeid, language = 'en') ⇒ Object



77
78
79
80
81
82
83
# File 'lib/the_tvdb/gateway.rb', line 77

def get_episode_details(episodeid, language = 'en')
  file_path = "#{episodes_path}/#{episodeid}.xml"
  open(file_path, 'wb') do |file|
    file << open("#{@api_path}/episodes/#{episodeid}/#{language}.xml").read
  end
  xml_to_hash(file_path, 'Episode')
end

#get_mirrorObject



41
42
43
44
# File 'lib/the_tvdb/gateway.rb', line 41

def get_mirror
  hash = xml_to_hash "#{endpoint}#{@api_key}/mirrors.xml", 'Mirror'
  hash['mirrorpath']
end

#get_series(name) ⇒ Object



60
61
62
63
# File 'lib/the_tvdb/gateway.rb', line 60

def get_series(name)
  doc = open_xml "#{endpoint}/GetSeries.php?seriesname=#{URI.escape(name)}"
  doc.css('Series').map { |series| Hash.from_xml(series.to_s)['Series'] }
end

#get_series_package(seriesid, language = 'en') ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/the_tvdb/gateway.rb', line 65

def get_series_package(seriesid, language = 'en')
  begin
    open("#{zip_path}/#{seriesid}.zip", 'wb') do |file|
      file << open("#{@api_path}/series/#{seriesid}/all/#{language}.zip").read
    end
    unzip_file("#{zip_path}/#{seriesid}.zip", "#{data_path}/xml/#{seriesid}")
    xml_to_hash "#{data_path}/xml/#{seriesid}/#{language}.xml", 'Data'
  rescue Exception => e
    puts "Could not retrieve series package - #{@api_path}/series/#{seriesid}/all/#{language}.zip"
  end
end

#set_timeObject

Useful for first time updating



56
57
58
# File 'lib/the_tvdb/gateway.rb', line 56

def set_time
  @last_updated = time
end

#timeObject



51
52
53
# File 'lib/the_tvdb/gateway.rb', line 51

def time
  xml_to_hash "#{endpoint}/Updates.php?type=none", 'Time'
end

#update(time) ⇒ Object



46
47
48
49
# File 'lib/the_tvdb/gateway.rb', line 46

def update(time)
  raise "A time reference to get updates is required. Please use set_time to fetch a first reference" unless (time || last_updated)
  hash = xml_to_hash "#{endpoint}/Updates.php?time=#{time || last_updated}", 'Items'
end

#zip_pathObject



16
17
18
# File 'lib/the_tvdb/gateway.rb', line 16

def zip_path
  config.zip_path
end