Module: Geonames::Models::MongoWrapper

Defined in:
lib/geonames_local/models/mongodb.rb

Class Method Summary collapse

Class Method Details

.batch(data) ⇒ Object



20
21
22
23
24
# File 'lib/geonames_local/models/mongodb.rb', line 20

def batch(data)
  @regions, @cities = data[:region], data[:city]
  @regions.each { |r| create Region, parse_region(r) }
  @cities.each  { |c| create City, parse_city(c) }
end

.cleanObject



26
27
28
# File 'lib/geonames_local/models/mongodb.rb', line 26

def clean
  [Nation, Region, City].each(&:delete_all)
end

.create(klass, data) ⇒ Object



30
31
32
33
34
35
# File 'lib/geonames_local/models/mongodb.rb', line 30

def create(klass, data)
  # info "#{klass}.new #{data}"
  klass.create! data
rescue => e
  warn "Prob com spot #{e} #{e.backtrace.join("\n")}"
end

.nations(data) ⇒ Object

Parse Nations



46
47
48
49
50
# File 'lib/geonames_local/models/mongodb.rb', line 46

def nations(data)
  data.each do |row|
    create Nation, parse_nation(row) rescue nil
  end
end

.nations_populated?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/geonames_local/models/mongodb.rb', line 52

def nations_populated?
  Nation.count > 0
end

.parse_city(s) ⇒ Object

Parse Cities



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/geonames_local/models/mongodb.rb', line 86

def parse_city(s)
  region = Region.find_by(code: s.region)
  # ---
  # info s.inspect
  info "City: #{s.zip} | #{s.name} / #{region.try(:abbr)}"
  {
    id: s.gid.to_s, code: s.code,
    name_translations: translate(s.name),
    souls: s.pop, geom: [s.lon, s.lat],
    region_id: region.id.to_s, postal: s.zip # tz
  }
end

.parse_nation(row) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/geonames_local/models/mongodb.rb', line 56

def parse_nation(row)
  abbr, iso3, ison, fips, name, capital, area, pop, continent,
  tld, cur_code, cur_name, phone, pos_code, pos_regex,
  langs, gid, neighbours = row.split(/\t/)
  info "Nation: #{name}/#{abbr}"
  # info "#{row}"
  # info "------------------------"
  {
    name_translations: translate(name),
    postal: pos_code, cash: cur_code, gid: gid,
    abbr: abbr, slug: name.downcase, code: iso3, lang: langs
  }
end

.parse_region(r) ⇒ Object

Parse Regions



73
74
75
76
77
78
79
80
81
# File 'lib/geonames_local/models/mongodb.rb', line 73

def parse_region(r)
  nation = Nation.find_by(abbr: /#{r.nation}/i)
  info "Region: #{r.name} / #{r.abbr}"
  {
    id: r.gid.to_s, abbr: r.abbr,
    name_translations: translate(r.name),
    nation: nation, code: r.region
  }
end

.translate(txt) ⇒ Object



37
38
39
40
41
# File 'lib/geonames_local/models/mongodb.rb', line 37

def translate(txt)
  name_i18n = Opt[:locales].reduce({}) do |h, l|
    h.merge(l => txt)
  end
end