meteo 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -10,4 +10,8 @@
10
10
 
11
11
  == Version 1.0.2
12
12
 
13
- * Bug fixes.
13
+ * Bug fixes.
14
+
15
+ == Version 1.1.0
16
+
17
+ * Adding forecast flag.
data/bin/meteo CHANGED
@@ -7,6 +7,7 @@ require 'meteo/meteo_cli'
7
7
  params = []
8
8
 
9
9
  units = "imperial"
10
+ forecast = 0
10
11
 
11
12
  index = 0
12
13
 
@@ -18,6 +19,11 @@ while index < ARGV.size do
18
19
  elsif param =~ /--units/ or param =~ /-u/
19
20
  units = ARGV[index+1]
20
21
  index = index + 1
22
+ elsif param =~ /--forecast=/ or param =~ /-f=/
23
+ forecast = ARGV[index][ARGV[index].index("=")+1..-1]
24
+ elsif param =~ /--forecast/ or param =~ /-f/
25
+ forecast = ARGV[index+1]
26
+ index = index + 1
21
27
  else
22
28
  params << param
23
29
  end
@@ -25,4 +31,4 @@ while index < ARGV.size do
25
31
  index = index + 1
26
32
  end
27
33
 
28
- MeteoCLI.start ["quote", "--units=#{units}", "#{params.join(' ')}"]
34
+ MeteoCLI.start ["quote", "--units=#{units}", "--forecast=#{forecast}", "#{params.join(' ')}"]
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Colors
4
+ def sun
5
+ "\033[33;1m\xe2\x98\x80"
6
+ end
7
+
8
+ def moon
9
+ "\033[36m\xe2\x98\xbd"
10
+ end
11
+
12
+ def clouds
13
+ "\033[37;1m\xe2\x98\x81"
14
+ end
15
+
16
+ def rain
17
+ "\xe2\x98\x94"
18
+ end
19
+
20
+ def fog
21
+ "\033[37;1m\xe2\x96\x92"
22
+ end
23
+
24
+ def snow
25
+ "\033[37;1m\xe2\x9d\x84"
26
+ end
27
+
28
+ def thunderstorm
29
+ "\xe2\x9a\xa1"
30
+ end
31
+
32
+ def background
33
+ "\033[44m"
34
+ end
35
+
36
+ def dashes
37
+ "\033[34m-"
38
+ end
39
+
40
+ def text
41
+ "\033[36;1m"
42
+ end
43
+
44
+ def delimiter
45
+ "\033[35m=>"
46
+ end
47
+
48
+ def data
49
+ "\033[33;1m"
50
+ end
51
+
52
+ def stop
53
+ "\033[0m"
54
+ end
55
+ end
data/lib/meteo/geo.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class Geo
5
+ GEO_LOCATION_SERVICE_URL = "http://freegeoip.net/json/"
6
+
7
+ attr_reader :url
8
+
9
+ def initialize
10
+ @url = GEO_LOCATION_SERVICE_URL
11
+ end
12
+
13
+ def quote ip_address=nil
14
+ quote_url = ip_address ? "#{url}#{ip_address}" : url
15
+
16
+ uri = URI.parse(URI.escape(quote_url))
17
+
18
+ JSON.parse(Net::HTTP.get(uri))
19
+ end
20
+ end
data/lib/meteo/meteo.rb CHANGED
@@ -1,19 +1,24 @@
1
1
  require 'net/http'
2
+ require 'json'
2
3
 
3
4
  class Meteo
4
- OPEN_WEATHER_MAP_SERVICE_URL = 'http://api.openweathermap.org/data/2.5/weather'
5
+ OPEN_WEATHER_MAP_SERVICE_URL = 'http://api.openweathermap.org/data/2.5'
5
6
 
6
7
  attr_reader :url
7
8
 
