wasserstand 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wasserstand.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nicholas E. Rabenau
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Wasserstand
2
+
3
+ Unofficial Ruby wrapper for [Pegel Online](http://www.pegelonline.wsv.de/)
4
+
5
+ [![Build Status](https://secure.travis-ci.org/nerab/wasserstand.png?branch=master)](http://travis-ci.org/nerab/wasserstand)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'wasserstand'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install wasserstand
20
+
21
+ ## Usage
22
+
23
+ TODO
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |test|
6
+ test.libs << 'lib' << 'test' << 'test/unit'
7
+ test.pattern = 'test/unit/test_*.rb'
8
+ end
9
+
10
+ task :default => :test
data/bin/wasserstand ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler'
4
+ Bundler.require
5
+
6
+ require 'wasserstand'
7
+ require 'optparse'
8
+
9
+ options = {}
10
+ OptionParser.new do |opts|
11
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
12
+ options[:verbose] = v
13
+ end
14
+ end.parse!
15
+
16
+ puts Wasserstand::Waterway['BODENSEE'].pegel.first.last.messwerte.first
@@ -0,0 +1,18 @@
1
+ module Wasserstand
2
+ #
3
+ # see http://www.pegelonline.wsv.de/gast/hilfe#hilfe_pegelparameter
4
+ #
5
+ class Level # Pegel
6
+ attr_reader :id
7
+ attr_accessor :name, :km, :measurements
8
+
9
+ def initialize(id)
10
+ @id = id
11
+ @measurements = []
12
+ end
13
+
14
+ def to_s
15
+ "#{@name} (km #{@km}): #{@measurements.last}"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,39 @@
1
+ module Wasserstand
2
+ =begin
3
+ <gewaesser>
4
+ <name>BODENSEE</name>
5
+ <item>
6
+ <no>8</no>
7
+ <psmgr>320</psmgr>
8
+ <pegelname>KONSTANZ</pegelname>
9
+ <messwert>380,7</messwert>
10
+ <km>0</km>
11
+ <pnp>391,89</pnp>
12
+ <tendenz>Gleich</tendenz>
13
+ <datum>13.09.2012</datum>
14
+ <uhrzeit>20:00:00</uhrzeit>
15
+ <pegelnummer>0906</pegelnummer>
16
+ </item>
17
+ </gewaesser>
18
+ =end
19
+ class Mapper
20
+ class << self
21
+ def map(node)
22
+ Waterway.new(node.xpath('name').text) .tap do |ww|
23
+ node.xpath('item').each do |item|
24
+ ww.level[name] = Level.new(item.xpath('pegelnummer').text).tap do |pegel|
25
+ pegel.name = item.xpath('pegelname').text
26
+ pegel.km = item.xpath('km').text
27
+
28
+ messdatum = Time.now # TODO parse date from date and time elements
29
+ wert = item.xpath('messwert').text
30
+ tendenz = item.xpath('tendenz').text
31
+
32
+ pegel.measurements << Measurement.new(messdatum, wert, tendenz)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,17 @@
1
+ module Wasserstand
2
+ class Measurement
3
+ attr_reader :date, :value, :trend
4
+
5
+ def initialize(date, value, trend)
6
+ @date, @value, @trend = date, value, trend
7
+ end
8
+
9
+ def to_i
10
+ @value
11
+ end
12
+
13
+ def to_s
14
+ "#{@date}: #{@value} cm, trend #{@trend}"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module Wasserstand
2
+ module Provider
3
+ class PegelOnline
4
+ def initialize(url = 'http://www.pegelonline.wsv.de/svgz/pegelstaende_neu.xml')
5
+ @url = url
6
+ end
7
+
8
+ def [](name)
9
+ doc = Nokogiri::HTML(open(@url).read)
10
+ results = doc.xpath("//data/table/gewaesser[name/text() = '#{name.upcase}']")
11
+
12
+ case results.size
13
+ when 0
14
+ return []
15
+ when 1
16
+ return Mapper.map(results.first)
17
+ else
18
+ raise "Found #{results.size} results for #{name}."
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Wasserstand
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ module Wasserstand
2
+ class Waterway
3
+ class << self
4
+ def [](name)
5
+ Wasserstand.provider[name]
6
+ end
7
+ end
8
+
9
+ attr_reader :name, :level
10
+
11
+ def initialize(name)
12
+ @name = name
13
+ @level = {}
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ require 'require_all'
5
+ require_rel 'wasserstand'
6
+
7
+ module Wasserstand
8
+ class << self
9
+ attr_writer :provider
10
+
11
+ def provider
12
+ @provider ||= Provider::PegelOnline.new
13
+ end
14
+ end
15
+ end