Module: TMDb

Extended by:
TMDb
Included in:
TMDb
Defined in:
lib/tmdb.rb,
lib/tmdb/person.rb,
lib/tmdb/version.rb,
lib/tmdb/attributes.rb,
lib/tmdb/null_cache.rb,
lib/tmdb/null_person.rb,
lib/tmdb/configuration.rb

Defined Under Namespace

Modules: Attributes Classes: Configuration, NullCache, NullPerson, Person, ServiceUnavailable

Constant Summary collapse

VERSION =
'0.4.0'

Instance Method Summary collapse

Instance Method Details

#configurationObject

Returns the global TMDb configuration.



8
9
10
# File 'lib/tmdb.rb', line 8

def configuration
  @configuration ||= Configuration.new
end

#configure {|configuration| ... } ⇒ Object

Configure TMDb by calling this method.

Yields:



14
15
16
# File 'lib/tmdb.rb', line 14

def configure
  yield configuration
end

#get_api_response(path, params = {}) ⇒ Object

Makes a TMDb API request given to the (relative) path with the given query params and using the configured API key. Returns the response as a hash (parsed from the original JSON). This method is not intended to be called directly by client code, instead you should call methods such as Person.find that return TMDb wrapper objects.

Raises a TMDb::ServiceUnavailable error if the service is unavailable or the API request limits have been exceeded.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tmdb.rb', line 27

def get_api_response(path, params = {})
  configuration.cache.fetch([path, params]) do
    connection = Faraday.new(:url => 'http://api.themoviedb.org/3/') do |builder|
      builder.request :url_encoded
      builder.adapter :net_http
    end
    response = connection.get(
      path,
      params.merge({ :api_key => TMDb.configuration.api_key })
    )
    if response.status == 503
      raise ServiceUnavailable
    else
      JSON.parse(response.body)
    end
  end
end