8
- def initialize
9
- @url = OPEN_WEATHER_MAP_SERVICE_URL
9
+ def initialize(forecast=false)
10
+ if forecast
11
+ @url = "#{OPEN_WEATHER_MAP_SERVICE_URL}/forecast/daily"
12
+ else
13
+ @url = "#{OPEN_WEATHER_MAP_SERVICE_URL}/weather"
14
+ end
10
15
  end
11
16
 
12
17
  def quote location, units
13
- quote_url = "#{url}?q=#{location}%20nj&units=#{units}"
18
+ quote_url = "#{url}?q=#{location}&units=#{units}"
14
19
 
15
20
  uri = URI.parse(URI.escape(quote_url))
16
21
 
17
- Net::HTTP.get(uri)
22
+ JSON.parse(Net::HTTP.get(uri))
18
23
  end
19
24
  end
@@ -1,10 +1,9 @@
1
1
  require "thor"
2
- require 'json'
3
- require 'meteo'
4
- require 'meteo/reporter'
2
+ require 'meteo/meteo'
3
+ require 'meteo/geo'
4
+ require 'meteo/weather_reporter'
5
5
 
6
6
  class MeteoCLI < Thor
7
- include Reporter
8
7
 
9
8
  desc "quote location", "quote weather for location"
10
9
  long_desc <<-LONGDESC
@@ -15,20 +14,23 @@ class MeteoCLI < Thor
15
14
 
16
15
  > $ meteo Plainsboro, NJ
17
16
  > $ meteo Moscow, RU --units=metric
17
+ > $ meteo # get weather for current location based on IP address
18
18
  LONGDESC
19
19
  option :units, :aliases => "-u"
20
+ option :forecast
20
21
  def quote(location)
22
+ location = (location.nil? or location.strip.size == 0) ? Geo.new.quote : location
23
+
21
24
  units = options[:units] ? options[:units] : "imperial"
25
+ forecast = options[:forecast] ? options[:forecast].to_i : 0
26
+
27
+ service = Meteo.new(forecast > 0)
22
28
 
23
- service = Meteo.new
29
+ response = service.quote(location, units)
24
30
 
25
- response = JSON.parse(service.quote(location, units))
31
+ reporter = WeatherReporter.new
26
32
 
27
- if response["message"]
28
- puts response["message"]
29
- else
30
- puts report(response, units).join(' ')
31
- end
33
+ reporter.report(response, units, forecast)
32
34
  end
33
35
 
34
36
  end
