Class: NsGeo::GeoLocationImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/ns_geo/geo_location_importer.rb

Class Method Summary collapse

Class Method Details

.get_countObject



18
19
20
# File 'app/services/ns_geo/geo_location_importer.rb', line 18

def self.get_count
  CSV.foreach(NsGeo.csv_location, :headers => true).count
end

.importObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/services/ns_geo/geo_location_importer.rb', line 5

def self.import
  total = get_count
  t1 = Time.now
  result = read_csv_file
  t2 = Time.now
  delta = t2 - t1
  puts('GeoLocation Import Report:')
  puts("Locations are imported in: #{delta} seconds.")
  puts("Total records: #{total}")
  puts("Saved records: #{result[:saved]}")
  puts("Discarded records: #{result[:discarded]}")
end

.read_csv_fileObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/ns_geo/geo_location_importer.rb', line 22

def self.read_csv_file
  result = {saved: 0, discarded: 0}
  CSV.foreach(NsGeo.csv_location, :headers => true) do |row|
    if save(row)
      result[:saved] = result[:saved] + 1
    else
      result[:discarded] = result[:discarded] + 1
    end
    unless Rails.env.production?
      if result[:saved] == 1000
        return result
      end
    end
  end
  result
end

.save(row) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/ns_geo/geo_location_importer.rb', line 39

def self.save(row)
  geo_location = NsGeo::GeoLocation.new
  geo_location[:ip_address] = row[0]
  geo_location[:country_code] = row[1]
  geo_location[:country] = row[2]
  geo_location[:city] = row[3]
  geo_location[:latitude] = row[4]
  geo_location[:longitude] = row[5]
  geo_location[:mystery_value] = row[6]
  geo_location.save
end