class TMDb::Base

Public Class Methods

bad_response(response) click to toggle source

Internal: Raises an exception depending on the type of the response.

response - Result of a request.

Raises an exception.

# File lib/tmdb-api/base.rb, line 17
def self.bad_response(response)
  if response.class == HTTParty::Response
    raise ArgumentError, response['status_message']
  end
  raise StandardError, 'Unkown error'
end
new(attributes = {}) click to toggle source

Internal: creates a new TMDb::Base object.

attributes - Attributes fetched from the API.

# File lib/tmdb-api/base.rb, line 27
def initialize(attributes = {})
  set_attributes(attributes)
end

Private Instance Methods

build_objects(key, values) click to toggle source

Internal: Builds objects for the nested resources from API.

key - attribute related to the object (ex: genres, spoken_languages). values - values of the attribute.

Returns an array of objects

# File lib/tmdb-api/base.rb, line 64
def build_objects(key, values)
  klass = TMDb.const_get(key.classify)
  values.map do |attr|
    klass.new(attr)
  end
end
candidate_to_object?(value) click to toggle source

Internal: Verifies if the value is an array of hashs.

value - value to be evaluated

Returns true or false

# File lib/tmdb-api/base.rb, line 54
def candidate_to_object?(value)
  value.is_a?(Array) && !value.empty? && value.all? { |h| h.is_a?(Hash) }
end
set_attributes(attributes) click to toggle source

Internal: Sets the attributes to the object.

attributes - A hash containing the keys and values to be

set in the object.

Returns nothing

# File lib/tmdb-api/base.rb, line 39
def set_attributes(attributes)
  attributes.each do |key, value|
    if candidate_to_object?(value)
      next unless TMDb.const_defined?(key.classify)
      value = build_objects(key, value)
    end
    self.instance_variable_set("@#{key}", value)
  end
end