Class: WorldDb::CountryReader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, TextUtils::ValueHelper, Model
Defined in:
lib/worlddb/readers/country.rb

Constant Summary

Constants included from Model

Model::Prop, Model::Tag, Model::Tagging

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, more_attribs = {}) ⇒ CountryReader

Returns a new instance of CountryReader.



61
62
63
64
65
66
# File 'lib/worlddb/readers/country.rb', line 61

def initialize( text, more_attribs={} )
  ## todo/fix: how to add opts={} ???

  @text         = text
  @more_attribs = more_attribs
end

Class Method Details

.from_file(path, more_attribs = {}) ⇒ Object



46
47
48
49
50
51
# File 'lib/worlddb/readers/country.rb', line 46

def self.from_file( path, more_attribs={} )
  ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
  ## - see textutils/utils.rb
  text = File.read_utf8( path )
  self.from_string( text, more_attribs )    
end

.from_string(text, more_attribs = {}) ⇒ Object



53
54
55
# File 'lib/worlddb/readers/country.rb', line 53

def self.from_string( text, more_attribs={} )
  CountryReader.new( text, more_attribs )
end

.from_zip(zip_file, entry_path, more_attribs = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/worlddb/readers/country.rb', line 17

def self.from_zip( zip_file, entry_path, more_attribs={} )
  ## get text content from zip

  entry = zip_file.find_entry( entry_path )

  ## todo/fix: add force encoding to utf-8 ??
  ##  check!!!
  ##  clean/prepprocess lines
  ##  e.g. CR/LF (/r/n) to LF (e.g. /n)
  text = entry.get_input_stream().read()

  ## NOTE: needs logger ref; only available in instance methods; use global logger for now
  logger = LogUtils::Logger.root
  logger.debug "text.encoding.name (before): #{text.encoding.name}"
#####
# NB: ASCII-8BIT == BINARY == Encoding Unknown; Raw Bytes Here
## NB:
# for now "hardcoded" to utf8 - what else can we do?
# - note: force_encoding will NOT change the chars only change the assumed encoding w/o translation
  text = text.force_encoding( Encoding::UTF_8 )
  logger.debug "text.encoding.name (after): #{text.encoding.name}"     

  ## todo:
  # NB: for convenience: convert fancy unicode dashes/hyphens to plain ascii hyphen-minus
  ## text = TextUtils.convert_unicode_dashes_to_plain_ascii( text, path: path )

  self.from_string( text, more_attribs )
end

Instance Method Details

#readObject



68
69
70
71
72
73
74
75
# File 'lib/worlddb/readers/country.rb', line 68

def read()
  reader = ValuesReader.from_string( @text, @more_attribs )

  reader.each_line do |attribs, values|
    opts = { skip_tags: skip_tags? }
    Country.create_or_update_from_attribs( attribs, values, opts )
  end
end

#skip_tags?Boolean

Returns:

  • (Boolean)


58
# File 'lib/worlddb/readers/country.rb', line 58

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

#strict?Boolean

Returns:

  • (Boolean)


59
# File 'lib/worlddb/readers/country.rb', line 59

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