undergroundweather 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,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in UndergroundWeather.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # Underground Weather
2
+
3
+ ## Usage
4
+
5
+ ## Development
6
+
7
+ ### Testing
8
+ Using minitest/spec
9
+
10
+ ```
11
+ rake test
12
+ ```
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ desc "Test UndergroundWeather"
5
+ Rake::TestTask.new do |t|
6
+ t.libs.push "lib"
7
+ t.test_files = FileList['specs/**/*_spec.rb', 'specs/*_spec.rb']
8
+ t.verbose = true
9
+ end
@@ -0,0 +1,39 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module UndergroundWeather
6
+ class ApiCall
7
+ attr_reader :response, :error
8
+
9
+ BASE_URL = 'http://api.wunderground.com/api'
10
+
11
+ def initialize(api_key, feature, query)
12
+ @api_key = api_key
13
+ @feature = feature
14
+ @query = query
15
+ @error = false
16
+
17
+ @response = JSON.parse(get)
18
+ @error = true if @response['error']
19
+ end
20
+
21
+ def url
22
+ URI.parse("#{BASE_URL}/#{@api_key}/#{@feature}/q/#{@query}.json")
23
+ end
24
+
25
+ def get
26
+ puts "Calling #{url}"
27
+ resp = Net::HTTP.get_response(url)
28
+
29
+ if resp.code == "200"
30
+ resp.body
31
+ else
32
+ puts "error retrieving weather feed"
33
+ @error = true
34
+ {}
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,12 @@
1
+ module UndergroundWeather
2
+ class Client
3
+ attr_accessor :api_key
4
+
5
+ def initialize(api_key)
6
+ @api_key = api_key
7
+ end
8
+
9
+ include Features
10
+ end
11
+ end
12
+
@@ -0,0 +1,21 @@
1
+ module UndergroundWeather
2
+ class DailyForecast
3
+ attr_reader :day, :date, :high_f, :high_c, :low_f, :low_c, :conditions, :icon
4
+ alias_method :high, :high_f
5
+ alias_method :low, :low_f
6
+
7
+ def initialize(forecast)
8
+ @day = forecast['date']['weekday']
9
+ @date = Time.at(forecast['date']['epoch'].to_i)
10
+
11
+ @high_f = forecast['high']['fahrenheit']
12
+ @high_c = forecast['high']['celsius']
13
+
14
+ @low_f = forecast['low']['fahrenheit']
15
+ @low_c = forecast['low']['celsius']
16
+
17
+ @conditions = forecast['conditions']
18
+ @icon = forecast['icon_url']
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ module UndergroundWeather
2
+ class Astronomy
3
+ attr_reader :age_of_moon
4
+
5
+ def initialize(feed)
6
+ phase = feed['moon_phase']
7
+
8
+ @age_of_moon = phase['ageOfMoon']
9
+
10
+ @raw_illuminated = phase['percentIlluminated']
11
+
12
+ @raw_sunrise = { :hour => phase['sunrise']['hour'], :minute => phase['sunrise']['minute'] }
13
+ @raw_sunset = { :hour => phase['sunset']['hour'], :minute => phase['sunset']['minute'] }
14
+
15
+ @now = Time.new
16
+ end
17
+
18
+ def illuminated
19
+ "#{@raw_illuminated}%"
20
+ end
21
+
22
+ def sunrise
23
+ today_at_hour_and_minute(@raw_sunset)
24
+ end
25
+
26
+ def sunset
27
+ today_at_hour_and_minute(@raw_sunrise)
28
+ end
29
+
30
+ private
31
+
32
+ def today_at_hour_and_minute(time = {})
33
+ Time.new(@now.year, @now.month, @now.day, time[:hour], time[:minute])
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,69 @@
1
+ module UndergroundWeather
2
+ class CurrentConditions
3
+ attr_reader :city, :state, :country, :zip, :latitude,
4
+ :longitude, :elevation, :time_zone,
5
+ :time, :station, :weather, :temp_f, :temp_c,
6
+ :humidity, :wind, :wind_dir, :wind_mph, :icon, :url,
7
+ :heat_index_f, :heat_index_c, :dewpoint_f, :dewpoint_c,
8
+ :windchill_f, :windchill_c, :visibility_mi, :visibility_km,
9
+ :pressure_mb, :pressure_in
10
+
11
+ def initialize(feed)
12
+ obsv = feed['current_observation']
13
+
14
+ @city = obsv['display_location']['city']
15
+ @state = obsv['display_location']['state']
16
+ @country = obsv['display_location']['country']
17
+ @zip = obsv['display_location']['zip']
18
+ @longitude = obsv['display_location']['longitude']
19
+ @latitude = obsv['display_location']['latitude']
20
+ @elevation = obsv['display_location']['elevation']
21
+ @time_zone = obsv['local_tz_short']
22
+ @time = obsv['observation_time_rfc822']
23
+ @station = obsv['station_id']
24
+
25
+ @weather = obsv['weather']
26
+ @temp_f = obsv['temp_f']
27
+ @temp_c = obsv['temp_c']
28
+ @humidity = obsv['relative_humidity']
29
+ @wind = obsv['wind_string']
30
+ @wind_dir = obsv['wind_dir']
31
+ @wind_mph = obsv['wind_mph']
32
+
33
+ @heat_index_f = obsv['heat_index_f']
34
+ @heat_index_c = obsv['heat_index_c']
35
+ @windchill_f = obsv['windchill_f']
36
+ @windchill_c = obsv['windchill_c']
37
+ @dewpoint_f = obsv['dewpoint_f']
38
+ @dewpoint_c = obsv['dewpoint_c']
39
+ @visibility_mi = obsv['visibility_mi']
40
+ @visibility_km = obsv['visibility_km']
41
+ @pressure_mb = obsv['pressure_mb']
42
+ @pressure_in = obsv['pressure_in']
43
+
44
+ @icon = obsv['icon_url']
45
+ @url = obsv['forecast_url']
46
+ end
47
+
48
+ def temp
49
+ @temp_f
50
+ end
51
+
52
+ def heat_index
53
+ @head_index_f
54
+ end
55
+
56
+ def windchill
57
+ @windchill_f
58
+ end
59
+
60
+ def visibility
61
+ @visibility_mi
62
+ end
63
+
64
+ def dewpoint
65
+ @dewpoint_f
66
+ end
67
+ end
68
+ end
69
+
@@ -0,0 +1,17 @@
1
+ module UndergroundWeather
2
+ class Forecast
3
+ attr_reader :sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday
4
+
5
+ def initialize(feed)
6
+ days = []
7
+ feed['forecast']['simpleforecast']['forecastday'].each do |daily|
8
+ days << DailyForecast.new(daily)
9
+ end
10
+
11
+ puts days.inspect
12
+ days.each do |d|
13
+ instance_variable_set("@#{d.day.downcase}", d)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ module UndergroundWeather
2
+ class Radar
3
+ attr_reader :url, :image
4
+
5
+ def initialize(feed)
6
+ @url = feed['radar']['url']
7
+ @image = feed['radar']['image_url']
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ module UndergroundWeather
2
+ class Satellite
3
+ attr_reader :image, :image_vis, :image_ir4
4
+
5
+ def initialize(feed)
6
+ sat = feed['satellite']
7
+
8
+ @image = sat['image_url']
9
+ @image_vis = sat['image_url_vis']
10
+ @image_ir4 = sat['image_url_ir4']
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,11 @@
1
+ module UndergroundWeather
2
+ class Webcams < Array
3
+ def initialize(feed)
4
+ super()
5
+
6
+ feed['webcams'].each do |wc|
7
+ self << Webcam.new(wc)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ module UndergroundWeather
2
+ module Features
3
+
4
+ features = {
5
+ :conditions => { :class => CurrentConditions },
6
+ :forecast => { :class => Forecast },
7
+ :astronomy => { :class => Astronomy },
8
+ :radar => { :class => Radar },
9
+ :satellite => { :class => Satellite },
10
+ :webcams => { :class => Webcams },
11
+ :history => { :class => ''},
12
+ :alerts => { :class => ''},
13
+ :hourly => { :class => ''},
14
+ :hourly7day => { :class => ''},
15
+ :forecast7day => { :class => ''},
16
+ :yesterday => { :class => ''}
17
+ }
18
+
19
+ features.each do |feature, v|
20
+ define_method(feature) do |zip_code|
21
+ call = ApiCall.new(@api_key, feature, zip_code)
22
+ if call.response && !call.error
23
+ v[:class].new(call.response)
24
+ else
25
+ #?
26
+ "error"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,3 @@
1
+ module UndergroundWeather
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,45 @@
1
+ module UndergroundWeather
2
+ class Webcam
3
+ attr_reader :handle, :camid, :camindex, :station_id, :link,
4
+ :linktext, :cameratype, :organization, :neighborhood,
5
+ :zip, :city, :state, :country, :time_zone, :latitude,
6
+ :longitude, :updated, :downloaded, :image, :image_thumb, :url
7
+
8
+ def initialize(wc)
9
+ @handle = wc['handle']
10
+ @camid = wc['camid']
11
+ @camindex = wc['camindex']
12
+ @station_id = wc['assoc_station_id']
13
+ @camera_type = wc['cameratype']
14
+
15
+ @link = wc['link']
16
+ @link_text = wc['link_text']
17
+
18
+ @organization = wc['organization']
19
+ @neighborhood = wc['neighborhood']
20
+ @zip = wc['zip']
21
+ @city = wc['city']
22
+ @state = wc['sate']
23
+ @country = wc['country']
24
+ @time_zone = wc['tzname']
25
+ @latitude = wc['lat']
26
+ @longitude = wc['lon']
27
+
28
+ @updated_at = wc['updated']
29
+ @downloaded_at = wc['downloaded']
30
+ @is_recent = wc['isrecent']
31
+
32
+ @image = wc['CURRENTIMAGEURL']
33
+ @image_thumb = wc['WIDGETCURRENTIMAGEURL']
34
+
35
+ @url = wc['CAMURL']
36
+ end
37
+
38
+ def recent
39
+ @is_recent == '1' ? true : false
40
+ end
41
+ alias_method(:recent?, :recent)
42
+
43
+ end
44
+ end
45
+
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path(File.dirname(__FILE__))
2
+
3
+ require 'UndergroundWeather/version'
4
+ require 'UndergroundWeather/api_call'
5
+ require 'UndergroundWeather/daily_forecast'
6
+ require 'UndergroundWeather/webcam'
7
+
8
+ require 'UndergroundWeather/features/current_conditions'
9
+ require 'UndergroundWeather/features/forecast'
10
+ require 'UndergroundWeather/features/astronomy'
11
+ require 'UndergroundWeather/features/radar'
12
+ require 'UndergroundWeather/features/satellite'
13
+ require 'UndergroundWeather/features/webcams'
14
+
15
+ require 'UndergroundWeather/features'
16
+ require 'UndergroundWeather/client'
17
+
18
+ module UndergroundWeather
19
+ def self.new(*args)
20
+ Client.new(*args)
21
+ end
22
+ end
data/specs/helper.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'minitest/autorun'
2
+
3
+ ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
4
+ $LOAD_PATH << File.join(ROOT, 'lib')
5
+ $LOAD_PATH << File.join(ROOT, 'lib', 'UndergroundWeather')
6
+
7
+ require File.join(ROOT, 'lib', 'UndergroundWeather.rb')
8
+
@@ -0,0 +1,6 @@
1
+ require './specs/helper'
2
+
3
+ describe UndergroundWeather::ApiCall do
4
+
5
+ end
6
+
@@ -0,0 +1,17 @@
1
+ require './specs/helper'
2
+
3
+ describe UndergroundWeather::Client do
4
+ before do
5
+ @uw = UndergroundWeather::Client.new('api-key')
6
+ end
7
+
8
+ it 'can be created with an api_key' do
9
+ UndergroundWeather::Client.new('api-key').must_be_instance_of(UndergroundWeather::Client)
10
+ end
11
+
12
+ it 'can have an api key' do
13
+ @uw.api_key.must_equal('api-key')
14
+ end
15
+ end
16
+
17
+
@@ -0,0 +1,8 @@
1
+ require './specs/helper'
2
+
3
+ describe UndergroundWeather do
4
+ it "instaniate Client on new" do
5
+ uw = UndergroundWeather.new("a")
6
+ uw.must_be_instance_of(UndergroundWeather::Client)
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "undergroundweather/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "undergroundweather"
7
+ s.version = UndergroundWeather::VERSION
8
+ s.authors = ["brookemckim"]
9
+ s.email = ["brooke.mckim@gmail.com"]
10
+ s.homepage = "http://github.com/brookemckim/undergroundweather"
11
+ s.summary = %q{API Client for wunderground.com}
12
+ s.description = %q{API Client for the Weather Underground}
13
+
14
+ s.rubyforge_project = "undergroundweather"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec", '~>2.7.0'
23
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: undergroundweather
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - brookemckim
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-17 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: API Client for the Weather Underground
15
+ email:
16
+ - brooke.mckim@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - lib/undergroundweather.rb
26
+ - lib/undergroundweather/api_call.rb
27
+ - lib/undergroundweather/client.rb
28
+ - lib/undergroundweather/daily_forecast.rb
29
+ - lib/undergroundweather/features.rb
30
+ - lib/undergroundweather/features/astronomy.rb
31
+ - lib/undergroundweather/features/current_conditions.rb
32
+ - lib/undergroundweather/features/forecast.rb
33
+ - lib/undergroundweather/features/radar.rb
34
+ - lib/undergroundweather/features/satellite.rb
35
+ - lib/undergroundweather/features/webcams.rb
36
+ - lib/undergroundweather/version.rb
37
+ - lib/undergroundweather/webcam.rb
38
+ - specs/helper.rb
39
+ - specs/undergroundweather/api_call_spec.rb
40
+ - specs/undergroundweather/client_spec.rb
41
+ - specs/undergroundweather_spec.rb
42
+ - undergroundweather.gemspec
43
+ homepage: http://github.com/brookemckim/undergroundweather
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project: undergroundweather
63
+ rubygems_version: 1.8.10
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: API Client for wunderground.com
67
+ test_files: []