weatheruby 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7787ec777c7e28c511d3afb014c0e89adaf18652
4
- data.tar.gz: 61ab8ab8949b5d85605db19177d535bcad195b9b
3
+ metadata.gz: 080f4dae1ac613cf073d26a558b44df9c9e8cc60
4
+ data.tar.gz: f222373bc209df14fb962f995ab6a102151670e5
5
5
  SHA512:
6
- metadata.gz: a0a0573dcd72d6986343a7fff63cae502b0d77736bfa73d3a12a7758f4b9688efd4c25860b66f1cb4cc40edced7fc7406260ab021bec698d274a5597c695a9cc
7
- data.tar.gz: 80b259e9c15493c1d88970fce879f5ad19082968313866d17260a765305ac5d5e5cc9527ffb3c65ae5946dcda2774cca3c8423e8f03b56a5d3dbfddd19ba75ce
6
+ metadata.gz: 488fd79edc6c949452c50754a0e24d7598a311f20a969279cbe8bbc02541b09d9b6fcf40c797cb879be6967a43f2eed98f9e0ede18cd1ea6c116ac428db47a9c
7
+ data.tar.gz: 4a5d071ac7d746646c927f479165970bd3e813ab4e9229c200a3b4c3c0e550f1e4a3a0c833bc3e1f51b0e80abc272d0e5263820bfaac48def1249c43baae1baa
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
  ## Version 0
3
+ ### Version 0.4.0
4
+ * New weatheruby executable.
5
+ * Fail when there are many results and it cannot get actual data.
6
+ * Many style fixes.
7
+
3
8
  ### Version 0.3.1
4
9
  * Fix forecast names to 10day, and made it apparent in documentation that the non-10 day methods are only for the next 3 days.
5
10
 
data/bin/weatheruby ADDED
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ path = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(path)
6
+
7
+ require 'rainbow'
8
+ require 'string-utility'
9
+ require_relative '../lib/weatheruby'
10
+
11
+ fail ArgumentError if ARGV.empty?
12
+
13
+ @args = {
14
+ help: false,
15
+ verbose: false,
16
+ token: nil,
17
+ location: nil,
18
+ language: 'EN'
19
+ }
20
+
21
+ usage = 'Usage: weatheruby [t] token [-l] [-j] languagecode [-v] [-h]'
22
+ help = "-h, --help Show helpful information about Weatheruby usage\n" \
23
+ "-v, --verbose Output verbose errors\n" \
24
+ "-t, --token The token to log in as\n" \
25
+ "-l, --location The location to get weather data for\n" \
26
+ '-j, --language The language code to use'
27
+
28
+ ARGV.each do |arg|
29
+ case arg
30
+ when '-t', '--token' then @args[:token] = ARGV.at(ARGV.index(arg) + 1)
31
+ when '-h', '--help' then @args[:help] = true
32
+ when '-l', '--location' then @args[:location] = ARGV.at(ARGV.index(arg) + 1)
33
+ when '-v', '--verbose' then @args[:verbose] = true
34
+ when '-j', '--language' then @args[:language] = ARGV.at(ARGV.index(arg) + 1)
35
+ end
36
+ end
37
+
38
+ if @args[:help]
39
+ puts usage
40
+ puts help
41
+ exit
42
+ end
43
+
44
+ if @args[:token].nil? || @args[:location].nil?
45
+ puts usage
46
+ exit
47
+ end
48
+
49
+ client = Weatheruby.new(@args[:token],
50
+ @args[:language],
51
+ true,
52
+ true,
53
+ @args[:verbose])
54
+
55
+ conditions = client.conditions(@args[:location])
56
+ moon = client.moon_phase(@args[:location])
57
+ alerts = client.alerts(@args[:location])
58
+ forecast = client.simple_forecast_10day(@args[:location])
59
+
60
+ location = Rainbow(@args[:location]).color(StringUtility.random_color_six)
61
+ updated = Rainbow(conditions[:updated]).magenta
62
+ weather = Rainbow(conditions[:weather]).color(StringUtility.random_color_six)
63
+ temp = Rainbow(conditions[:formatted_temperature].to_s).cyan
64
+ humidity = Rainbow(conditions[:humidity].to_s).cyan
65
+ wind = Rainbow(conditions[:formatted_wind]).green
66
+ feels = Rainbow(conditions[:formatted_feelslike]).yellow
67
+ age = Rainbow(moon[:age].to_s).white
68
+ illum = Rainbow("#{moon[:illumination]} %").white
69
+
70
+ puts "Getting weather data for #{location}"
71
+ puts updated
72
+ puts weather
73
+ puts temp
74
+ puts feels
75
+ puts "Humidity: #{humidity} %"
76
+ puts wind
77
+ alerts.each do |a|
78
+ alert = Rainbow(a[:description]).red.bright + ' until ' +
79
+ Rainbow(a[:expires]).red.bright
80
+ puts alert
81
+ end
82
+ puts "Moon phase: #{age}"
83
+ puts "Moon illumination: #{illum}"
84
+
85
+ puts '* BEGIN 10 DAY FORECAST *'
86
+ 20.times do |i|
87
+ d = Rainbow(forecast[i][:weekday_name]).color(StringUtility.random_color_six)
88
+ puts "#{d}: #{forecast[i][:text]}"
89
+ end
@@ -12,11 +12,11 @@ module Weather
12
12
  count = 0
