Sha256: a0d53c0b83d5de633c776b979c10c68ddc5c7fa339afd115f26ab0cd59dd0226

Contents?: true

Size: 917 Bytes

Versions: 11

Compression:

Stored size: 917 Bytes

Contents

module WhosDatedWho
  class Importer
    include RethinkDB::Shortcuts
    attr_reader :conn, :logger, :table_name

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

    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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
whos_dated_who-0.3.0 lib/whos_dated_who/importer.rb
whos_dated_who-0.2.7 lib/whos_dated_who/importer.rb
whos_dated_who-0.2.6 lib/whos_dated_who/importer.rb
whos_dated_who-0.2.5 lib/whos_dated_who/importer.rb
whos_dated_who-0.2.4 lib/whos_dated_who/importer.rb
whos_dated_who-0.2.3 lib/whos_dated_who/importer.rb
whos_dated_who-0.2.2 lib/whos_dated_who/importer.rb
whos_dated_who-0.2.1 lib/whos_dated_who/importer.rb
whos_dated_who-0.2.0 lib/whos_dated_who/importer.rb
whos_dated_who-0.1.1 lib/whos_dated_who/importer.rb
whos_dated_who-0.1.0 lib/whos_dated_who/importer.rb