weather_by_ip 0.2.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- weather_by_ip (0.2.0)
5
- api_object (>= 0.5.1)
4
+ weather_by_ip (0.5.1)
5
+ api_object (>= 0.7)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -10,11 +10,17 @@ GEM
10
10
  activesupport (3.2.6)
11
11
  i18n (~> 0.6)
12
12
  multi_json (~> 1.0)
13
- api_object (0.5.1)
13
+ api_object (0.7.1)
14
14
  activesupport
15
+ freegeoip
15
16
  geo_ip
16
17
  nori (>= 1.1)
17
18
  rest-client (>= 1.6)
19
+ faraday (0.8.0.rc2)
20
+ multipart-post (~> 1.1)
21
+ freegeoip (0.1.0)
22
+ faraday (= 0.8.0.rc2)
23
+ multi_json (= 1.2.0)
18
24
  geo_ip (0.5.0)
19
25
  json (~> 1.4)
20
26
  rest-client (~> 1.6.1)
@@ -22,7 +28,8 @@ GEM
22
28
  json (1.7.3)
23
29
  mime-types (1.18)
24
30
  minitest (3.1.0)
25
- multi_json (1.3.6)
31
+ multi_json (1.2.0)
32
+ multipart-post (1.1.5)
26
33
  nori (1.1.0)
27
34
  rest-client (1.6.7)
28
35
  mime-types (>= 1.16)
