wasserstand 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -20,4 +20,4 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- see tests
23
+ See the tests or bin/wasserstand for usage examples.
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ * Tests for the command line client
2
+ * CSV output for the command line client
3
+ * JSON output for the command line client
@@ -6,11 +6,61 @@ Bundler.require
6
6
  require 'wasserstand'
7
7
  require 'optparse'
8
8
 
9
- options = {}
10
9
  OptionParser.new do |opts|
11
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
12
- options[:verbose] = v
10
+ opts.banner = <<HERE
11
+ Wasserstand provides water level information as provided by PegelOnline.
12
+
13
+ Usage:
14
+ #{File.basename($0)} [options] [WATERWAY | LEVEL]
15
+
16
+ Author:
17
+ Nicolas E. Rabenau nerab@gmx.at
18
+
19
+ Homepage:
20
+ http://github.com/nerab/wasserstand
21
+
22
+ Options:
23
+ HERE
24
+
25
+ opts.on("-w", "--verbose", "Run verbosely") do |v|
26
+ Wasserstand.logger.level = Log4r::INFO
27
+ Wasserstand.logger.info("Enable info logger")
28
+ end
29
+
30
+ opts.on("-t", "--trace", "Run with traces enabled") do |v|
31
+ Wasserstand.logger.level = Log4r::ALL
32
+ Wasserstand.logger.debug("Enable debug logger")
33
+ end
34
+
35
+ opts.on("-u", "--url URL", "Use URL to fetch levels") do |url|
36
+ Wasserstand.logger.info("Using URL #{url}")
37
+ Wasserstand.provider = Wasserstand::Provider::PegelOnline.new(url)
13
38
  end
14
39
  end.parse!
15
40
 
16
- puts Wasserstand::Waterway['BODENSEE'].pegel.first.last.messwerte.first
41
+ query = ARGV.shift
42
+
43
+ if query.nil?
44
+ Wasserstand::Waterway.all.each{|ww| puts "#{ww.name} (#{ww.levels.size} levels)"}
45
+ else
46
+ begin
47
+ waterway = Wasserstand::Waterway[query]
48
+
49
+ if waterway
50
+ puts "#{waterway.name} has #{waterway.levels.size} levels:"
51
+ waterway.levels.sort_by{|name, level| level.km}.each{|name, level| puts "#{name} (km #{level.km}): #{level.measurements.last}"}
52
+ else
53
+ level = Wasserstand::Level[query]
54
+
55
+ if level.nil?
56
+ STDERR.puts "#{File.basename($0)}: No waterway nor level found that matches '#{query}."
57
+ else
58
+ puts "Level #{level.name} (#{level.waterway}, km #{level.km}):"
59
+ level.measurements.each{|measurement| puts measurement}
60
+ end
61
+ end
62
+ rescue
63
+ STDERR.puts $!
64
+ $!.backtrace.each{|msg| Wasserstand.logger.debug(msg)}
65
+ end
66
+ end
@@ -4,8 +4,9 @@ require 'tzinfo'
4
4
  require 'unicode_utils/upcase'
5
5
  require 'unicode_utils/downcase'
6
6
  require 'forwardable'
7
-
7
+ require 'log4r'
8
8
  require 'require_all'
9
+
9
10
  require_rel 'wasserstand'
10
11
 
11
12
  module Wasserstand
@@ -17,5 +18,14 @@ module Wasserstand
17
18
  def provider
18
19
  @provider ||= Provider::PegelOnline.new
19
20
  end
21
+
22
+ def logger
23
+ @logger ||= Log4r::Logger.new(self.name).tap do |logger|
24
+ out = Log4r::Outputter.stderr
25
+ out.formatter = Log4r::PatternFormatter.new(:pattern => "%l: %m")
26
+ logger.outputters = out
27
+ logger.level = Log4r::WARN
28
+ end
29
+ end
20
30
  end
21
31
  end
@@ -1,6 +1,7 @@
1
1
  module Wasserstand
2
2
  module Finders
3
3
  def [](name)
4
+ return nil if name.to_s.empty?
4
5
  results = all.select{|named| UnicodeUtils.upcase(name) == named.name}
5
6
 
6
7
  case results.size