13
13
  response['alerts'].each do |a|
14
14
  ret[count] = {
15
- :type => a['type'],
16
- :description => a['description'],
17
- :date => a['date'],
18
- :expires => a['expires'],
19
- :message => a['message'],
15
+ type: a['type'],
16
+ description: a['description'],
17
+ date: a['date'],
18
+ expires: a['expires'],
19
+ message: a['message']
20
20
  }
21
21
  count += 1
22
22
  end
@@ -64,26 +64,26 @@ module Weather
64
64
  display_location = current_observation['display_location']
65
65
 
66
66
  ret = {
67
- :full_name => display_location['full'],
68
- :city_name => display_location['city'],
69
- :state_abbreviation => display_location['state'],
70
- :state_name => display_location['state_name'],
71
- :country => display_location['country'],
72
- :zip_code => display_location['zip'].to_i,
73
- :updated => current_observation['observation_time'],
74
- :weather => current_observation['weather'],
75
- :formatted_temperature => current_observation['temperature_string'],
76
- :temperature_f => current_observation['temp_f'],
77
- :temperature_c => current_observation['temp_c'],
78
- :humidity => current_observation['relative_humidity'],
79
- :formatted_wind => current_observation['wind_string'],
80
- :wind_direction => current_observation['wind_dir'],
81
- :wind_degrees => current_observation['wind_degrees'],
82
- :wind_speed => current_observation['wind_mph'],
83
- :wind_gust_speed => current_observation['wind_gust_mph'].to_i,
84
- :formatted_feelslike => current_observation['feelslike_string'],
85
- :feelslike_f => current_observation['feelslike_f'].to_i,
86
- :feelslike_c => current_observation['feelslike_c'].to_i
67
+ full_name: display_location['full'],
68
+ city_name: display_location['city'],
69
+ state_abbreviation: display_location['state'],
70
+ state_name: display_location['state_name'],
71
+ country: display_location['country'],
72
+ zip_code: display_location['zip'].to_i,
73
+ updated: current_observation['observation_time'],
74
+ weather: current_observation['weather'],
75
+ formatted_temperature: current_observation['temperature_string'],
76
+ temperature_f: current_observation['temp_f'],
77
+ temperature_c: current_observation['temp_c'],
78
+ humidity: current_observation['relative_humidity'],
79
+ formatted_wind: current_observation['wind_string'],
80
+ wind_direction: current_observation['wind_dir'],
81
+ wind_degrees: current_observation['wind_degrees'],
82
+ wind_speed: current_observation['wind_mph'],
83
+ wind_gust_speed: current_observation['wind_gust_mph'].to_i,
84
+ formatted_feelslike: current_observation['feelslike_string'],
85
+ feelslike_f: current_observation['feelslike_f'].to_i,
86
+ feelslike_c: current_observation['feelslike_c'].to_i
87
87
  }
88
88
 
89
89
  ret[:humidity] = ret[:humidity].sub('%', '').to_i
@@ -106,11 +106,11 @@ module Weather
106
106
 
107
107
  if response['response']['error'].nil?
108
108
  ret = {
109
- :average_low_f => response['almanac']['temp_low']['normal']['F'].to_i,
110
- :average_low_c => response['almanac']['temp_low']['normal']['C'].to_i,
111
- :record_year => response['almanac']['temp_low']['recordyear'].to_i,
112
- :record_low_f => response['almanac']['temp_low']['record']['F'].to_i,
113
- :record_low_c => response['almanac']['temp_low']['record']['C'].to_i
109
+ average_low_f: response['almanac']['temp_low']['normal']['F'].to_i,
110
+ average_low_c: response['almanac']['temp_low']['normal']['C'].to_i,
111
+ record_year: response['almanac']['temp_low']['recordyear'].to_i,
112
+ record_low_f: response['almanac']['temp_low']['record']['F'].to_i,
113
+ record_low_c: response['almanac']['temp_low']['record']['C'].to_i
114
114
  }
115
115
 
116
116
  ret
@@ -131,11 +131,11 @@ module Weather
131
131
 
132
132
  if response['response']['error'].nil?
