Class: Wasserstand::Provider::PegelOnline

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/wasserstand/provider/pegel_online.rb

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ PegelOnline

Returns a new instance of PegelOnline.



8
9
10
# File 'lib/wasserstand/provider/pegel_online.rb', line 8

def initialize(url = nil)
  @url = url || 'http://www.pegelonline.wsv.de/svgz/pegelstaende_neu.xml'
end

Instance Method Details

#cacheObject



34
35
36
37
38
39
# File 'lib/wasserstand/provider/pegel_online.rb', line 34

def cache
  if @cache.nil?
    self.cache = HeapCache.new
  end
  @cache
end

#cache=(c) ⇒ Object



41
42
43
44
# File 'lib/wasserstand/provider/pegel_online.rb', line 41

def cache=(c)
  Wasserstand.logger.info "Using cache #{c}"
  @cache = c
end

#levelsObject



30
31
32
# File 'lib/wasserstand/provider/pegel_online.rb', line 30

def levels
  waterways.inject([]){|result, ww| result.concat(ww.levels.values)}
end

#to_sObject



46
47
48
# File 'lib/wasserstand/provider/pegel_online.rb', line 46

def to_s
  "#<#{self.class.name}:#{@url}>"
end

#waterwaysObject

Cache entries may expire, and simply iterating over the list of current cache entries would render an incomplete picture. Therefore we maintain the knowledge of what waterways exist in a single list, which is the authoritative source. If that one is gone, it is gone as a whole and everything will need to be refreshed.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wasserstand/provider/pegel_online.rb', line 13

def waterways
  names = cache.get(KEY_NAMES) || replenish

  names.map do |name|
    ww = cache.get(name) # no fetch with block in Dalli, so we cannot use it here either ...

    # Not finding the Waterway for a name means it was removed from the cache, but it may or may not exist in the backend. Therefore we need to replenish our cache including our knowledge about the names.
    if ww.nil?
      replenish
      ww = cache.get(name)
      # Still nil? Does not matter. Replenish has auto-removed an outdated name.
    end

    ww
  end.compact # don't return nil entries
end