open-weather 0.9

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.db
2
+ *~
3
+ *#
4
+ *.DS_Store
5
+ log/*
6
+ tmp/*
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'httparty'
4
+ gem 'addressable'
5
+ gem 'rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.4)
5
+ coderay (1.0.9)
6
+ diff-lcs (1.2.2)
7
+ httparty (0.11.0)
8
+ multi_json (~> 1.0)
9
+ multi_xml (>= 0.5.2)
10
+ method_source (0.8.2)
11
+ multi_json (1.7.3)
12
+ multi_xml (0.5.3)
13
+ pry (0.9.12.2)
14
+ coderay (~> 1.0.5)
15
+ method_source (~> 0.8)
16
+ slop (~> 3.4)
17
+ rspec (2.13.0)
18
+ rspec-core (~> 2.13.0)
19
+ rspec-expectations (~> 2.13.0)
20
+ rspec-mocks (~> 2.13.0)
21
+ rspec-core (2.13.1)
22
+ rspec-expectations (2.13.0)
23
+ diff-lcs (>= 1.1.3, < 2.0)
24
+ rspec-mocks (2.13.0)
25
+ slop (3.4.6)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ addressable
32
+ httparty
33
+ pry
34
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 HsPS <mailme@hsps.in>, Deepak Kumar<deepakkumarnd@gmail.com>
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,45 @@
1
+ ## A ruby wrapper for Open Weather Map API.
2
+
3
+ ## Installation
4
+
5
+ Add the following to your **Gemfile**
6
+
7
+ gem 'open_weather'
8
+
9
+ or
10
+
11
+ $ gem install open_weather
12
+
13
+ ## Usage
14
+
15
+ require 'open_weather'
16
+
17
+ # current weather APIs
18
+
19
+ # get current weather by city name
20
+ OpenWeather::Current.city("Cochin, IN")
21
+
22
+ # get current weather by city id
23
+ OpenWeather::Current.city_id("1273874")
24
+
25
+ # get current weather by geocode. (lat, lon)
26
+ OpenWeather::Current.geocode(9.94, 76.26)
27
+
28
+ # weather forecast APIs
29
+
30
+ # get weather forecast by city name
31
+ OpenWeather::Forecast.city("Cochin, IN")
32
+
33
+ # get weather forecast by city id
34
+ OpenWeather::Forecast.city_id("1273874")
35
+
36
+ # get weather forecast by geocode. (lat, lon)
37
+ OpenWeather::Forecast.geocode(9.94, 76.26)
38
+
39
+ ## Contributing
40
+
41
+ Fork it
42
+ Create your feature branch (git checkout -b my-new-feature)
43
+ Commit your changes (git commit -am 'Added some feature')
44
+ Push to the branch (git push origin my-new-feature)
45
+ Create new Pull Request
@@ -0,0 +1,10 @@
1
+ module OpenWeather
2
+ $LOAD_PATH<< "../lib"
3
+
4
+ autoload :Base, "open_weather/base"
5
+ autoload :Current, "open_weather/current"
6
+ autoload :Forecast, "open_weather/forecast"
7
+ autoload :VERSION, "open_weather/version"
8
+
9
+ require "open_weather/api.rb"
10
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module OpenWeather
3
+ module ClassMethods
4
+ #City format : Eg, Cochin,IN
5
+ #Useage: OpenWeather::Current.city('Cochin,In')
6
+ def city(city, options = {})
7
+ new(options.merge(city: city)).retrive
8
+ end
9
+
10
+ #City Id, an integer value. Eg, 2172797
11
+ #Useage: OpenWeather::Current.city_id(2172797)
12
+ def city_id(id, options = {})
13
+ new(options.merge(id: id)).retrive
14
+ end
15
+
16
+ #City Geographics Cordingates : Eg, lat 35 lon 139
17
+ def geocode(lat, lon, options = {})
18
+ new(options.merge(lat: lat, lon: lon)).retrive
19
+ end
20
+ end
21
+
22
+ class Base
23
+ extend ClassMethods
24
+ end
25
+ end
@@ -0,0 +1,52 @@
1
+ require 'httparty'
2
+ require 'addressable/uri'
3
+
4
+ module OpenWeather
5
+ class Base
6
+
7
+ attr_reader :url, :options, :weather_info, :status, :message
8
+
9
+ def initialize url, options
10
+ @status = false
11
+ @url = url
12
+ @options = extract_options!(options)
13
+ end
14
+
15
+ def retrive
16
+ @response = send_request(self) unless @options.empty?
17
+ parse_response if @response
18
+ end
19
+
20
+ def success?
21
+ @status == 200
22
+ end
23
+
24
+ private
25
+
26
+ def extract_options!(options)
27
+ valid_options = [:lat, :lon, :city, :country, :id]
28
+ options.keys.each { |k| options.delete(k) unless valid_options.include?(k) }
29
+
30
+ if options[:city] || options[:country]
31
+ options[:q] = "#{options[:country]},#{options[:city]}"
32
+ options.delete(:city)
33
+ options.delete(:country)
34
+ end
35
+
36
+ options
37
+ end
38
+
39
+ def parse_response
40
+ @weather_info = @response
41
+ @status = @weather_info["cod"]
42
+ @message = @weather_info["message"] unless @status
43
+ @weather_info
44
+ end
45
+
46
+ def send_request(request)
47
+ uri = Addressable::URI.parse(request.url)
48
+ uri.query_values = request.options
49
+ HTTParty.get(uri.to_s)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ module OpenWeather
2
+ class Current < Base
3
+ def initialize(options = {})
4
+ super("http://api.openweathermap.org/data/2.5/weather", options)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module OpenWeather
2
+ class Forecast < Base
3
+ def initialize options = {}
4
+ super('http://api.openweathermap.org/data/2.5/forecast', options)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module OpenWeather
2
+ VERSION = "0.9"
3
+ end
@@ -0,0 +1,17 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'open_weather/version'
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "open-weather"
6
+ gem.version = OpenWeather::VERSION
7
+ gem.authors = ["HsPS <mailme@hsps.in>", "Deepak <deepakkumarnd@gmail.com>"]
8
+ gem.email = ['mailme@hsps.in']
9
+ gem.homepage = "https://github.com/coderhs/ruby_open_weather_map"
10
+ gem.summary = %q{ A ruby wrapper for Open Weather Map API. }
11
+ gem.description = %q{ A ruby wrapper for Open Weather Map API. }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = gem.files.grep(/^(spec|test|features)/)
14
+ gem.executables = gem.files.grep(/^bin/).map{ |f| File.basename(f) }
15
+ gem.require_paths = ["lib"]
16
+ gem.add_development_dependency "rspec"
17
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ # success response
4
+ #
5
+ # {"coord"=>{"lon"=>76.26, "lat"=>9.94},
6
+ # "sys"=>
7
+ # {"message"=>0.1938,
8
+ # "country"=>"India",
9
+ # "sunrise"=>1390007802,
10
+ # "sunset"=>1390049651},
11
+ # "weather"=>
12
+ # [{"id"=>800, "main"=>"Clear", "description"=>"Sky is Clear", "icon"=>"01d"}],
13
+ # "base"=>"gdps stations",
14
+ # "main"=>
15
+ # {"temp"=>303.534,
16
+ # "temp_min"=>303.534,
17
+ # "temp_max"=>303.534,
18
+ # "pressure"=>1022.72,
19
+ # "sea_level"=>1026.59,
20
+ # "grnd_level"=>1022.72,
21
+ # "humidity"=>72},
22
+ # "wind"=>{"speed"=>1.21, "deg"=>143.5},
23
+ # "clouds"=>{"all"=>0},
24
+ # "dt"=>1390029526,
25
+ # "id"=>1273874,
26
+ # "name"=>"Kochi",
27
+ # "cod"=>200
28
+ # }
29
+
30
+ # failure response
31
+ #
32
+ # {"message"=>"Error: Not found city", "cod"=>"404"}
33
+
34
+ describe 'Connection Error' do
35
+ it 'return error when the network is unreachable' do
36
+ response = OpenWeather::Current.city('Cochin, In')
37
+ response['cod'].should eq(200)
38
+ end
39
+ end
40
+
41
+
42
+ describe 'Open weather Current API' do
43
+ context '.city' do
44
+ it 'return current weather for cochi' do
45
+ response = OpenWeather::Current.city('Cochin, In')
46
+ response['cod'].should eq(200)
47
+ end
48
+
49
+ it 'returns error if the city is invalid' do
50
+ response = OpenWeather::Current.city('Cochiiiiiin, In')
51
+ response['cod'].should eq("404")
52
+ end
53
+ end
54
+
55
+ context '.city_id' do
56
+ it 'return current weather for city id of cochi' do
57
+ response = OpenWeather::Current.city_id('1273874')
58
+ response['cod'].should eq(200)
59
+ end
60
+
61
+ it 'returns error if the city id is invalid' do
62
+ response = OpenWeather::Current.city('invalidid')
63
+ response['cod'].should eq("404")
64
+ end
65
+ end
66
+
67
+ context '.geocode' do
68
+ it 'return current weather for geocode of cochi' do
69
+ response = OpenWeather::Current.geocode('9.94', '76.26')
70
+ response['cod'].should eq(200)
71
+ end
72
+
73
+ it 'returns error if the geocode is invalid' do
74
+ response = OpenWeather::Current.geocode('181', '181')
75
+ response['cod'].should eq("404")
76
+ end
77
+ end
78
+ end
79
+
80
+ describe 'Open weather Forecast API' do
81
+ context '.city' do
82
+ it 'return forecast weather for cochi' do
83
+ response = OpenWeather::Forecast.city('Cochin, In')
84
+ response['cod'].to_s.should eq("200")
85
+ end
86
+
87
+ it 'returns error if the city is invalid' do
88
+ response = OpenWeather::Forecast.city('Cochiiiiiin, In')
89
+ response['cod'].to_s.should eq("404")
90
+ end
91
+ end
92
+
93
+ context '.city_id' do
94
+ it 'return forecast weather for city id of cochi' do
95
+ response = OpenWeather::Forecast.city_id('1273874')
96
+ response['cod'].to_s.should eq("200")
97
+ end
98
+
99
+ it 'returns error if the city id is invalid' do
100
+ response = OpenWeather::Forecast.city('invalidid')
101
+ response['cod'].to_s.should eq("404")
102
+ end
103
+ end
104
+
105
+ context '.geocode' do
106
+ it 'return forecast weather for geocode of cochi' do
107
+ response = OpenWeather::Forecast.geocode('9.94', '76.26')
108
+ response['cod'].to_s.should eq("200")
109
+ end
110
+
111
+ it 'returns error if the geocode is invalid' do
112
+ response = OpenWeather::Forecast.geocode('181', '181')
113
+ response['cod'].to_s.should eq("404")
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Version" do
4
+ it "should be version 0.9" do
5
+ OpenWeather::VERSION.should == "0.9"
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ # spec helper
2
+ require 'open_weather'
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open-weather
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.9'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - HsPS <mailme@hsps.in>
9
+ - Deepak <deepakkumarnd@gmail.com>
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-01-18 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ description: ! ' A ruby wrapper for Open Weather Map API. '
32
+ email:
33
+ - mailme@hsps.in
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE
42
+ - README.md
43
+ - lib/open_weather.rb
44
+ - lib/open_weather/api.rb
45
+ - lib/open_weather/base.rb
46
+ - lib/open_weather/current.rb
47
+ - lib/open_weather/forecast.rb
48
+ - lib/open_weather/version.rb
49
+ - open_weather.gemspec
50
+ - spec/open_weather/api_spec.rb
51
+ - spec/open_weather/version_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: https://github.com/coderhs/ruby_open_weather_map
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.23
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: A ruby wrapper for Open Weather Map API.
77
+ test_files:
78
+ - spec/open_weather/api_spec.rb
79
+ - spec/open_weather/version_spec.rb
80
+ - spec/spec_helper.rb
81
+ has_rdoc: