Module: MovieDB::Relation::QueryMethods

Extended by:
Secret
Included in:
Movie
Defined in:
lib/movieDB/relation/query_methods.rb

Instance Method Summary collapse

Methods included from Secret

key

Instance Method Details

#fetch(*ids, expire: 1800) ⇒ Object

Fetch data from IMDb. Default expiration time for stored object in redis is 1800 seconds. You can set this value to what ever you like.

Example:

m = MovieDB::Movie.new

m.fetch("0369324", "0369662", expire: 84600)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/movieDB/relation/query_methods.rb', line 22

def fetch(*ids, expire: 1800)
  store_data(ids_to_array(ids), expire)

  # Collect all fetched data and assign to global variable
  arr = []

  ids.each do |id|
    arr << (hgetall(id))
  end

  $movie_data = arr
end

#fetch_data(method, ids = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/movieDB/relation/query_methods.rb', line 109

def fetch_data(method, ids = nil)
  if ids.nil?
    MovieDB::DataStore.get_data(method)
  else
    ids.each do |id|
      MovieDB::DataStore.get_data(method, id)
    end
  end
end

#imdb_tmdb_lookup(id, expire) ⇒ Object

Fetch the movie from both IMDb and TMDb repositories.

Future release of this software will scrap IMDb data from boxofficemojoAPI.com using Mechanize gem.

Reference github.com/skozilla/BoxOfficeMojo/tree/master/boxofficemojoAPI for the api.



85
86
87
88
# File 'lib/movieDB/relation/query_methods.rb', line 85

def imdb_tmdb_lookup(id, expire) # :nodoc:
  query_imdb(id, expire)
  query_tmdb(id, expire)
end

#movie_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/movieDB/relation/query_methods.rb', line 74

def movie_exists?(id)
  !hgetall(id).empty?
end

#mset(record, id, expire) ⇒ Object



70
71
72
# File 'lib/movieDB/relation/query_methods.rb', line 70

def mset(record, id, expire)
  MovieDB::DataStore.write_data(imdb_tmdb: record, id: id, expire: expire)
end

#query_imdb(id, expire) ⇒ Object

:nodoc:

Raises:

  • (NameError)


90
91
92
93
94
95
96
97
# File 'lib/movieDB/relation/query_methods.rb', line 90

def query_imdb(id, expire) # :nodoc:
  # Query IMDb
  imdb = Imdb::Movie.new(id)

  raise NameError, "#{id} is an invalid IMDb id." if imdb.title.nil?

  mset(imdb, id, expire)
end

#query_tmdb(id, expire) ⇒ Object

:nodoc:

Raises:

  • (NameError)


99
100
101
102
103
104
105
106
107
# File 'lib/movieDB/relation/query_methods.rb', line 99

def query_tmdb(id, expire) # :nodoc:
  Tmdb::Api.key(MovieDB::Secret.key)

  tmdb = Tmdb::Movie.detail("tt#{id}")

  raise NameError, "#{id} is an invalid TMDb id." if tmdb.nil?

  mset(tmdb, id, expire)
end

#store_data(ids, expire) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/movieDB/relation/query_methods.rb', line 35

def store_data(ids, expire)
  check_rate_limit(ids)

  ids.each do |id|
    movie_exists?(id) ? true : imdb_tmdb_lookup(id, expire)
  end
end