Class: WorldDb::LangReader

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, opts = {}) ⇒ LangReader

Returns a new instance of LangReader.



62
63
64
65
66
67
68
69
# File 'lib/worlddb/readers/lang.rb', line 62

def initialize( text, opts={} )
  @text = text

  ## 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

Class Method Details

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



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

def self.from_file( path, opts={} )
  ## 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, opts )
end

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



54
55
56
# File 'lib/worlddb/readers/lang.rb', line 54

def self.from_string( text, opts={} )
  LangReader.new( text, opts )
end

.from_zip(zip_file, entry_path) ⇒ Object

todo: add opts={} etc.



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
45
# File 'lib/worlddb/readers/lang.rb', line 18

def self.from_zip( zip_file, entry_path )
  ## 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 )
end

Instance Method Details

#readObject



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
# File 'lib/worlddb/readers/lang.rb', line 71

def read()
  reader = HashReader.from_string( @text )

  reader.each do |key, value|

    ### fix:
    ##  move to Lang.read() for (re)use

      logger.debug "adding lang >>#{key}<< >>#{value}<<..."
    
      lang_key   = key.strip
      lang_title = value.strip
    
      lang_attribs = {}
      
      ## check if it exists
      lang = Lang.find_by_key( lang_key )
      if lang.present?
        logger.debug "update lang #{lang.id}-#{lang.key}:"
      else
        logger.debug "create lang:"
        lang = Lang.new
        lang_attribs[ :key ] = lang_key
      end
      
      lang_attribs[ :title ] = lang_title
      
      logger.debug lang_attribs.to_json
   
      lang.update_attributes!( lang_attribs )
  end # each key,value

end

#skip_tags?Boolean

Returns:

  • (Boolean)


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

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

#strict?Boolean

Returns:

  • (Boolean)


60
# File 'lib/worlddb/readers/lang.rb', line 60

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