Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #21 #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GEM
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.0)
safe_yaml (1.0.3)
safe_yaml (1.0.4)
vcr (2.9.2)
webmock (1.18.0)
addressable (>= 2.3.6)
Expand Down
33 changes: 25 additions & 8 deletions lib/open_weather/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@ class Base
attr_reader :url, :options, :weather_info, :status, :message

def initialize(url, options)
@status = false
@url = url
@options = extract_options!(options)
@status = false
@url = url
@options = extract_options!(options)
@weather_info = { 'cod': nil, 'message': '' }
end

def retrive
response = send_request unless @options.empty?
parse_response(response)

if ![200, 404].include?(response.code.to_i)
@weather_info['cod'] = response.code.to_i
@weather_info['message'] = response.message
else
parse_response(response.body)
end

@status = @weather_info['cod']
@message = @weather_info['message']

@weather_info
end

def success?
Expand All @@ -40,16 +52,21 @@ def extract_options!(options)

def parse_response(response)
return if response.nil?
@weather_info = JSON.parse(response)
@status = @weather_info['cod']
@message = @weather_info['message'] unless @status

begin
@weather_info = JSON.parse(response)
rescue JSON::ParserError
@weather_info['cod'] = 500
@weather_info['message'] = 'JSON parse error'
end

@weather_info
end

def send_request
uri = URI(@url)
uri.query = URI.encode_www_form(options)
Net::HTTP.get(uri)
Net::HTTP.get_response(uri)
end
end
end
46 changes: 46 additions & 0 deletions spec/fixtures/cassettes/api/forecast_json_parse_error.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions spec/fixtures/cassettes/api/forecast_server_error.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions spec/open_weather/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@
end
response['cod'].to_s.should eq('404')
end

it 'returns error if json parse error' do
response = VCR.use_cassette('api/forecast_json_parse_error') do
OpenWeather::Forecast.city('Cochin, In')
end
response['cod'].to_s.should eq('500')
end

it 'returns error if server return 500' do
response = VCR.use_cassette('api/forecast_server_error') do
OpenWeather::Forecast.city('Cochin, In')
end
response['cod'].to_s.should eq('500')
end
end

context '.city_id' do
Expand Down
4 changes: 2 additions & 2 deletions spec/open_weather/version_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe 'Version' do
it 'should be version 0.10.0' do
OpenWeather::VERSION.should == '0.10.0'
it 'should be version 0.11.0' do
OpenWeather::VERSION.should == '0.11.0'
end
end