133
133
  ret = {
134
- :average_high_f => response['almanac']['temp_high']['normal']['F'].to_i,
135
- :average_high_c => response['almanac']['temp_high']['normal']['C'].to_i,
136
- :record_year => response['almanac']['temp_high']['recordyear'].to_i,
137
- :record_high_f => response['almanac']['temp_high']['record']['F'].to_i,
138
- :record_high_c => response['almanac']['temp_high']['record']['C'].to_i
134
+ average_high_f: response['almanac']['temp_high']['normal']['F'].to_i,
135
+ average_high_c: response['almanac']['temp_high']['normal']['C'].to_i,
136
+ record_year: response['almanac']['temp_high']['recordyear'].to_i,
137
+ record_high_f: response['almanac']['temp_high']['record']['F'].to_i,
138
+ record_high_c: response['almanac']['temp_high']['record']['C'].to_i
139
139
  }
140
140
 
141
141
  ret
@@ -158,16 +158,16 @@ module Weather
158
158
  ret = {}
159
159
  response['currenthurricane'].each do |h|
160
160
  ret[h['stormInfo']['stormName_Nice']] = {
161
- :name => h['stormInfo']['stormName'],
162
- :number => h['stormInfo']['stormNumber'],
163
- :category => h['Current']['Category'],
164
- :time => h['Current']['Time']['pretty'],
165
- :wind_speed_mph => h['Current']['WindSpeed']['Mph'],
166
- :wind_speed_kts => h['Current']['WindSpeed']['Kts'],
167
- :wind_speed_kph => h['Current']['WindSpeed']['Kph'],
168
- :gust_speed_mph => h['Current']['WindGust']['Mph'],
169
- :gust_speed_kts => h['Current']['WindGust']['Kts'],
170
- :gust_speed_kph => h['Current']['WindGust']['Kph'],
161
+ name: h['stormInfo']['stormName'],
162
+ number: h['stormInfo']['stormNumber'],
163
+ category: h['Current']['Category'],
164
+ time: h['Current']['Time']['pretty'],
165
+ wind_speed_mph: h['Current']['WindSpeed']['Mph'],
166
+ wind_speed_kts: h['Current']['WindSpeed']['Kts'],
167
+ wind_speed_kph: h['Current']['WindSpeed']['Kph'],
168
+ gust_speed_mph: h['Current']['WindGust']['Mph'],
169
+ gust_speed_kts: h['Current']['WindGust']['Kts'],
170
+ gust_speed_kph: h['Current']['WindGust']['Kph']
171
171
  }
172
172
  end
173
173
 
@@ -191,7 +191,7 @@ module Weather
191
191
  def simple_forecast(location)
192
192
  response = get('forecast', location)
193
193
 
194
- return parse_simple_forecast(response)
194
+ parse_simple_forecast(response)
195
195
  end
196
196
 
197
197
  # Gets more complicated forecast information for the location. Only gets
@@ -205,7 +205,7 @@ module Weather
205
205
  def complex_forecast(location)
206
206
  response = get('forecast', location)
207
207
 
208
- return parse_complex_forecast(response)
208
+ parse_complex_forecast(response)
209
209
  end
210
210
 
211
211
  # Exactly the same as #simple_forecast, except that it gets the data for
@@ -213,7 +213,7 @@ module Weather
213
213
  def simple_forecast_10day(location)
214
214
  response = get('forecast10day', location)
215
215
 
216
- return parse_simple_forecast(response)
216
+ parse_simple_forecast(response)
217
217
  end
218
218
 
219
219
  # Exactly the same as #complex_forecast, except that it gets the data for
@@ -221,10 +221,11 @@ module Weather
221
221
  def complex_forecast_10day(location)
222
222
  response = get('forecast10day', location)
223
223
 
224
- return parse_complex_forecast(response)
224
+ parse_complex_forecast(response)
225
225
  end
226
226
 
227
227
  private
228
+
228
229
  # Parses the simple forecast information.
229
230
  def parse_simple_forecast(response)
230
231
  if response['response']['error'].nil?
@@ -232,9 +233,9 @@ module Weather
232
233
 
233
234
  response['forecast']['txt_forecast']['forecastday'].each do |f|
234
235
  ret[f['period']] = {
235
- :weekday_name => f['title'],
236
- :text => f['fcttext'],
237
- :text_metric => f['fcttext_metric']
236
+ weekday_name: f['title'],
237
+ text: f['fcttext'],
238
+ text_metric: f['fcttext_metric']
238
239
  }
239
240
  end
240
241
 
@@ -255,36 +256,36 @@ module Weather
255
256
 
256
257
  response['forecast']['simpleforecast']['forecastday'].each do |f|
