benschwarz-bom-weather 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,12 @@
1
+ BOMWeather
2
+ ----------
3
+
4
+
5
+ Weather from the Australian Bureau of Meteorology
6
+
7
+ More information in the wrap up stages (lets get a gem together first!)
8
+
9
+ Locations output forecasts
10
+ Issue time and date and forecast date are mapped to all objects, this is incorrect behavior.
11
+
12
+ All in all it is "working"
@@ -0,0 +1 @@
1
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,15 @@
1
+ # Dependencies
2
+ require 'rubygems'
3
+ require 'open-uri'
4
+ require 'validatable'
5
+ require 'time'
6
+ require 'date'
7
+
8
+ # Create namespace
9
+ module BOMWeather
10
+ VERSION = '0.0.1'
11
+ API_BASE = 'ftp://ftp2.bom.gov.au/anon/gen/fwo/'
12
+ end
13
+
14
+ # Require classes
15
+ %w(query base forecast parser location).each{|r| require File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), r)}
@@ -0,0 +1,4 @@
1
+ # Convenience methods
2
+ module BOMWeather
3
+
4
+ end
@@ -0,0 +1,19 @@
1
+ module BOMWeather
2
+ class Forecast
3
+ attr_accessor :forecast_date, :issue_time, :max_temp, :min_temp, :conditions
4
+
5
+ include Validatable
6
+ validates_presence_of :forecast_date, :issue_time, :max_temp, :min_temp, :conditions
7
+
8
+ #validates_true_for :forecast_date, :logic => lambda { forecast_date.is_a?(Time) }
9
+ #validates_true_for :issue_time, :logic => lambda { issue_time.is_a?(Time) }
10
+
11
+ def initialize(params)
12
+ @forecast_date = params[:forecast_date]
13
+ @issue_time = params[:issue_time]
14
+ @conditions = params[:conditions]
15
+
16
+ @max_temp, @min_temp = params[:max_temp], params[:min_temp]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ module BOMWeather
2
+ class Location
3
+ attr_reader :location_id, :name, :state, :outlook
4
+
5
+ def initialize(location={})
6
+ @location_id, @name, @state = location[:loc_id], location[:location], location[:state]
7
+ map_forecast(location)
8
+ end
9
+
10
+ # Single key finder only
11
+ def self.find(finder={})
12
+ locations = Query.new.execute.rows.find_all{|row| row[finder.keys.first] =~ /#{finder[finder.keys.first]}/i}
13
+
14
+ @loc = locations.uniq.map do |location|
15
+ Location.new(location)
16
+ end
17
+
18
+ return @loc
19
+ end
20
+
21
+ def today
22
+ @outlook.first
23
+ end
24
+
25
+ private
26
+ def map_forecast(forecast_hash)
27
+
28
+ # Forecasts come in with min, max and forecast (a description)
29
+ # we want to create groups of each of these, check for nils as a group
30
+ # map the @outlook to these objects
31
+
32
+ @outlook = []
33
+ (0..7).to_a.each do |i|
34
+ # Min, Max, forecast
35
+
36
+ forecast = {
37
+ :forecast_date => forecast_hash[:forecast_date],
38
+ :issue_time => forecast_hash[:issue_time],
39
+ :max_temp => forecast_hash["max_#{i}".to_sym],
40
+ :min_temp => forecast_hash["min_#{i}".to_sym],
41
+ :conditions => forecast_hash["forecast_#{i}".to_sym]
42
+ }
43
+
44
+ @outlook << Forecast.new(forecast)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,31 @@
1
+ module BOMWeather
2
+ class Parser
3
+ attr_reader :headers, :rows
4
+
5
+ def initialize(input_stream)
6
+ @data = input_stream.split("\n")
7
+ parse_headers
8
+ parse_rows
9
+ end
10
+
11
+ private
12
+
13
+ def parse_headers
14
+ # Get the first line as headers, split apart and create symbols
15
+ @headers = @data.shift.split("#").map{ |h| h.to_sym }
16
+ end
17
+
18
+ def parse_rows
19
+ @rows = @data.map do |row|
20
+ fields = row.split('#')
21
+ hash = {}
22
+
23
+ @headers.each_with_index do |header, index|
24
+ hash[header] = fields[index]
25
+ end
26
+
27
+ hash
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ module BOMWeather
2
+ class Query
3
+ attr_accessor :uri
4
+
5
+ def initialize(uri="ftp://ftp2.bom.gov.au/anon/gen/fwo/IDA00001.dat")
6
+ @uri = uri
7
+ end
8
+
9
+ def execute
10
+ Parser.new(open(@uri).read)
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benschwarz-bom-weather
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Schwarz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-29 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: validatable
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.6.7
23
+ version:
24
+ description: Weather from the Australian Bureau of Meteorology
25
+ email: ben.schwarz@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - Rakefile
34
+ - README
35
+ - lib/bom-weather.rb
36
+ - lib/bom-weather/base.rb
37
+ - lib/bom-weather/forecast.rb
38
+ - lib/bom-weather/location.rb
39
+ - lib/bom-weather/parser.rb
40
+ - lib/bom-weather/query.rb
41
+ has_rdoc: false
42
+ homepage: http://github.com/benschwarz/bom-weather
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.0.1
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Weather from the Australian Bureau of Meteorology
67
+ test_files: []
68
+