Module: Geonames::Models::MongoWrapper

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

Class Method Summary collapse

Class Method Details

.batch(data, clean = false) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/geonames_local/models/mongodb.rb', line 22

def batch data, clean = false
  [Region, City].each(&:delete_all) if clean

  @regions, @cities = data[:region], data[:city]
  @regions.each { |r| create Region, parse_region(r) }
  @cities.each  { |c| create City, parse_city(c) }
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, clean) ⇒ Object

Parse Nations



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

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

.parse_city(s) ⇒ Object

Parse Cities



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/geonames_local/models/mongodb.rb', line 83

def parse_city(s)
  region = Region.find_by(code: s.region)
  slug = City.new(slug: s.ascii).slug
  attempt = slug.dup
  try = 1
  until City.where(slug: attempt.downcase).first.nil?
    attempt = "#{slug}-#{region.abbr}-#{try}"
    try += 1
    break if try > 7
  end
  # ---
  # info s.inspect
  info "City: #{s.zip} | #{slug} - #{s.name} / #{region.try(:abbr)}"
  {
    name_translations: translate(s.name),
    slug: attempt, gid: s.gid, code: s.code,
    souls: s.pop, geom: [s.lon, s.lat],
    region: region, abbr: region.abbr, zip: s.zip # tz
  }
end

.parse_nation(row) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/geonames_local/models/mongodb.rb', line 53

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),
    zip: pos_code, cash: cur_code, gid: gid,
    abbr: abbr, slug: name.downcase, code: iso3, lang: langs
  }
end

.parse_region(s) ⇒ Object

Parse Regions



70
71
72
73
74
75
76
77
78
# File 'lib/geonames_local/models/mongodb.rb', line 70

def parse_region s
  nation = Nation.find_by(abbr: /#{s.nation}/i)
  info "Region: #{s.name} / #{s.abbr}"
  {
    name_translations: translate(s.name),
    gid: s.gid, abbr: s.abbr,
    nation: nation, code: s.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