Skip to content

Commit a41d34a

Browse files
committed
Weather Forecast
1 parent b3ef4e3 commit a41d34a

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

lib/open_weather_api/api.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
require_relative 'current_weather'
21

32
module OpenWeather
4-
class Current
3+
module ClassMethods
54
#City format : Eg, Cochin,IN
65
#Useage: OpenWeather::Current.city('Cochin,In')
7-
def self.city(city, options = {})
6+
def city(city, options = {})
87
new(options.merge(city: city)).retrive
98
end
109

1110
#City Id, an integer value. Eg, 2172797
1211
#Useage: OpenWeather::Current.city_id(2172797)
13-
def self.city_id(id, options = {})
12+
def city_id(id, options = {})
1413
new(options.merge(id: id)).retrive
1514
end
1615

1716
#City Geographics Cordingates : Eg, lat 35 lon 139
18-
def self.geocode(lat, lon, options = {})
17+
def geocode(lat, lon, options = {})
1918
new(options.merge(lat: lat, lon: lon)).retrive
2019
end
21-
end
20+
end
21+
22+
class Base
23+
extend ClassMethods
24+
end
2225
end

lib/open_weather_api/forecast.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

spec/ruby_weather_api/api_spec.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# failure
2929
# {"message"=>"Error: Not found city", "cod"=>"404"}
3030

31-
describe 'Open weather API' do
31+
describe 'Open weather Current API' do
3232
context '.city' do
3333
it 'return current weather for cochi' do
3434
response = OpenWeather::Current.city('Cochin, In')
@@ -64,4 +64,42 @@
6464
response['cod'].should eq("404")
6565
end
6666
end
67+
end
68+
69+
describe 'Open weather Forecast API' do
70+
context '.city' do
71+
it 'return forecast weather for cochi' do
72+
response = OpenWeather::Forecast.city('Cochin, In')
73+
response['cod'].to_s.should eq("200")
74+
end
75+
76+
it 'returns error if the city is invalid' do
77+
response = OpenWeather::Forecast.city('Cochiiiiiin, In')
78+
response['cod'].to_s.should eq("404")
79+
end
80+
end
81+
82+
context '.city_id' do
83+
it 'return forecast weather for city id of cochi' do
84+
response = OpenWeather::Forecast.city_id('1273874')
85+
response['cod'].to_s.should eq("200")
86+
end
87+
88+
it 'returns error if the city id is invalid' do
89+
response = OpenWeather::Forecast.city('invalidid')
90+
response['cod'].to_s.should eq("404")
91+
end
92+
end
93+
94+
context '.geocode' do
95+
it 'return forecast weather for geocode of cochi' do
96+
response = OpenWeather::Forecast.geocode('9.94', '76.26')
97+
response['cod'].to_s.should eq("200")
98+
end
99+
100+
it 'returns error if the geocode is invalid' do
101+
response = OpenWeather::Forecast.geocode('181', '181')
102+
response['cod'].to_s.should eq("404")
103+
end
104+
end
67105
end

0 commit comments

Comments
 (0)