-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathbase.rb
More file actions
72 lines (55 loc) · 1.61 KB
/
base.rb
File metadata and controls
72 lines (55 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'net/http'
require 'json'
module OpenWeather
class Base
attr_reader :url, :options, :weather_info, :status, :message
def initialize(url, options)
@status = false
@url = url
@options = extract_options!(options)
@weather_info = { 'cod': nil, 'message': '' }
end
def retrive
response = send_request unless @options.empty?
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?
@status == 200
end
private
def extract_options!(options)
valid_options = [ :id, :lat, :lon, :cnt, :city, :lang, :units, :APPID,
:country]
options.keys.each { |k| options.delete(k) unless valid_options.include?(k) }
if options[:city] || options[:country]
options[:q] = "#{options[:country]},#{options[:city]}"
options.delete(:city)
options.delete(:country)
end
options
end
def parse_response(response)
return if response.nil?
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_response(uri)
end
end
end