Module: MovieDB::DataStore

Defined in:
lib/movieDB/data_store.rb

Class Method Summary collapse

Class Method Details

.get_data(method, id = nil) ⇒ Object

You can fetch one data at at a time. Do not send an array of arguments.

Example the following is accepted.

MovieDB::Movie.get_data('0369610')

Not accepted:

MovieDB::Movie.get_data(['0369610', 3079380])


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/movieDB/data_store.rb', line 59

def self.get_data(method, id = nil)
  initialize_redis

  case method
    when :all
      return @redis_db.hgetall "#{id}"
    when :hkeys
      return @redis_db.hkeys "#{id}"
    when :hvals
      return @redis_db.hvals "#{id}"
    when :scan
      return @redis_db.scan 0
    when :flushall
      return @redis_db.flushall
    when :get
      return @redis_db.hgetall("#{id}")
    when :ttl
      return @redis_db.ttl("#{id}")
  else
    raise ArgumentError, "The method #{method} is invalid."
  end
end

.imdb_methodsObject



12
13
14
15
16
# File 'lib/movieDB/data_store.rb', line 12

def imdb_methods
  [:title, :also_known_as, :cast_members, :cast_characters, :cast_members_characters,
   :director, :writers, :trailer_url, :genres, :languages, :countries, :length, :company, :plot, :plot_synopsis,
   :plot_summary, :poster, :rating, :votes, :tagline, :mpaa_rating, :year, :release_date, :filming_locations]
end

.initialize_redisObject

Create a redis instance with timeouts.



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

def self.initialize_redis
  @redis_db ||= Redis.new(connect_timeout: 20, timeout: 20)
end

.write_data(**options) ⇒ Object

The options returns with 3 keys options, contains the movie data options, contains the IMDb id. options contains the expiration time for redis.

IMDb return a status code of 34 if the resource can not be found.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/movieDB/data_store.rb', line 27

def self.write_data(**options)
  if options[:imdb_tmdb].is_a? Hash

    options.each_pair do |k, v|
        if v.is_a? Hash
          if v["status_code"] == "34"
            puts "#{options[:id]} is an invalid IMDb id."
          else
            v.each_pair do |j, w|
                @redis_db.hsetnx "#{options[:id]}", "#{j}", "#{w}"
            end
          end
        end
    end
  else
    imdb_methods.each do |method|
      @redis_db.hsetnx "#{options[:imdb_tmdb].id}", method.to_s, "#{options[:imdb_tmdb].send(method)}"
    end
  end

  @redis_db.expire "#{options[:id]}", "#{options[:expire]}"
end