257
258
  ret[f['period'] - 1] = {
258
- :high_f => f['high']['fahrenheit'].to_i,
259
- :high_c => f['high']['celsius'].to_i,
260
- :low_f => f['low']['fahrenheit'].to_i,
261
- :low_c => f['low']['celsius'].to_i,
262
- :conditions => f['conditions'].to_i,
263
- :snow => {
264
- :snow_total_in => f['snow_allday']['in'],
265
- :snow_total_cm => f['snow_allday']['cm'],
266
- :snow_night_in => f['snow_night']['in'],
267
- :snow_night_cm => f['snow_night']['cm'],
268
- :snow_day_in => f['snow_day']['in'],
269
- :snow_day_cm => f['snow_day']['cm']
259
+ high_f: f['high']['fahrenheit'].to_i,
260
+ high_c: f['high']['celsius'].to_i,
261
+ low_f: f['low']['fahrenheit'].to_i,
262
+ low_c: f['low']['celsius'].to_i,
263
+ conditions: f['conditions'].to_i,
264
+ snow: {
265
+ snow_total_in: f['snow_allday']['in'],
266
+ snow_total_cm: f['snow_allday']['cm'],
267
+ snow_night_in: f['snow_night']['in'],
268
+ snow_night_cm: f['snow_night']['cm'],
269
+ snow_day_in: f['snow_day']['in'],
270
+ snow_day_cm: f['snow_day']['cm']
270
271
  },
271
- :quantative_precipitation => {
272
- :qpf_total_in => f['qpf_allday']['in'],
273
- :qpf_total_cm => f['qpf_allday']['cm'],
274
- :qpf_night_in => f['qpf_night']['in'],
275
- :qpf_night_cm => f['qpf_night']['cm'],
276
- :qpf_day_in => f['qpf_day']['in'],
277
- :qpf_day_cm => f['qpf_day']['cm']
272
+ quantative_precipitation: {
273
+ qpf_total_in: f['qpf_allday']['in'],
274
+ qpf_total_cm: f['qpf_allday']['cm'],
275
+ qpf_night_in: f['qpf_night']['in'],
276
+ qpf_night_cm: f['qpf_night']['cm'],
277
+ qpf_day_in: f['qpf_day']['in'],
278
+ qpf_day_cm: f['qpf_day']['cm']
278
279
  },
279
- :wind => {
280
- :average_mph => f['avewind']['mph'],
281
- :average_kph => f['avewind']['kph'],
282
- :average_dir => f['avewind']['dir'],
283
- :average_temp => f['avewind']['degrees'],
284
- :max_mph => f['maxwind']['mph'],
285
- :max_kph => f['maxwind']['kph'],
286
- :max_dir => f['maxwind']['dir'],
287
- :max_temp => f['maxwind']['degrees']
280
+ wind: {
281
+ average_mph: f['avewind']['mph'],
282
+ average_kph: f['avewind']['kph'],
283
+ average_dir: f['avewind']['dir'],
284
+ average_temp: f['avewind']['degrees'],
285
+ max_mph: f['maxwind']['mph'],
286
+ max_kph: f['maxwind']['kph'],
287
+ max_dir: f['maxwind']['dir'],
288
+ max_temp: f['maxwind']['degrees']
288
289
  }
289
290
  }
290
291
  end
data/lib/weatheruby.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'httpclient'
2
2
  require 'json'
3
3
  require_relative 'weather/actions'
4
+ require_relative 'weather/exceptions'
4
5
 
5
6
  class Weatheruby
6
7
  include Weather::Actions
@@ -38,10 +39,13 @@ class Weatheruby
38
39
  url = URI.encode(url)
39
40
  uri = URI.parse(url)
40
41
  res = @client.get(uri)
42
+ json = JSON.parse(res.body)
43
+ unless json['response']['results'].nil?
44
+ fail Weather::Exceptions::TooManyResultsError
45
+ end
41
46
 
42
47
  if autoparse
43
- return JSON.parse(res.body)
44
- # puts JSON.parse(res.body)
48
+ return json
45
49
  else
46
50
  return res
47
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weatheruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-18 00:00:00.000000000 Z
11
+ date: 2015-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -24,13 +24,43 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rainbow
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: string-utility
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  description: Accessing the Weather Underground API through HTTPClient.
28
56
  email: elifosterwy@gmail.com
29
- executables: []
57
+ executables:
58
+ - weatheruby
30
59
  extensions: []
31
60
  extra_rdoc_files: []
32
61
  files:
33
62
  - CHANGELOG.md
63
+ - bin/weatheruby
34
64
  - lib/weather/actions.rb
35
65
  - lib/weatheruby.rb
36
66
  homepage: https://github.com/elifoster/weatheruby
@@ -59,3 +89,4 @@ signing_key:
59
89
  specification_version: 4
60
90
  summary: A Ruby gem for accessing the Weather Underground API.
61
91
  test_files: []
92
+ has_rdoc: