Class: Tvdb2::Client

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
HTTParty, API
Defined in:
lib/tvdb2/client.rb

Overview

This class works as a client to retrieve data from TVDB json api version 2. Make http requests to api.thetvdb.com.

This class cache all http requests so only the first time a request is made (Memoist gem is used).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#actors, #best_search, #episode, #episodes, #images, #images_summary, #languages, #search, #series, #series_summary

Constructor Details

#initialize(apikey:, language: 'en') ⇒ Client

Create an object client. Take 2 keyword arguments:

Parameters:

  • apikey (String)

    your tvdb apikey (you can get one at thetvdb.com/?tab=apiregister). Required.

  • language (Symbol, String)

    the language in which you want get data. You can change later. Optional. Default is nil that is EN.

Raises:



42
43
44
45
46
47
# File 'lib/tvdb2/client.rb', line 42

def initialize(apikey:, language: 'en')
  @language = language
  response = post('/login', apikey: apikey)
  raise RequestError.new(response) if response.code != 200
  @token = response.parsed_response['token']
end

Instance Attribute Details

#languageObject

The language in which you want get data.

Examples:

got = client.best_search('Game of Thrones')
puts got.name         # print 'Game of Thrones'
client.language = :it # change language to italian
got.series!           # make a request to get new data
puts got.name         # print 'Il Trono di Spade'


28
29
30
# File 'lib/tvdb2/client.rb', line 28

def language
  @language
end

Class Method Details

.image_url(path) ⇒ String

Helper method to get the full url of an image from the relative path retrieved from api.

Examples:

got = client.best_search('Game of Thrones')
puts got.posters.first.fileName # posters/121361-1.jpg
puts TVDB.image_url(got.posters.first.fileName) # http://thetvdb.com/banners/posters/121361-1.jpg
# or
puts got.posters.first.fileName_url # http://thetvdb.com/banners/posters/121361-1.jpg

Parameters:

  • path (String)

    the relative path of an image.

Returns:

  • (String)

    the complete url of the image.



91
92
93
# File 'lib/tvdb2/client.rb', line 91

def self.image_url(path)
  URI::join("https://thetvdb.com/banners/", path).to_s
end

Instance Method Details

#refresh_token!String

Refresh your api token.

Returns:

  • (String)

    the new token

Raises:



52
53
54
55
56
57
# File 'lib/tvdb2/client.rb', line 52

def refresh_token!
  response = get('/refresh_token')
  raise RequestError.new(response) if response.code != 200
  @token = response['token']
  return @token
end

#with_language(locale) { ... } ⇒ Object

Inside the block change the language in which you want get data.

Examples:

got = client.best_search('Game of Thrones')
ep = got['3x9']
client.with_language(:it) do |c|
  puts ep.name # print the title of episode 3x9 in italian
end
puts ep.name   # print the title of episode 3x9 in english

Parameters:

  • locale (Symbol, String)

    the language in which you want get data.

Yields:

  • block called with the selected language.



71
72
73
74
75
76
# File 'lib/tvdb2/client.rb', line 71

def with_language(locale, &block)
  tmp_language = @language
  @language = locale.to_s
  block.call(self)
  @language = tmp_language
end