Class: WhosDatedWho::Importer

Inherits:
Object
  • Object
show all
Includes:
RethinkDB::Shortcuts
Defined in:
lib/whos_dated_who/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, table_name) ⇒ Importer

Returns a new instance of Importer.



6
7
8
9
10
# File 'lib/whos_dated_who/importer.rb', line 6

def initialize(db, table_name)
  @conn = r.connect(host: 'localhost', db: db || 'celebs')
  @logger = Logger.new(STDOUT)
  @table_name = table_name
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



4
5
6
# File 'lib/whos_dated_who/importer.rb', line 4

def conn
  @conn
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/whos_dated_who/importer.rb', line 4

def logger
  @logger
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



4
5
6
# File 'lib/whos_dated_who/importer.rb', line 4

def table_name
  @table_name
end

Instance Method Details

#fetch_and_import(celebs) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/whos_dated_who/importer.rb', line 12

def fetch_and_import(celebs)
  celebs.each do |celeb|
    begin
      logger.info "Fetching #{celeb}"
      result = Client.new.fetch(celeb)
      logger.info "Importing #{celeb} to #{table_name}"
      import(result)
    rescue
      first_name, last_name = celeb.split
      r.table("missing_#{table_name}").insert({first_name: first_name, last_name: last_name}).run(conn)
      logger.error("Error importing #{celeb}, added to missing")
    end
  end
end

#import(results) ⇒ Object



27
28
29
# File 'lib/whos_dated_who/importer.rb', line 27

def import(results)
  r.table(table_name).insert(JSON.parse(results.to_json)).run(conn)
end