Sha256: 771ae88379479799523913b68caac49b7b2e6b9f6bb01e3a7c9c8b38ac45acf6

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module Wasserstand
  module Provider
    class PegelOnline
      attr_writer :cache

      include Enumerable
      extend Forwardable
      def_delegator :all, :each

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

      # 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 of what waterways exist.
      def waterways
        replenish if @names.empty?
        @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 name.
          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

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

      def cache
        @cache ||= HeapCache.new
      end

      private

      #
      # Replenish the cache and names index
      #
      def replenish
        @names.clear
        STDERR.puts "FETCH #{@url}"
        doc = Nokogiri::HTML(open(@url).read, nil, 'ISO-8859-1')
        doc.xpath("//data/table/gewaesser").each do |node|
          ww = WaterwayMapper.map(node)
          @names << ww.name
          cache.set(ww.name, ww)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wasserstand-0.0.9 lib/wasserstand/provider/pegel_online.rb