Class: ThetvdbApi::Request::Base

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

Direct Known Subclasses

Actor, Banner, Episode, Search, Series, Update

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Base

Returns a new instance of Base.



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

def initialize(uri)
  @uri = uri
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Class Method Details

.api_keyObject



8
9
10
# File 'lib/thetvdb_api/request/base.rb', line 8

def self.api_key
  ThetvdbApi::Configuration.api_key
end

Instance Method Details

#array_mapped(klass, key) ⇒ Object



43
44
45
# File 'lib/thetvdb_api/request/base.rb', line 43

def array_mapped(klass, key)
  array_normalize(key).map { |data| klass.new(data) }
end

#array_normalize(key) ⇒ Object



39
40
41
# File 'lib/thetvdb_api/request/base.rb', line 39

def array_normalize(key)
  dig(key).is_a?(Array) ? dig(key) : [dig(key)]
end

#collection_response(key, klass) ⇒ Object



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

def collection_response(key, klass)
  response_condition? ? array_mapped(klass, key) : []
end

#dataObject



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

def data
  response[data_key]
end

#dig(key) ⇒ Object



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

def dig(key)
  data[key]
end

#object_response(key, klass) ⇒ Object



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

def object_response(key, klass)
  response_condition? ? klass.new(data[key]) : nil
end

#request_options(options = {}) ⇒ Object



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

def request_options(options = {})
  {
    query: options,
    base_uri: ThetvdbApi::Configuration.api_url
  }
end

#responseObject



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

def response
  @response ||= self.class.get(uri, request_options)
end

#response_condition?Boolean

Returns:

  • (Boolean)


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

def response_condition?
  response.code == 200 && data.is_a?(Hash)
end