weather_by_ip 0.1.0
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.
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +16 -0
- data/lib/weather_by_ip.rb +18 -0
- data/lib/weather_by_ip/version.rb +3 -0
- data/lib/weather_by_ip/weather.rb +86 -0
- data/test/unit/weather_by_ip_test.rb +24 -0
- data/weather_by_ip.gemspec +21 -0
- metadata +79 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
weather_by_ip (0.0.1)
|
5
|
+
api_object (>= 0.3)
|
6
|
+
geo_ip
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (3.2.6)
|
12
|
+
i18n (~> 0.6)
|
13
|
+
multi_json (~> 1.0)
|
14
|
+
api_object (0.3.3)
|
15
|
+
activesupport
|
16
|
+
nori (>= 1.1)
|
17
|
+
rest-client (>= 1.6)
|
18
|
+
geo_ip (0.5.0)
|
19
|
+
json (~> 1.4)
|
20
|
+
rest-client (~> 1.6.1)
|
21
|
+
i18n (0.6.0)
|
22
|
+
json (1.7.3)
|
23
|
+
mime-types (1.18)
|
24
|
+
minitest (3.0.1)
|
25
|
+
multi_json (1.3.6)
|
26
|
+
nori (1.1.0)
|
27
|
+
rest-client (1.6.7)
|
28
|
+
mime-types (>= 1.16)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
minitest
|
35
|
+
weather_by_ip!
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 tmoskun
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# WeatherByIp
|
2
|
+
|
3
|
+
This gem captures weather information based on ip.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'weather_by_ip'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install weather_by_ip
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The gem relies on [ipinfodb.com](http://ipinfodb.com/) webservice to retrieve location based on ip.
|
22
|
+
|
23
|
+
The service requires an API key, in order to get it [register](http://ipinfodb.com/register.php) at the web site.
|
24
|
+
|
25
|
+
Consider making a donation to [ipinfodb.com](http://ipinfodb.com/) at [http://ipinfodb.com/donate.php](http://ipinfodb.com/donate.php).
|
26
|
+
|
27
|
+
The gem also uses Google weather service to the weather information by location. It doesn't require any key.
|
28
|
+
|
29
|
+
The weather information then is obtained as the following:
|
30
|
+
|
31
|
+
```
|
32
|
+
WeatherByIp.get_weather(<your ip>, <your ipinfodb_key>)
|
33
|
+
```
|
34
|
+
|
35
|
+
## Limitations
|
36
|
+
|
37
|
+
THe location by ip service is free and doesn't always give the correct location.
|
38
|
+
|
39
|
+
## Testing
|
40
|
+
|
41
|
+
For the test to be run, the ipinfodb.com api key should be either placed into the test/data/keys/ipinfodb_key.txt file or passed as an environment variable:
|
42
|
+
|
43
|
+
```
|
44
|
+
API_KEY='<your key>' rake test
|
45
|
+
```
|
46
|
+
|
47
|
+
There is no existing api key provided with this gem as per the Terms and Conditions of the ipinfodb service.
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
desc 'Run tests'
|
5
|
+
task :test do
|
6
|
+
result = system("ruby -Ilib -Itest -e 'ARGV.take(ARGV.size-1).each { |f| load f }' test/unit/* '#{File.read('test/data/keys/ipinfodb_key.txt') rescue ENV['API_KEY']}'")
|
7
|
+
exit(result ? 0 : 1)
|
8
|
+
end
|
9
|
+
|
10
|
+
task :ci => [:test]
|
11
|
+
task :default => [:test]
|
12
|
+
|
13
|
+
namespace :pre_commit do
|
14
|
+
desc "run the tests"
|
15
|
+
task :ci => [:test]
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "weather_by_ip/version"
|
2
|
+
require 'weather_by_ip/weather'
|
3
|
+
|
4
|
+
class WeatherByIp
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
include WeatherInfo
|
9
|
+
|
10
|
+
def get_weather ip, key
|
11
|
+
Weather.new(Weather.get_results_by_ip(ip, key, :weather => :zip_code))
|
12
|
+
rescue
|
13
|
+
puts "Cannot obtain location or weather. Verify that you've obtained and set the api_key for the ipinfodb.com webservice"
|
14
|
+
return
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'api_object'
|
2
|
+
module WeatherInfo
|
3
|
+
|
4
|
+
URL = "http://www.google.com"
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.send(:include, ActiveApi)
|
8
|
+
end
|
9
|
+
|
10
|
+
module WeatherMethods
|
11
|
+
def to_s
|
12
|
+
inspect
|
13
|
+
end
|
14
|
+
def get_icon_path icon
|
15
|
+
"#{URL.chomp('/')}/#{icon}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class ForecastInfo < ActiveApi::ApiObject
|
20
|
+
include WeatherMethods
|
21
|
+
attr_reader :city, :postal_code, :current_date, :current_time
|
22
|
+
|
23
|
+
api_column :current_date, :forecast_date
|
24
|
+
api_column :current_time, :current_date_time
|
25
|
+
|
26
|
+
def inspect
|
27
|
+
"#{@city} #{@current_time}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def ==(other_info)
|
31
|
+
return false if other_info.nil?
|
32
|
+
self.city == other_info.city
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
class CurrentWeather < ActiveApi::ApiObject
|
38
|
+
include WeatherMethods
|
39
|
+
attr_reader :sky, :temp_f, :temp_c, :humidity, :wind, :icon
|
40
|
+
|
41
|
+
api_column :sky, :condition
|
42
|
+
api_column :wind, :wind_condition
|
43
|
+
|
44
|
+
def inspect
|
45
|
+
"Today #{@temp_f}F (#{@temp_c}C), #{@sky}, #{@humidity}, #{@wind}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class WeatherForecast < ActiveApi::ApiObject
|
50
|
+
include WeatherMethods
|
51
|
+
attr_reader :day_of_week, :sky, :temp_low, :temp_high, :icon
|
52
|
+
|
53
|
+
api_column :temp_low, :low
|
54
|
+
api_column :temp_high, :high
|
55
|
+
api_column :sky, :condition
|
56
|
+
|
57
|
+
def inspect
|
58
|
+
"#{@day_of_week} low #{@temp_low}, high #{@temp_high}, #{@sky}"
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
class Weather < ActiveApi::ApiObject
|
65
|
+
include WeatherMethods
|
66
|
+
attr_reader :forecast_information, :current_conditions, :forecast_conditions
|
67
|
+
|
68
|
+
initialize_from_api :url => "#{URL}/ig/", :action => 'api', :data_tags => :data
|
69
|
+
|
70
|
+
api_association :forecast_information, :as => ForecastInfo
|
71
|
+
api_association :current_conditions, :as => CurrentWeather
|
72
|
+
api_association :forecast_conditions, :as => WeatherForecast
|
73
|
+
|
74
|
+
|
75
|
+
def inspect
|
76
|
+
"#{@forecast_information} \n\t#{@current_conditions} \n\t#{@forecast_conditions.instance_of?(Array) ? (@forecast_conditions.inject('') {|str, e| (str + e.inspect + "\n\t")}) : @forecast_conditions}"
|
77
|
+
end
|
78
|
+
|
79
|
+
def ==(other_weather)
|
80
|
+
return false if other_weather.nil?
|
81
|
+
self.forecast_information == other_weather.forecast_information && self.current_conditions != nil && other_weather.current_conditions != nil && self.forecast_conditions.size == other_weather.forecast_conditions.size
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'weather_by_ip'
|
3
|
+
require 'weather_by_ip/weather'
|
4
|
+
|
5
|
+
class WeatherByIpTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
IP = '99.156.82.20'
|
8
|
+
|
9
|
+
def test_should_get_correct_weather
|
10
|
+
weather = WeatherByIp.get_weather(IP, ARGV.last)
|
11
|
+
unless weather.nil?
|
12
|
+
refute_nil(weather.forecast_information)
|
13
|
+
refute_nil(weather.current_conditions)
|
14
|
+
refute_nil(weather.forecast_conditions)
|
15
|
+
assert_equal(weather.forecast_information.city, "Austin, TX")
|
16
|
+
assert_equal(weather.forecast_information.current_date, Date.today.to_s)
|
17
|
+
assert_match(/^\/ig\/images\/weather\/.+\.gif/, weather.current_conditions.icon)
|
18
|
+
assert(weather.forecast_conditions.size > 3)
|
19
|
+
assert_equal(weather.forecast_conditions[1].day_of_week, Date.tomorrow.strftime("%a"))
|
20
|
+
assert_match(/^\/ig\/images\/weather\/.+\.gif/, weather.forecast_conditions[1].icon)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/weather_by_ip/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["tmoskun"]
|
6
|
+
gem.email = ["tanyamoskun@gmail.com"]
|
7
|
+
gem.description = %q{An interface to load weather information based on an approximate user location}
|
8
|
+
gem.summary = %q{An interface to load weather information based on an approximate user location}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "weather_by_ip"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = WeatherInfo::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "minitest"
|
19
|
+
gem.add_dependency('api_object', '>= 0.4')
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weather_by_ip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- tmoskun
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: &72318530 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *72318530
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: api_object
|
27
|
+
requirement: &72318140 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0.4'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *72318140
|
36
|
+
description: An interface to load weather information based on an approximate user
|
37
|
+
location
|
38
|
+
email:
|
39
|
+
- tanyamoskun@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- Gemfile
|
45
|
+
- Gemfile.lock
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- lib/weather_by_ip.rb
|
50
|
+
- lib/weather_by_ip/version.rb
|
51
|
+
- lib/weather_by_ip/weather.rb
|
52
|
+
- test/unit/weather_by_ip_test.rb
|
53
|
+
- weather_by_ip.gemspec
|
54
|
+
homepage: ''
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.17
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: An interface to load weather information based on an approximate user location
|
78
|
+
test_files:
|
79
|
+
- test/unit/weather_by_ip_test.rb
|