Class: TVdb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tvdb/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



5
6
7
8
# File 'lib/tvdb/client.rb', line 5

def initialize(api_key)
  @api_key = api_key
  @urls = Urls.new(api_key)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/tvdb/client.rb', line 3

def api_key
  @api_key
end

#urlsObject (readonly)

Returns the value of attribute urls.



3
4
5
# File 'lib/tvdb/client.rb', line 3

def urls
  @urls
end

Instance Method Details

#get_serie_zip(id, lang = 'en') ⇒ Object



36
37
38
39
# File 'lib/tvdb/client.rb', line 36

def get_serie_zip(id, lang='en')
  zip_file = open_or_rescue(@urls[:serie_zip] % {:serie_id => id, :language => lang})
  zip_file.nil? ? nil : Zip::ZipFile.new(zip_file.path)
end

#search(name, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tvdb/client.rb', line 10

def search(name, options={})
  default_options = {:lang => 'en', :match_mode => :all}
  options = default_options.merge(options)
  
  search_url = @urls[:get_series] % {:name => URI.escape(name), :language => options[:lang]}
  
  doc = Hpricot(OpenURI.open_uri(search_url).read)
  
  ids = if options[:match_mode] == :exact
    doc.search('series').select{|s| s.search('seriesname').inner_text == name }.collect{|e| e.search('id')}.map(&:inner_text)
  else
    doc.search('series').search('id').map(&:inner_text)
  end
  
  ids.map do |sid|
    get_serie_from_zip(sid, options[:lang])
  end.compact
end

#serie_in_language(serie, lang) ⇒ Object



29
30
31
32
33
34
# File 'lib/tvdb/client.rb', line 29

def serie_in_language(serie, lang)
  return nil if !serie.respond_to?(:tvdb_id)
  return serie if lang == serie.language
  
  get_serie_from_zip(serie.tvdb_id, lang)
end