data/lib/meteo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Meteo
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,147 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'meteo/colors'
4
+ require 'awesome_print'
5
+
6
+ class WeatherReporter
7
+ include Colors
8
+
9
+ WIND_DIRECTIONS = %W(N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW)
10
+
11
+ def report(response, units, forecast)
12
+ if forecast == 0
13
+ current_weather_report response, units
14
+ else
15
+ forecast_report response, units, forecast
16
+ end
17
+ end
18
+
19
+ def current_weather_report response, units
20
+ if response["message"]
21
+ puts response["message"]
22
+ else
23
+ sunrise = response['sys']['sunrise']
24
+ sunset = response['sys']['sunset']
25
+ pressure = response['main']['pressure']
26
+ city = response['name']
27
+ temperature = response['main']['temp']
28
+ humidity = response['main']['humidity']
29
+ sky = response['weather'][0]['main']
30
+ wind = response['wind']['speed']
31
+ azimuth = response['wind']['deg']
32
+
33
+ direction = WIND_DIRECTIONS[((azimuth + 11.25)/22.5 % 16).to_i]
34
+
35
+ result = %W(
36
+ #{background}#{text}#{get_icon(sky, current_period(sunset, sunrise))}#{dashes}#{text}
37
+ #{city} #{delimiter}#{data}
38
+ #{text}Temperature #{data}#{temperature}#{temperature_scale(units)}
39
+ #{text}Humidity #{data}#{humidity}%
40
+ #{text}Wind #{data}#{wind}#{speed_unit(units)} #{direction}
41
+ #{text}Pressure #{data}#{pressure(pressure, units)}#{pressure_unit(units)}
42
+ #{text}Sunrise #{data}#{Time.at(sunrise).strftime('%I:%M:%S%p')}
43
+ #{text}Sunset #{data}#{Time.at(sunset).strftime('%I:%M:%S%p')}#{stop}
44
+ )
45
+
46
+ puts result.join(' ')
47
+ end
48
+ end
49
+
50
+ def forecast_report response, units, forecast
51
+ city = response['city']['name']
52
+
53
+ max_forecast = response['list'].size
54
+
55
+ forecast = (forecast <= max_forecast) ? forecast : max_forecast
56
+
57
+ puts "#{background}#{text}Forecast for #{city}:"
58
+
59
+ (0..forecast-1).each do |index|
60
+ day = response['list'][index]
61
+ sky = day['weather'][0]['main']
62
+ date = day['dt']
63
+ max = day['temp']['max']
64
+ min = day['temp']['min']
65
+ morning = day['temp']['morn']
66
+ evening = day['temp']['eve']
67
+ night = day['temp']['night']
68
+ humidity = day['humidity']
69
+ pressure = day['pressure']
70
+
71
+ wind = day['speed']
72
+ azimuth = day['deg']
73
+
74
+ direction = WIND_DIRECTIONS[((azimuth + 11.25)/22.5 % 16).to_i]
75
+
76
+ result = %W(
77
+ #{background}#{text}#{index+1}. #{get_icon(sky, 'none')}
78
+ #{data}#{Time.at(date).strftime('%Y/%m/%d')}
79
+ #{text}Temperature
80
+ #{text}min: #{data}#{min}#{temperature_scale(units)}
81
+ #{text}max: #{data}#{max}#{temperature_scale(units)}
82
+ #{text}morning: #{data}#{morning}#{temperature_scale(units)}
83
+ #{text}evening: #{data}#{evening}#{temperature_scale(units)}
84
+ #{text}night: #{data}#{night}#{temperature_scale(units)}
85
+ #{text}Humidity #{data}#{humidity}%
86
+ #{text}Wind #{data}#{wind}#{speed_unit(units)} #{direction}
87
+ #{text}Pressure #{data}#{pressure(pressure, units)}#{pressure_unit(units)}#{stop}
88
+ )
89
+
90
+ puts result.join(' ')
91
+ end
92
+ end
93
+
94
+ def get_icon(sky, period)
95
+ name =
96
+ case sky
97
+ when 'Clear'
98
+ (period == "night") ? moon : sun
99
+
100
+ when 'Clouds'
101
+ clouds
102
+
103
+ when 'Rain'
104
+ rain
105
+
106
+ when 'Fog'
107
+ fog
108
+
109
+ when 'Snow'
110
+ snow
111
+
112
+ when 'Thunderstorm'
113
+ thunderstorm
114
+
115
+ else
116
+ (period == "night") ? moon : sun
117
+ end
118
+ end
119
+
120
+ def current_period
121
+ current_time = Time.now.to_i
122
+
123
+ if current_time >= sunset or current_time <= sunrise
124
+ 'night'
125
+ else
126
+ 'day'
127
+ end
128
+ end
129
+
130
+ def temperature_scale units
131
+ (units == 'metric') ? "°C" : "°F"
132
+ end
133
+
134
+ def speed_unit units
135
+ (units == 'metric') ? "m/s" : "mph"
136
+ end
137
+
138
+ def pressure_unit units
139
+ (units == 'metric') ? "hPa" : "inHg"
140
+ end
141
+
142
+ def pressure pressure, units
143
+ (units == 'metric') ? sprintf('%.0f', pressure) : sprintf('%.2f', pressure*0.0295)
144
+ end
145
+ end
146
+
147
+
data/meteo.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/lib/meteo/version')
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "meteo"
7
+ spec.summary = %q{Provides command line access to OpenWeatherMap service.}
8
+ spec.description = %q{Provides command line access to OpenWeatherMap service.}
9
+ spec.email = "alexander.shvets@gmail.com"
10
+ spec.authors = ["Alexander Shvets"]
11
+ spec.homepage = "http://github.com/shvets/meteo"
12
+ spec.executables = ["meteo"]
13
+
14
+ spec.files = `git ls-files`.split($\)
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+ spec.version = Meteo::VERSION
18
+ spec.license = "MIT"
19
+
20
+
21
+ spec.add_runtime_dependency "thor", [">= 0"]
22
+ spec.add_development_dependency "gemspec_deps_gen", [">= 0"]
23
+ spec.add_development_dependency "gemcutter", [">= 0"]
24
+
25
+ end
26
+
data/spec/geo_spec.rb ADDED
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ require 'meteo/geo'
4
+
5
+ describe Geo do
6
+ describe "#quote" do
7
+ it "gets the quote" do
8
+ result = subject.quote("apple.com")
9
+
10
+ expect(result['city']).to eq("Cupertino")
11
+ expect(result['country_code']).to eq("US")
12
+ end
13
+ end
14
+
15
+ end
data/spec/meteo_spec.rb CHANGED
@@ -1,15 +1,11 @@
1
- # meteo_spec.rb
2
-
3
1
  require File.dirname(__FILE__) + '/spec_helper'
