Class: WorldDb::ReaderBase

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, Models, TextUtils::ValueHelper, Matcher
Defined in:
lib/worlddb/reader.rb

Direct Known Subclasses

Reader, ZipReader

Instance Method Summary collapse

Methods included from Matcher

#match_cities_for_country, #match_countries_for_continent, #match_regions_abbr_for_country, #match_regions_for_country, #match_regions_iso_for_country, #match_regions_nuts_for_country, #match_xxx_for_country, #match_xxx_for_country_n_region

Constructor Details

#initialize(opts = {}) ⇒ ReaderBase

Returns a new instance of ReaderBase.



23
24
25
26
27
28
# File 'lib/worlddb/reader.rb', line 23

def initialize( opts={} )
  ## option: do NOT generate/add any tags for countries/regions/cities
  @skip_tags =  opts[:skip_tags].present? ? true : false
  ## option: for now issue warning on update, that is, if key/record (country,region,city) already exists
  @strict    =  opts[:strict].present? ? true : false
end

Instance Method Details

#find_country(country_name) ⇒ Object

helper methods

todo: move to country model !!!!!!

find a good name Country.find  not really possible
superfind ??  use search!!! search_by_name()  Country.search() or lookup?

todo: also add City.search_by_name etc. !!!



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/worlddb/reader.rb', line 227