@@ -1,10 +1,12 @@
1
- # http://stackoverflow.com/questions/2030336/how-do-i-create-a-hash-in-ruby-that-compares-strings-ignoring-case
2
- class HashClod < Hash
3
- def [](key)
4
- key.respond_to?(:upcase) ? super(UnicodeUtils.upcase(key)) : super(key)
5
- end
1
+ module Wasserstand
2
+ # http://stackoverflow.com/questions/2030336/how-do-i-create-a-hash-in-ruby-that-compares-strings-ignoring-case
3
+ class HashClod < Hash
4
+ def [](key)
5
+ key.respond_to?(:upcase) ? super(UnicodeUtils.upcase(key)) : super(key)
6
+ end
6
7
 
7
- def []=(key, value)
8
- key.respond_to?(:upcase) ? super(UnicodeUtils.upcase(key), value) : super(key, value)
8
+ def []=(key, value)
9
+ key.respond_to?(:upcase) ? super(UnicodeUtils.upcase(key), value) : super(key, value)
10
+ end
9
11
  end
10
12
  end
@@ -1,16 +1,18 @@
1
- class HeapCache
2
- def initialize
3
- @backend = {}
4
- end
1
+ module Wasserstand
2
+ class HeapCache
3
+ def initialize
4
+ @backend = {}
5
+ end
5
6
 
6
- def get(name)
7
- result = @backend[name]
8
- STDERR.puts "#{self.class.name} GET #{result ? 'HIT' : 'MISS'} #{name}"
9
- result
10
- end
7
+ def get(name)
8
+ result = @backend[name]
9
+ Wasserstand.logger.debug "#{self.class.name} GET #{result ? 'HIT' : 'MISS'} #{name}"
10
+ result
11
+ end
11
12
 
12
- def set(name, value)
13
- STDERR.puts "#{self.class.name} SET #{name} => #{value.inspect}"
14
- @backend[name] = value
13
+ def set(name, value)
14
+ Wasserstand.logger.debug "#{self.class.name} SET #{name} => #{value.inspect}"
15
+ @backend[name] = value
16
+ end
15
17
  end
16
18
  end
@@ -11,7 +11,7 @@ module Wasserstand
11
11
  end
12
12
 
13
13
  def to_s
14
- "#{@date}: #{@value} cm, trend #{@trend}"
14
+ "#{@date}: #{@value} cm, Trend #{@trend}"
15
15
  end
16
16
  end
17
17
  end
@@ -44,7 +44,7 @@ module Wasserstand
44
44
  #
45
45
  def replenish
46
46
  @names.clear
47
- STDERR.puts "FETCH #{@url}"
47
+ Wasserstand.logger.info "Fetching #{@url}"
48
48
  doc = Nokogiri::HTML(open(@url).read, nil, 'ISO-8859-1')
49
49
  doc.xpath("//data/table/gewaesser").each do |node|
50
50
  ww = WaterwayMapper.map(node)
@@ -1,3 +1,3 @@
1
1
  module Wasserstand
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -19,6 +19,8 @@ Gem::Specification.new do |gem|
19
19
  gem.add_runtime_dependency 'nokogiri'
20
20
  gem.add_runtime_dependency 'tzinfo'
21
21
  gem.add_runtime_dependency 'unicode_utils'
22
+ gem.add_runtime_dependency 'log4r'
23
+
22
24
  gem.add_development_dependency 'minitest'
23
25
  gem.add_development_dependency 'rake'
24
26
  gem.add_development_dependency 'pry'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wasserstand
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: log4r
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  - !ruby/object:Gem::Dependency
79
95
  name: minitest
80
96
  requirement: !ruby/object:Gem::Requirement
@@ -171,6 +187,7 @@ files:
171
187
  - LICENSE
172
188
  - README.md
173
189
  - Rakefile
190
+ - TODO
174
191
  - bin/wasserstand
175
192
  - lib/wasserstand.rb
176
193
  - lib/wasserstand/finders.rb
@@ -222,3 +239,4 @@ test_files:
222
239
  - test/unit/test_measurement.rb
223
240
  - test/unit/test_trend.rb
224
241
  - test/unit/test_waterway.rb
242
+ has_rdoc: