weatheruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4bc5408dc72cef114956f54043208d345fe2c91
4
+ data.tar.gz: 48a16aefd779d173f68164dd9b342e746988e9a4
5
+ SHA512:
6
+ metadata.gz: ee9e13401e525b4f375c1c720c502318aa46382b328f0b64729cef3a120542539212b43f47ffcee4a721d84defdfb7673642b24fcae206e1fa42dbbc8ad4ce32
7
+ data.tar.gz: bbe710d088da26c738f6cb2a38ac902899fdbf2c06a3ace9570fa4c4a1b8a6560671d13e5196184956d5b2877259562c463c90039e6f83befd205d5ac6ea2ff6
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+ ## Version 0
3
+ ### Version 0.1.0
4
+ * Intial version. Includes #conditions, #moon_phase, and #alerts.
@@ -0,0 +1,81 @@
1
+ module Weather
2
+ module Actions
3
+ # Gets alert information for a location.
4
+ # @param location [String] The place to get the alert data for.
5
+ # @return [Hash/Nil] Nil if there are no alerts, or a hash of arrays
6
+ # containing relevant data if not. Each array in the hash contains
7
+ # information for a different alert.
8
+ def alerts(location)
9
+ response = get('alerts', location)
10
+ if response['alerts'].nil?
11
+ return nil
12
+ else
13
+ ret = []
14
+ count = 0
15
+ response['alerts'].each do |a|
16
+ ret[count] = {
17
+ :type => a['type'],
18
+ :description => a['description'],
19
+ :date => a['date'],
20
+ :expires => a['expires'],
21
+ :message => a['message'],
22
+ }
23
+ count += 1
24
+ end
25
+ end
26
+
27
+ ret
28
+ end
29
+
30
+ # Gets the current moon phase of the location.
31
+ # @param location [String] The place to get the phase for.
32
+ # @return [Hash] A hash of two integers for the moon phase information.
33
+ # The age key in the hash contains the moon's age in days, and the
34
+ # illumination key contains the percentage of how illuminated it is.
35
+ def moon_phase(location)
36
+ response = get('astronomy', location)
37
+ ret = {}
38
+ ret[:age] = response['moon_phase']['ageOfMoon'].to_i
39
+ ret[:illumination] = response['moon_phase']['percentIlluminated'].to_i
40
+
41
+ ret
42
+ end
43
+
44
+ # Gets weahter conditions for the location.
45
+ # @param location [String] The place to get the weather report for.
46
+ # @return [Hash] A hash containing strings of relevant weather information.
47
+ def conditions(location)
48
+ response = get('conditions', location)
49
+ current_observation = response['current_observation']
50
+ display_location = current_observation['display_location']
51
+
52
+ ret = {
53
+ :full_name => display_location['full'],
54
+ :city_name => display_location['city'],
55
+ :state_abbreviation => display_location['state'],
56
+ :state_name => display_location['state_name'],
57
+ :country => display_location['country'],
58
+ :zip_code => display_location['zip'].to_i,
59
+ :updated => current_observation['observation_time'],
60
+ :weather => current_observation['weather'],
61
+ :formatted_temperature => current_observation['temperature_string'],
62
+ :temperature_f => current_observation['temp_f'],
63
+ :temperature_c => current_observation['temp_c'],
64
+ :humidity => current_observation['relative_humidity'],
65
+ :formatted_wind => current_observation['wind_string'],
66
+ :wind_direction => current_observation['wind_dir'],
67
+ :wind_degrees => current_observation['wind_degrees'],
68
+ :wind_speed => current_observation['wind_mph'],
69
+ :wind_gust_speed => current_observation['wind_gust_mph'].to_i,
70
+ :formatted_feelslike => current_observation['feelslike_string'],
71
+ :feelslike_f => current_observation['feelslike_f'].to_i,
72
+ :feelslike_c => current_observation['feelslike_c'].to_i
73
+ }
74
+
75
+ ret[:humidity] = ret[:humidity].sub('%', '').to_i
76
+
77
+ ret
78
+ end
79
+
80
+ end
81
+ end
data/lib/weatheruby.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'httpclient'
2
+ require 'json'
3
+ require_relative 'weather/actions'
4
+
5
+ class Weatheruby
6
+ include Weather::Actions
7
+
8
+ # Creates a new instance of Weatheruby.
9
+ # @param api_key [String] Your personal API key obtained on sign up for
10
+ # Weather Underground.
11
+ # @param language [String] The language code you would like to use.
12
+ # @param use_pws [Boolean] Whether to use the Personal Weather Station
13
+ # feature.
14
+ # @param use_bestfct [Boolean] Whether to use BestForecast.
15
+ def initialize(api_key, language = 'EN', use_pws = true, use_bestfct = true)
16
+ @api_key = api_key
17
+ @language_key = language.upcase
18
+ @use_pws = use_pws ? 1 : 0
19
+ @use_bestfct = use_bestfct ? 1 : 0
20
+
21
+ @client = HTTPClient.new
22
+ end
23
+
24
+ # Performs a generic HTTP GET request. This method should generally not be
25
+ # used by a standard user, unless there is not a method for a particular
26
+ # action/feature.
27
+ # @param feature [String] The "feature" parameter defined by Wunderground.
28
+ # @param location [String] The location of the query.
29
+ # @param autoparse [Boolean] Whether to automatically parse the response.
30
+ # @return [JSON/HTTPMessage] Parsed JSON if true, or raw response if not.
31
+ def get(feature, location, autoparse = true)
32
+ url = "http://api.wunderground.com/api/#{@api_key}/#{feature}/lang:" \
33
+ "#{@language_key}/pws:#{@use_pws}/bestfct:#{@use_bestfct}/q/" \
34
+ "#{location}.json"
35
+ url = URI.encode(url)
36
+ uri = URI.parse(url)
37
+ res = @client.get(uri)
38
+
39
+ if autoparse
40
+ return JSON.parse(res.body)
41
+ else
42
+ return res
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weatheruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eli Foster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Accessing the Weather Underground API through HTTPClient.
28
+ email: elifosterwy@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - CHANGELOG.md
34
+ - lib/weather/actions.rb
35
+ - lib/weatheruby.rb
36
+ homepage: https://github.com/elifoster/weatheruby
37
+ licenses:
38
+ - CC-BY-NC-ND-4.0
39
+ metadata:
40
+ issue_tracker: https://github.com/elifoster/weatheruby/issues
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.4.5.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A Ruby gem for accessing the Weather Underground API.
61
+ test_files: []