def find_country( country_name )

  name = country_name.strip

  ## 1) first try 1:1 (exact) match
  cty = Country.find_by_name( name )   # NOTE: assume AR escapes quotes in name ??
  if cty.nil?
    ## 2) retry: remove all () enclosed
    name = name.gsub( /\([^)]+\)/, '' ).strip
    cty = Country.find_by_name( name )

    ### NOTE: escape ' for sql like clause
    ##   for now use '' for escapes, that is, double quotes
    ##  check - working for postgresql n sqlite?? 
    name_esc = name.gsub( /'/, "''" )

    ## 3) retry: use SQL like match
    ##    % is used to match *zero* or more occurrences of any characters
    ##  todo: check if it matches zero too
    if cty.nil?
      cty = Country.where( "name LIKE '%#{name_esc}%'" ).first
    end

    ## 4) retry: use SQL like match for alternative names match
    if cty.nil?
      cty = Country.where( "alt_names LIKE '%#{name_esc}%'" ).first
    end
  end
  cty  # return cty (country); nil if not found
end

#load(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/worlddb/reader.rb', line 40

def load( name )

  if name =~ /^continents/
     load_continent_defs( name )
  elsif name =~ /\/continents/
     load_continent_refs( name )
  elsif name =~ /^lang/
     ## todo: pass along opts too
     ## use match_usage( name ) - why? why not?? ???
     r = create_lang_reader( name )
     r.read()
  elsif name =~ /\/lang/
     ## todo: pass along opts too
     ## use match_usage( name ) - why? why not?? ???
     r = create_usage_reader( name )
     r.read()
  elsif name =~ /\/fifa/     ||
        name =~ /\/fips/     ||
        name =~ /\/internet/ ||
        name =~ /\/ioc/      ||
        name =~ /\/iso/      ||
        name =~ /\/motor/
     load_codes( name )
  elsif name =~ /^tag.*\.\d$/
     ## todo: pass along opts too
     ## use match_tags( name ) - why? why not?? ???
     
     ######## FIX: add back again
     ### fix: use read() only, that is, w/o name
     ## r = create_tag_reader( name )
     ## r.read()
  elsif match_countries_for_continent( name ) do |continent|  # # e.g. africa/countries or america/countries
          ### NB: continent changed to regions (e.g. middle-east, caribbean, north-america, etc.)
          ## auto-add continent (from folder structure) as tag
          ## fix: allow dash/hyphen/minus in tag

          ### todo/fix: add opts - how??
          r = create_country_reader( name, tags: continent.tr('-', '_') )
          r.read()
        end
  elsif match_cities_for_country( name ) do |country_key|  #  name =~ /\/([a-z]{2})\/cities/
          ## auto-add required country code (from folder structure)
          country = Country.find_by_key!( country_key )
          logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"

          r = create_city_reader( name, country_id: country.id )
          r.read()
        end
  elsif match_regions_abbr_for_country( name ) do |country_key|   # name =~ /\/([a-z]{2})\/regions\.abbr/
          load_regions_xxx( country_key, 'abbr', name )
        end
  elsif match_regions_iso_for_country( name ) do |country_key|  # name =~ /\/([a-z]{2})\/regions\.iso/
          load_regions_xxx( country_key, 'iso', name )
        end
  elsif match_regions_nuts_for_country( name ) do |country_key|  # name =~ /\/([a-z]{2})\/regions\.nuts/
          load_regions_xxx( country_key, 'nuts', name )
        end
  elsif match_regions_for_country( name ) do |country_key|  # name =~ /\/([a-z]{2})\/regions/
          ## auto-add required country code (from folder structure)
          country = Country.find_by_key!( country_key )
          logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"

          r = create_region_reader( name, country_id: country.id )
          r.read()
        end
  else
    logger.error "unknown world.db fixture type >#{name}<"
    # todo/fix: exit w/ error
  end
end

#load_codes(name) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/worlddb/reader.rb', line 166

def load_codes( name )
  reader = create_line_reader( name )

  reader.each_line do |line|

    values = line.split(',')

    logger.debug '[>' + values.join( '<|>' ) + '<]'

    if name =~ /iso/
      # special case for iso
      #  country ref, alpha2, alpha3, num
      country_name = values[0].strip
    else
      #  code, country ref
      country_name = values[1].strip
    end

    ## try to find country
    cty = find_country( country_name )

    if cty.nil?
      logger.warn "no country match found for >#{country_name}<; skipping line; in [#{name}]"
      next
    end

    if name =~ /\/fifa/
      cty.fifa = values[0].strip
    elsif name =~ /\/fips/
      cty.fips = values[0].strip
    elsif name =~ /\/internet/
      # NOTE: remove (optional) leading . e.g. .at becomes at
      cty.net  = values[0].sub( /^\s*\./,'' ).strip  
    elsif name =~ /\/ioc/
      cty.ioc = values[0].strip
    elsif name =~ /\/motor/
      cty.motor = values[0].strip
    elsif name =~ /\/iso/
      cty.alpha2 = values[1].strip
      cty.alpha3 = values[2].strip
      # NOTE: num is a string!!! use (rename to) num_str - why? why not?
      cty.num    = values[3].strip
    else
      logger.warn "warn: unknown country code type; skipping line; in [#{name}]"
      next
    end

    cty.save!
  end
end

#load_continent_defs(name, more_attribs = {}) ⇒ Object

use ContinentDef Reader



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/worlddb/reader.rb', line 140

def load_continent_defs( name, more_attribs={} )
  reader = create_values_reader( name, more_attribs )

  reader.each_line do |attribs, values|

    ## check optional values
    values.each_with_index do |value, index|
      logger.warn "unknown type for value >#{value}<"
    end

    rec = Continent.find_by_key( attribs[ :key ] )
    if rec.present?
      logger.debug "update Continent #{rec.id}-#{rec.key}:"
    else
      logger.debug "create Continent:"
      rec = Continent.new
    end

    logger.debug attribs.to_json

    rec.update_attributes!( attribs )

  end # each lines
end

#load_continent_refs(name) ⇒ Object

use ContinentRefReader



128
129
130
131
132
133
134
135
136
137
# File 'lib/worlddb/reader.rb', line 128

def load_continent_refs( name )
  reader = create_hash_reader( name )

  reader.each do |key, value|
    country = Country.find_by_key!( key )
    continent = Continent.find_by_key!( value )
    country.continent_id = continent.id
    country.save!
  end
end

#load_regions_xxx(country_key, xxx, name) ⇒ Object

use RegionAttrReader



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/worlddb/reader.rb', line 113

def load_regions_xxx( country_key, xxx, name )
  country = Country.find_by_key!( country_key )
  logger.debug "Country #{country.key} >#{country.title} (#{country.code})<"

  reader = create_hash_reader( name )

  reader.each do |key, value|
    region = Region.find_by_country_id_and_key!( country.id, key )
    region.send( "#{xxx}=", value )
    region.save!
  end
end

#load_setup(name) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/worlddb/reader.rb', line 31

def load_setup( name )
  reader = create_fixture_reader( name )

  reader.each do |fixture|
    load( fixture )
  end
end

#skip_tags?Boolean

Returns:

  • (Boolean)


19
# File 'lib/worlddb/reader.rb', line 19

def skip_tags?()   @skip_tags == true;  end

#strict?Boolean

Returns:

  • (Boolean)


20
# File 'lib/worlddb/reader.rb', line 20

def strict?()      @strict == true;     end