Skip to content

Commit 5688869

Browse files
committed
Merge pull request #4 from 42races/master
public apis added for Current weather
2 parents bd10623 + d846b2f commit 5688869

File tree

7 files changed

+108
-8
lines changed

7 files changed

+108
-8
lines changed

Gemfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ source 'http://rubygems.org'
22

33
gem 'httparty'
44
gem 'addressable'
5-
gem 'rspec'
5+
gem 'rspec'
6+
7+
8+
gem 'pry'

Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ GEM
22
remote: http://rubygems.org/
33
specs:
44
addressable (2.3.4)
5+
coderay (1.0.9)
56
diff-lcs (1.2.2)
67
httparty (0.11.0)
78
multi_json (~> 1.0)
89
multi_xml (>= 0.5.2)
10+
method_source (0.8.2)
911
multi_json (1.7.3)
1012
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)
1117
rspec (2.13.0)
1218
rspec-core (~> 2.13.0)
1319
rspec-expectations (~> 2.13.0)
@@ -16,11 +22,13 @@ GEM
1622
rspec-expectations (2.13.0)
1723
diff-lcs (>= 1.1.3, < 2.0)
1824
rspec-mocks (2.13.0)
25+
slop (3.4.6)
1926

2027
PLATFORMS
2128
ruby
2229

2330
DEPENDENCIES
2431
addressable
2532
httparty
33+
pry
2634
rspec

lib/open_weather_api.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ module OpenWeather
44
autoload :Base, "open_weather_api/base"
55
autoload :Current, "open_weather_api/current_weather"
66
autoload :VERSION, "open_weather_api/version"
7+
8+
require "open_weather_api/api.rb"
79
end

lib/open_weather_api/api.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require_relative 'current_weather'
2+
3+
module OpenWeather
4+
class Current
5+
#City format : Eg, Cochin,IN
6+
#Useage: OpenWeather::Current.city('Cochin,In')
7+
def self.city(city, options = {})
8+
new(options.merge(city: city)).retrive
9+
end
10+
11+
#City Id, an integer value. Eg, 2172797
12+
#Useage: OpenWeather::Current.city_id(2172797)
13+
def self.city_id(id, options = {})
14+
new(options.merge(id: id)).retrive
15+
end
16+
17+
#City Geographics Cordingates : Eg, lat 35 lon 139
18+
def self.geocode(lat, lon, options = {})
19+
new(options.merge(lat: lat, lon: lon)).retrive
20+
end
21+
end
22+
end

lib/open_weather_api/current_weather.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(options = {})
99
end
1010

1111
def retrive
12-
@response = Base.public_send(:send_request, self) unless @options.empty?
12+
@response = Base.send_request(self) unless @options.empty?
1313
parse_response if @response
1414
end
1515

@@ -20,23 +20,20 @@ def success?
2020
private
2121

2222
def extract_options!(options)
23-
valid_options = [:lat, :lon, :city, :country, :city_id]
23+
valid_options = [:lat, :lon, :city, :country, :id]
2424
options.keys.each { |k| options.delete(k) unless valid_options.include?(k) }
25+
2526
if options[:city] || options[:country]
2627
options[:q] = "#{options[:country]},#{options[:city]}"
2728
options.delete(:city)
2829
options.delete(:country)
2930
end
3031

31-
if options[:city_id]
32-
options[:id] = options[:city_id]
33-
options.delete(:city_id)
34-
end
3532
options
3633
end
3734

3835
def parse_response
39-
@weather_info = @response.to_h
36+
@weather_info = @response
4037
@status = @weather_info["cod"]
4138
@message = @weather_info["message"] unless @status
4239
@weather_info

spec/ruby_weather_api/api_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
3+
# success
4+
# response = {"coord"=>{"lon"=>76.26, "lat"=>9.94},
5+
# "sys"=>
6+
# {"message"=>0.1938,
7+
# "country"=>"India",
8+
# "sunrise"=>1390007802,
9+
# "sunset"=>1390049651},
10+
# "weather"=>
11+
# [{"id"=>800, "main"=>"Clear", "description"=>"Sky is Clear", "icon"=>"01d"}],
12+
# "base"=>"gdps stations",
13+
# "main"=>
14+
# {"temp"=>303.534,
15+
# "temp_min"=>303.534,
16+
# "temp_max"=>303.534,
17+
# "pressure"=>1022.72,
18+
# "sea_level"=>1026.59,
19+
# "grnd_level"=>1022.72,
20+
# "humidity"=>72},
21+
# "wind"=>{"speed"=>1.21, "deg"=>143.5},
22+
# "clouds"=>{"all"=>0},
23+
# "dt"=>1390029526,
24+
# "id"=>1273874,
25+
# "name"=>"Kochi",
26+
# "cod"=>200}
27+
28+
# failure
29+
# {"message"=>"Error: Not found city", "cod"=>"404"}
30+
31+
describe 'Open weather API' do
32+
context '.city' do
33+
it 'return current weather for cochi' do
34+
response = OpenWeather::Current.city('Cochin, In')
35+
response['cod'].should eq(200)
36+
end
37+
38+
it 'returns error if the city is invalid' do
39+
response = OpenWeather::Current.city('Cochiiiiiin, In')
40+
response['cod'].should eq("404")
41+
end
42+
end
43+
44+
context '.city_id' do
45+
it 'return current weather for city id of cochi' do
46+
response = OpenWeather::Current.city_id('1273874')
47+
response['cod'].should eq(200)
48+
end
49+
50+
it 'returns error if the city id is invalid' do
51+
response = OpenWeather::Current.city('invalidid')
52+
response['cod'].should eq("404")
53+
end
54+
end
55+
56+
context '.geocode' do
57+
it 'return current weather for geocode of cochi' do
58+
response = OpenWeather::Current.geocode('9.94', '76.26')
59+
response['cod'].should eq(200)
60+
end
61+
62+
it 'returns error if the geocode is invalid' do
63+
response = OpenWeather::Current.geocode('181', '181')
64+
response['cod'].should eq("404")
65+
end
66+
end
67+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# spec helper
22
require 'open_weather_api'
3+
require 'pry'

0 commit comments

Comments
 (0)