Class: Wasserstand::PegelOnline::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/wasserstand/provider/pegel-online.rb

Direct Known Subclasses

LevelProvider, WaterwayProvider

Instance Method Summary collapse

Constructor Details

#initialize(url = 'http://www.pegelonline.wsv.de/svgz/pegelstaende_neu.xml') ⇒ Provider

Returns a new instance of Provider.



4
5
6
# File 'lib/wasserstand/provider/pegel-online.rb', line 4

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

Instance Method Details

#[](name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/wasserstand/provider/pegel-online.rb', line 8

def [](name)
  results = doc.xpath(xpath_lookup(name))

  case results.size
  when 0
    return nil # loookup returns nil if not found. This is a lookup, not find_all.
  when 1
    return mapper.map(results.first)
  else
    raise "Name is not unique. Found #{results.size} results for #{name}."
  end
end

#allObject



21
22
23
# File 'lib/wasserstand/provider/pegel-online.rb', line 21

def all
  doc.xpath(xpath_all).map{|o| mapper.map(o)}
end

#find_by_name(name_expression) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wasserstand/provider/pegel-online.rb', line 25

def find_by_name(name_expression)
  # Not the best performing way, but it gives us the ability to use the  XPath 2.0 'matches' function
  # which isn't supported in Nokogiri (yet).
  doc.xpath(xpath_finder(name_expression), Class.new{
    def matches(node_set, regex)
      node_set.find_all do |node|
        node.to_s.match(%r{#{regex}})
      end
    end
  }.new).map{|o| mapper.map(o)}
end