tide 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ # gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.2"
12
+ gem "rcov", ">= 0"
13
+ gem "rdoc"
14
+ end
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.2)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2)
10
+ rcov (0.9.9)
11
+ rdoc (3.5.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.2)
19
+ rcov
20
+ rdoc
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Kenzie Campbell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ = Tide
2
+
3
+ Tide class fetches returns Canadian tide prediction data from waterlevels.gc.ca
4
+
5
+ Accepts options for tide zone, tide region, tide station, date and timezone. Defaults to 7 days of tide prediction data (starting from today) for Sydney, Nova Scotia in Atlantic time.
6
+
7
+ Outputs a semi-colon delimited array of tides (date, time, height in meters) with Tide.new.to_csv or a formatted html table with Tide.new.to_html.
8
+
9
+ == Example Output
10
+
11
+ ===Tide.new.to_csv
12
+
13
+ 2010-02-11,00:47,0.3
14
+ 2010-02-11,07:17,0.9
15
+ 2010-02-11,12:23,0.6
16
+ ...
17
+
18
+ ===Tide.new.to_html
19
+
20
+ <table summary="Tide tables for Sydney with columns for time of day and tide height (in meters).">
21
+ <thead>
22
+ <tr><th scope="col">Time</th><th scope="col">Height</th></tr>
23
+ </thead>
24
+ <tbody>
25
+ <tr><th scope="rowgroup" colspan="2">2010-02-11</th></tr>
26
+ <tr><td>00:47</td><td>0.3m</td></tr>
27
+ <tr><td>07:17</td><td>0.9m</td></tr>
28
+ <tr><td>12:23</td><td>0.6m</td></tr>
29
+ <tr><td>18:47</td><td>1.1m</td></tr>
30
+ </tbody>
31
+ ...
32
+ </table>
33
+
34
+ == Contributing to tide
35
+
36
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
37
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
38
+ * Fork the project
39
+ * Start a feature/bugfix branch
40
+ * Commit and push until you are happy with your contribution
41
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
42
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
43
+
44
+ == Copyright
45
+
46
+ Copyright (c) 2011 Kenzie Campbell. See LICENSE.txt for
47
+ further details.
48
+
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "tide"
18
+ gem.homepage = "http://github.com/kenzie/tide"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Tide class fetches returns Canadian tide prediction data from waterlevels.gc.ca}
21
+ gem.description = %Q{Tide class fetches returns Canadian tide prediction data from waterlevels.gc.ca
22
+
23
+ Accepts options for tide zone, tide region, tide station, date and timezone. Defaults to 7 days of tide prediction data (starting from today) for Sydney, Nova Scotia in Atlantic time.
24
+
25
+ Outputs a semi-colon delimited array of tides (date, time, height in meters) with Tide.new.to_csv or a formatted html table with Tide.new.to_html.}
26
+ gem.email = "kenzie@route19.com"
27
+ gem.authors = ["Kenzie Campbell"]
28
+ # dependencies defined in Gemfile
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rake/testtask'
33
+ Rake::TestTask.new(:test) do |test|
34
+ test.libs << 'lib' << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+
39
+ require 'rcov/rcovtask'
40
+ Rcov::RcovTask.new do |test|
41
+ test.libs << 'test'
42
+ test.pattern = 'test/**/test_*.rb'
43
+ test.verbose = true
44
+ test.rcov_opts << '--exclude "gems/*"'
45
+ end
46
+
47
+ task :default => :test
48
+
49
+ require 'rdoc/task'
50
+ RDoc::Task.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "tide #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1 @@
1
+ rdoc/
@@ -0,0 +1,85 @@
1
+
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ class Tide
6
+
7
+ POST_URL = 'http://www.waterlevels.gc.ca/cgi-bin/tide-shc.cgi'
8
+ TIDE_REGEX = /#\sDate;Time;Height<br>(.*)<br><\/p><\/td>/
9
+ STATION_REGEX = /# Station : (.*) \(\d+\).*/
10
+ INIT_DEFAULTS = { :zone => 27, :region => 5, :station => 610, :timezone => 'AST', :date => Time.now }
11
+
12
+ attr_reader :location
13
+
14
+ # Create new tide table.
15
+ def initialize(options = {})
16
+ options = INIT_DEFAULTS.merge(options)
17
+ @zone = options[:zone]
18
+ @region = options[:region]
19
+ @station = options[:station]
20
+ @timezone = options[:timezone]
21
+ @date = options[:date]
22
+ @location = data.match(STATION_REGEX)[1]
23
+ end
24
+
25
+ # Downlaod raw data
26
+ def data
27
+ @data ||= download_tide_data
28
+ end
29
+
30
+ # Returns semi-colon delimited list of tide data.
31
+ def to_csv
32
+ @csv ||= format_csv
33
+ end
34
+
35
+ # Returns formatted html table of tide data.
36
+ def to_html
37
+ @html ||= format_html
38
+ end
39
+
40
+ private
41
+
42
+ # Downloads new tide data
43
+ def download_tide_data(type='predict', view='text', language='english')
44
+ res = Net::HTTP.post_form(
45
+ URI.parse(POST_URL),
46
+ {
47
+ 'zone' => @zone,
48
+ 'region' => @region,
49
+ 'station' => @station,
50
+ 'year' => @date.year,
51
+ 'month' => @date.month,
52
+ 'day' => @date.day,
53
+ 'TZ' => @timezone, # TODO use supplied date for timezone
54
+ 'queryType' => type,
55
+ 'view' => view,
56
+ 'language' => language
57
+ }
58
+ )
59
+ # TODO rescue request errors
60
+ res.body
61
+ end
62
+
63
+ # Formats tide data as csv.
64
+ def format_csv
65
+ data.match(TIDE_REGEX)[1].gsub('<br>', "\n").gsub(';', ',')
66
+ end
67
+
68
+ # Formats tide data as html table.
69
+ def format_html
70
+ formatted = "<table summary=\"Tide tables for #{location} with columns for time of day and tide height (in meters).\">\n\t<thead>\n\t\t<tr><th scope=\"col\">Time</th><th scope=\"col\">Height</th></tr>\n\t</thead>\n"
71
+ last_date = nil
72
+ to_csv.each_line do |row|
73
+ row = row.chomp.split(',')
74
+ if row[0] != last_date
75
+ formatted += "\t</tbody>\n" unless last_date.nil?
76
+ formatted += "\t<tbody>\n\t\t<tr><th scope=\"rowgroup\" colspan=\"2\">#{row[0]}</th></tr>\n"
77
+ last_date = row[0]
78
+ end
79
+ formatted += "\t\t<tr><td>#{row[1]}</td><td>#{row[2]}m</td></tr>\n"
80
+ end
81
+ formatted += "\t</tbody>\n</table>\n"
82
+ formatted
83
+ end
84
+
85
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ # require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'tide'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,39 @@
1
+
2
+ require 'helper'
3
+
4
+ class TideTest < Test::Unit::TestCase
5
+
6
+ # TODO fake web requests
7
+
8
+ def test_location
9
+ @tide = Tide.new(:zone => 27, :region => 5, :station => 610)
10
+ assert_match "Sydney", @tide.location
11
+ end
12
+
13
+ def test_to_csv
14
+ @tide = Tide.new
15
+ date_regex = /\d{4}-\d{2}-\d{2}/
16
+ assert_match date_regex, @tide.to_csv
17
+ end
18
+
19
+ def test_to_html
20
+ @tide = Tide.new
21
+ date_regex = /<tr><th scope=\"rowgroup\" colspan=\"2\">\d{4}-\d{2}-\d{2}<\/th><\/tr>/
22
+ assert_match date_regex, @tide.to_html
23
+ end
24
+
25
+ def test_specific_date
26
+ a_year_ago = Time.now - 60*60*24*365
27
+ @tide = Tide.new(:date => a_year_ago)
28
+ formatted_date = a_year_ago.strftime('%Y-%m-%d')
29
+ date_regex = /<tr><th scope=\"rowgroup\" colspan=\"2\">#{formatted_date}<\/th><\/tr>/
30
+ assert_match date_regex, @tide.to_html
31
+ end
32
+
33
+ def test_specific_timezone
34
+ tide_ast = Tide.new(:timezone => 'AST')
35
+ tide_est = Tide.new(:timezone => 'EST')
36
+ assert_not_equal tide_ast.to_csv, tide_est.to_csv
37
+ end
38
+
39
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tide}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Kenzie Campbell}]
12
+ s.date = %q{2011-06-09}
13
+ s.description = %q{Tide class fetches returns Canadian tide prediction data from waterlevels.gc.ca
14
+
15
+ Accepts options for tide zone, tide region, tide station, date and timezone. Defaults to 7 days of tide prediction data (starting from today) for Sydney, Nova Scotia in Atlantic time.
16
+
17
+ Outputs a semi-colon delimited array of tides (date, time, height in meters) with Tide.new.to_csv or a formatted html table with Tide.new.to_html.}
18
+ s.email = %q{kenzie@route19.com}
19
+ s.extra_rdoc_files = [
20
+ "LICENSE.txt",
21
+ "README.rdoc"
22
+ ]
23
+ s.files = [
24
+ ".document",
25
+ "Gemfile",
26
+ "Gemfile.lock",
27
+ "LICENSE.txt",
28
+ "README.rdoc",
29
+ "Rakefile",
30
+ "VERSION",
31
+ "lib/.gitignore",
32
+ "lib/tide.rb",
33
+ "test/helper.rb",
34
+ "test/test_tide.rb",
35
+ "tide.gemspec"
36
+ ]
37
+ s.homepage = %q{http://github.com/kenzie/tide}
38
+ s.licenses = [%q{MIT}]
39
+ s.require_paths = [%q{lib}]
40
+ s.rubygems_version = %q{1.8.5}
41
+ s.summary = %q{Tide class fetches returns Canadian tide prediction data from waterlevels.gc.ca}
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
49
+ s.add_development_dependency(%q<rcov>, [">= 0"])
50
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
54
+ s.add_dependency(%q<rcov>, [">= 0"])
55
+ s.add_dependency(%q<rdoc>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ s.add_dependency(%q<rdoc>, [">= 0"])
62
+ end
63
+ end
64
+
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tide
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Kenzie Campbell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-09 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: jeweler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rcov
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rdoc
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ description: |-
60
+ Tide class fetches returns Canadian tide prediction data from waterlevels.gc.ca
61
+
62
+ Accepts options for tide zone, tide region, tide station, date and timezone. Defaults to 7 days of tide prediction data (starting from today) for Sydney, Nova Scotia in Atlantic time.
63
+
64
+ Outputs a semi-colon delimited array of tides (date, time, height in meters) with Tide.new.to_csv or a formatted html table with Tide.new.to_html.
65
+ email: kenzie@route19.com
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files:
71
+ - LICENSE.txt
72
+ - README.rdoc
73
+ files:
74
+ - .document
75
+ - Gemfile
76
+ - Gemfile.lock
77
+ - LICENSE.txt
78
+ - README.rdoc
79
+ - Rakefile
80
+ - VERSION
81
+ - lib/.gitignore
82
+ - lib/tide.rb
83
+ - test/helper.rb
84
+ - test/test_tide.rb
85
+ - tide.gemspec
86
+ homepage: http://github.com/kenzie/tide
87
+ licenses:
88
+ - MIT
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 537320941182422965
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.5
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Tide class fetches returns Canadian tide prediction data from waterlevels.gc.ca
116
+ test_files: []
117
+