Class: ThetvdbApi::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/thetvdb_api/base.rb

Direct Known Subclasses

Actor, Banner, Episode, Search, Series, Update

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base



5
6
7
8
9
# File 'lib/thetvdb_api/base.rb', line 5

def initialize(client)
  @client = client
  @params = {}
  @mapper = nil
end

Instance Method Details

#api_keyObject



60
61
62
# File 'lib/thetvdb_api/base.rb', line 60

def api_key
  @client.api_key
end

#connectionObject



11
12
13
# File 'lib/thetvdb_api/base.rb', line 11

def connection
  @connection ||= Faraday.new(url: base_url)
end

#get(uri) ⇒ Object



15
16
17
18
# File 'lib/thetvdb_api/base.rb', line 15

def get(uri)
  @uri_template = URITemplate.new(uri)
  self
end

#languageObject



56
57
58
# File 'lib/thetvdb_api/base.rb', line 56

def language
  @client.language
end

#params(options) ⇒ Object



20
21
22
23
24
# File 'lib/thetvdb_api/base.rb', line 20

def params(options)
  @mapper = options.delete(:mapper)
  @params = { language: @client.language }.merge(options)
  self
end

#prepare_uriObject



31
32
33
34
# File 'lib/thetvdb_api/base.rb', line 31

def prepare_uri
  assert_uri_template
  @uri_template.expand(@params.merge(api_key: api_key))
end

#responseObject



26
27
28
29
# File 'lib/thetvdb_api/base.rb', line 26

def response
  assert_uri_template
  ThetvdbApi::Response.new(connection.get(uri, @options), @mapper)
end

#restful_param_keys(uri_expanded) ⇒ Object



47
48
49
# File 'lib/thetvdb_api/base.rb', line 47

def restful_param_keys(uri_expanded)
  @uri_template.extract(uri_expanded).keys
end

#series_uriObject



52
53
54
# File 'lib/thetvdb_api/base.rb', line 52

def series_uri
  '{api_key}/series/{series_id}'
end

#uriObject



40
41
42
43
44
45
# File 'lib/thetvdb_api/base.rb', line 40

def uri
  uri = prepare_uri
  @params.reject!{ |param| restful_param_keys(uri).include?(param.to_s) }

  uri
end

#urlObject



36
37
38
# File 'lib/thetvdb_api/base.rb', line 36

def url
  "#{base_url}#{uri}"
end