data/README.md CHANGED
@@ -26,15 +26,30 @@ Consider making a donation to [ipinfodb.com](http://ipinfodb.com/) at [http://ip
26
26
 
27
27
  The gem also uses Google weather service to the weather information by location. It doesn't require any key.
28
28
 
29
+ It's also has functionality to get weather information by city name or zip code. In this case, registration with ipinfodb.com is not needed.
30
+
29
31
  The weather information then is obtained as the following:
30
32
 
31
33
  ```
32
- WeatherByIp.get_weather(<your ip>, <your ipinfodb_key>)
34
+ WeatherByIp.get_weather('<your ip>', '<your ipinfodb_key>')
35
+
36
+ or
37
+
38
+ GeoIp.api_key = <your key>
39
+ WeatherByIp.get_weather('<your ip>')
40
+
41
+ or
42
+
43
+ WeatherByIp.get_weather('<your city, state or zip code>')
44
+ WeatherByIp.get_weather('Santa Clara, CA')
45
+ WeatherByIp.get_weather('78744')
33
46
  ```
34
47
 
35
48
  ## Limitations
36
49
 
37
- THe location by ip service is free and doesn't always give the correct location.
50
+ * The location by ip service is free and doesn't always give the correct location.
51
+ * Retrieval by zip code has been tested on the US and Canadian zip codes. It's recommended to use city names if retrieval by zip code doesn't work.
52
+
38
53
 
39
54
  ## Testing
40
55
 
@@ -1,5 +1,6 @@
1
1
  require "weather_by_ip/version"
2
2
  require 'weather_by_ip/weather'
3
+ require 'uri'
3
4
 
4
5
  class WeatherByIp
5
6
 
@@ -7,8 +8,15 @@ class WeatherByIp
7
8
 
8
9
  include WeatherInfo
9
10
 
10
- def get_weather ip, key = GeoIp.api_key
11
- Weather.new(Weather.get_results_by_ip(ip, :key => key, :weather => :zip_code))
11
+ def get_weather location, key = GeoIp.api_key
12
+ zipcode_opt = key.nil? ? :zipcode : :zip_code #difference between freegeoip and ipinfodb services
13
+ results = (location_ip? location) ? Weather.get_results_by_ip(location, :key => key, :weather => zipcode_opt) : Weather.get_results(:weather => URI.escape(location))
14
+ Weather.new(results)
15
+ end
16
+
17
+ private
18
+ def location_ip? loc
19
+ loc =~ /(?:\d{1,3}\.){3}\d{1,3}/
12
20
  end
13
21
 
14
22
  end
@@ -1,3 +1,3 @@
1
1
  module WeatherInfo
2
- VERSION = "0.2.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -2,6 +2,7 @@ require 'api_object'
2
2
  module WeatherInfo
3
3
 
4
4
  URL = "http://www.google.com"
5
+ IMG_URL = "http://g0.gstatic.com"
5
6
 
6
7
  def self.included(base)
7
8
  base.send(:include, ActiveApi)
@@ -11,8 +12,8 @@ module WeatherInfo
11
12
  def to_s
12
13
  inspect
13
14
  end
14
- def get_icon_path icon
15
- "#{URL.chomp('/')}/#{icon}"
15
+ def get_icon_path
16
+ "#{IMG_URL.chomp('/')}/#{self.icon}" if self.icon
16
17
  end
17
18
  end
18
19
 
@@ -31,7 +32,7 @@ module WeatherInfo
31
32
  return false if other_info.nil?
32
33
  self.city == other_info.city
33
34
  end
34
-
35
+
35
36
  end
36
37
 
37
38
  class CurrentWeather < ActiveApi::ApiObject
@@ -44,6 +45,7 @@ module WeatherInfo
44
45
  def inspect
45
46
  "Today #{@temp_f}F (#{@temp_c}C), #{@sky}, #{@humidity}, #{@wind}"
46
47
  end
48
+
47
49
  end
48
50
 
49
51
  class WeatherForecast < ActiveApi::ApiObject
@@ -5,10 +5,9 @@ require 'weather_by_ip/weather'
5
5
  class WeatherByIpTest < MiniTest::Unit::TestCase
6
6
 
7
7
  IP = '99.156.82.20'
8
-
9
- def test_should_get_correct_weather
8
+
9
+ def test_should_get_correct_weather_by_ip
10
10
  check_weather WeatherByIp.get_weather(IP, ARGV.last)
11
-
12
11
  end
13
12
 
14
13
  def test_should_get_correct_weather_with_key_preset
@@ -16,13 +15,31 @@ class WeatherByIpTest < MiniTest::Unit::TestCase
16
15
  check_weather WeatherByIp.get_weather(IP)
17
16
  end
18
17
 
18
+ def test_should_get_correct_weather_by_location
19
+ check_weather WeatherByIp.get_weather('78744')
20
+ check_weather WeatherByIp.get_weather('San Jose, CA'), 'San Jose, CA'
21
+ end
22
+
23
+ def test_should_get_correct_weather_with_key_missing
24
+ GeoIp.api_key = nil
25
+ weather = WeatherByIp.get_weather(IP)
26
+ check_weather WeatherByIp.get_weather(IP), "Round Rock, TX"
27
+ #assert_empty(weather, "weather object should be empty")
28
+ end
29
+
30
+ def test_should_get_empty_weather_with_incorrect_location
31
+ weather = WeatherByIp.get_weather('999999')
32
+ assert_empty(weather, "weather object should be empty")
33
+ end
34
+
35
+
19
36
  private
20
- def check_weather weather
37
+ def check_weather weather, city = 'Austin, TX'
21
38
  unless weather.nil?
22
39
  refute_nil(weather.forecast_information)
23
40
  refute_nil(weather.current_conditions)
24
41
  refute_nil(weather.forecast_conditions)
25
- assert_equal(weather.forecast_information.city, "Austin, TX")
42
+ assert_equal(weather.forecast_information.city, city)
26
43
  assert_equal(weather.forecast_information.current_date, Date.today.to_s)
27
44
  assert_match(/^\/ig\/images\/weather\/.+\.gif/, weather.current_conditions.icon)
28
45
  assert(weather.forecast_conditions.size > 3)
@@ -16,6 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.version = WeatherInfo::VERSION
17
17
 
18
18
  gem.add_development_dependency "minitest"
19
- gem.add_dependency('api_object', '>= 0.5.1')
19
+ gem.add_dependency('api_object', '>= 0.7')
20
20
 
21
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weather_by_ip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-13 00:00:00.000000000 Z
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &73543910 !ruby/object:Gem::Requirement
16
+ requirement: &85402070 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,18 +21,18 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *73543910
24
+ version_requirements: *85402070
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: api_object
27
- requirement: &73543430 !ruby/object:Gem::Requirement
27
+ requirement: &85400790 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
- version: 0.5.1
32
+ version: '0.7'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *73543430
35
+ version_requirements: *85400790
36
36
  description: An interface to load weather information based on an approximate user
37
37
  location
38
38
  email: