Class: WorldDb::CityReader
- Inherits:
-
Object
- Object
- WorldDb::CityReader
- Includes:
- LogUtils::Logging, Models, TextUtils::ValueHelper
- Defined in:
- lib/worlddb/readers/city.rb
Class Method Summary collapse
- .from_file(path, more_attribs = {}) ⇒ Object
- .from_string(text, more_attribs = {}) ⇒ Object
- .from_zip(zip_file, entry_path, more_attribs = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(text, more_attribs = {}) ⇒ CityReader
constructor
A new instance of CityReader.
- #read ⇒ Object
- #skip_tags? ⇒ Boolean
- #strict? ⇒ Boolean
Constructor Details
#initialize(text, more_attribs = {}) ⇒ CityReader
Returns a new instance of CityReader.
62 63 64 65 66 67 |
# File 'lib/worlddb/readers/city.rb', line 62 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
47 48 49 50 51 52 |
# File 'lib/worlddb/readers/city.rb', line 47 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
54 55 56 |
# File 'lib/worlddb/readers/city.rb', line 54 def self.from_string( text, more_attribs={} ) CityReader.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/city.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
#read ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/worlddb/readers/city.rb', line 70 def read() reader = ValuesReader.from_string( @text, @more_attribs ) reader.each_line do |attribs, values| opts = { skip_tags: } City.create_or_update_from_attribs( attribs, values, opts ) end end |
#skip_tags? ⇒ Boolean
59 |
# File 'lib/worlddb/readers/city.rb', line 59 def () @skip_tags == true; end |
#strict? ⇒ Boolean
60 |
# File 'lib/worlddb/readers/city.rb', line 60 def strict?() @strict == true; end |