Skip to content

Commit bd10623

Browse files
committed
Merge pull request #3 from 42races/current_weather
get current weather info by city, city id or latitude and longitude
2 parents f6696ae + d6e2749 commit bd10623

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

lib/open_weather_api/base.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
require 'addressable/uri'
33
module OpenWeather
44
class Base
5-
def self.send_request hash, url
6-
uri = Addressable::URI.parse(url)
7-
uri.query_values = hash
5+
def self.send_request(request)
6+
uri = Addressable::URI.parse(request.url)
7+
uri.query_values = request.options
88
HTTParty.get(uri.to_s)
99
end
1010
end
11-
end
11+
end

lib/open_weather_api/current_weather.rb

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
module OpenWeather
22
class Current < Base
3+
attr_reader :url, :options, :weather_info, :status, :message
34

4-
@api_url = 'http://api.openweathermap.org/data/2.5/weather'
5-
6-
#format : Cochin,IN
7-
def self.city(city, options = { })
8-
query = Hash.new
9-
query[:q] = city
10-
send_request query.merge!(options), @api_url
5+
def initialize(options = {})
6+
@status = false
7+
@url = "http://api.openweathermap.org/data/2.5/weather"
8+
@options = extract_options!(options)
9+
end
10+
11+
def retrive
12+
@response = Base.public_send(:send_request, self) unless @options.empty?
13+
parse_response if @response
14+
end
15+
16+
def success?
17+
@status == 200
18+
end
19+
20+
private
21+
22+
def extract_options!(options)
23+
valid_options = [:lat, :lon, :city, :country, :city_id]
24+
options.keys.each { |k| options.delete(k) unless valid_options.include?(k) }
25+
if options[:city] || options[:country]
26+
options[:q] = "#{options[:country]},#{options[:city]}"
27+
options.delete(:city)
28+
options.delete(:country)
29+
end
30+
31+
if options[:city_id]
32+
options[:id] = options[:city_id]
33+
options.delete(:city_id)
34+
end
35+
options
36+
end
37+
38+
def parse_response
39+
@weather_info = @response.to_h
40+
@status = @weather_info["cod"]
41+
@message = @weather_info["message"] unless @status
42+
@weather_info
1143
end
1244
end
1345
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require "spec_helper"
2+
3+
describe "Current Weather Info" do
4+
context "Blank request" do
5+
before { @api = OpenWeather::Current.new }
6+
7+
it "has the version 2.5 api url" do
8+
@api.url.should eq("http://api.openweathermap.org/data/2.5/weather")
9+
end
10+
11+
it "wont't send request if the options are empty" do
12+
@api.retrive.should be_nil
13+
end
14+
end
15+
16+
context "Request with proper options" do
17+
before { @api = OpenWeather::Current.new(city: "Kochi", country: "India") }
18+
19+
it "retirves the weather info" do
20+
@api.retrive
21+
@api.success?.should be_true
22+
end
23+
end
24+
25+
context "Request with wrong options" do
26+
before { @api = OpenWeather::Current.new(city: "Kochiiiiii", country: "India") }
27+
28+
it "retirves the weather info" do
29+
@api.retrive
30+
@api.success?.should be_false
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)