Class: BeerDb::BeerReader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, Models
Defined in:
lib/beerdb/readers/beer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of BeerReader.



34
35
36
37
38
# File 'lib/beerdb/readers/beer.rb', line 34

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



23
24
25
26
27
28
# File 'lib/beerdb/readers/beer.rb', line 23

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



30
31
32
# File 'lib/beerdb/readers/beer.rb', line 30

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

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



13
14
15
16
17
18
19
20
21
# File 'lib/beerdb/readers/beer.rb', line 13

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

  text = entry.get_input_stream().read()
  text = text.force_encoding( Encoding::UTF_8 )

  self.from_string( text, more_attribs )
end

Instance Method Details

#readObject



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
# File 'lib/beerdb/readers/beer.rb', line 41

def read()

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

  ### todo: cleanup - check if [] works for build_title...
  #     better cleaner way ???
  if @more_attribs[:state_id].present?
    known_breweries_source = Brewery.where( state_id:  @more_attribs[:state_id] )
  elsif @more_attribs[:country_id].present?
    known_breweries_source = Brewery.where( country_id: @more_attribs[:country_id] )
  else
    logger.warn "no state or country specified; use empty brewery ary for header mapper"
    known_breweries_source = []
  end

  known_breweries  = TextUtils.build_title_table_for( known_breweries_source )


  reader.each_line do |new_attributes, values|

    ## note: check for header attrib; if present remove
    ### todo: cleanup code later
    ## fix: add to new_attributes hash instead of values ary
    ##   - fix: match_brewery()   move state,city code out of values loop for reuse at the end
    if new_attributes[:header].present?
      brewery_line = new_attributes[:header].dup   # note: make sure we make a copy; will use in-place string ops
      new_attributes.delete(:header)   ## note: do NOT forget to remove from hash!

      logger.debug "  trying to find brewery in line >#{brewery_line}<"
      ## todo: check what map_titles_for! returns (nothing ???)
      TextUtils.map_titles_for!( 'brewery', brewery_line, known_breweries )
      brewery_key = TextUtils.find_key_for!( 'brewery', brewery_line )
      logger.debug "  brewery_key = >#{brewery_key}<"
      unless brewery_key.nil?
        ## bingo! add brewery_id upfront, that is, as first value in ary
        values = values.unshift "by:#{brewery_key}"
      end
    end

    Beer.create_or_update_from_attribs( new_attributes, values )
  end # each_line
end