4
2
 
5
- require 'meteo'
6
- require 'json'
7
- #require 'awesome_print'
3
+ require 'meteo/meteo'
8
4
 
9
5
  describe Meteo do
10
6
  describe "#quote" do
11
7
  it "gets the quote" do
12
- result = JSON.parse(subject.quote("plainsboro, nj", "imperial"))
8
+ result = subject.quote("plainsboro, nj", "imperial")
13
9
 
14
10
  expect(result['sys']['country']).to eq("United States of America")
15
11
  end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,3 @@
1
1
  $: << File.expand_path('../lib', File.dirname(__FILE__))
2
+
3
+ require 'awesome_print'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ require 'meteo/weather_reporter'
4
+
5
+ describe WeatherReporter do
6
+ it 'has expected meteo status instance methods' do
7
+ %w[fog rain moon sun clouds thunderstorm].each do |status|
8
+ subject.methods.should include(status.to_sym)
9
+ end
10
+ end
11
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meteo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Shvets
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-06 00:00:00.000000000 Z
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -60,6 +60,15 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - .gitignore
63
+ - .idea/.name
64
+ - .idea/.rakeTasks
65
+ - .idea/encodings.xml
66
+ - .idea/meteo.iml
67
+ - .idea/misc.xml
68
+ - .idea/modules.xml
69
+ - .idea/scopes/scope_settings.xml
70
+ - .idea/vcs.xml
71
+ - .idea/workspace.xml
63
72
  - .ruby-gemset
64
73
  - .ruby-version
65
74
  - CHANGES
@@ -70,13 +79,18 @@ files:
70
79
  - bin/meteo
71
80
  - bin/meteo.bat
72
81
  - lib/meteo.rb
82
+ - lib/meteo/colors.rb
83
+ - lib/meteo/geo.rb
73
84
  - lib/meteo/meteo.rb
74
85
  - lib/meteo/meteo_cli.rb
75
- - lib/meteo/reporter.rb
76
86
  - lib/meteo/version.rb
87
+ - lib/meteo/weather_reporter.rb
88
+ - meteo.gemspec
77
89
  - meteo.gemspec.erb
90
+ - spec/geo_spec.rb
78
91
  - spec/meteo_spec.rb
79
92
  - spec/spec_helper.rb
93
+ - spec/weather_reporter_spec.rb
80
94
  homepage: http://github.com/shvets/meteo
81
95
  licenses:
82
96
  - MIT
@@ -102,5 +116,7 @@ signing_key:
102
116
  specification_version: 4
103
117
  summary: Provides command line access to OpenWeatherMap service.
104
118
  test_files:
119
+ - spec/geo_spec.rb
105
120
  - spec/meteo_spec.rb
106
121
  - spec/spec_helper.rb
122
+ - spec/weather_reporter_spec.rb