class OpenWeather::Base
Attributes
message[R]
options[R]
status[R]
url[R]
weather_info[R]
Public Class Methods
new(url, options)
click to toggle source
# File lib/open_weather/base.rb, line 9 def initialize(url, options) @status = false @url = url @options = extract_options!(options) end
Public Instance Methods
retrieve(url=nil)
click to toggle source
# File lib/open_weather/base.rb, line 15 def retrieve(url=nil) response = send_request url unless @options.empty? parse_response(response) end
success?()
click to toggle source
# File lib/open_weather/base.rb, line 20 def success? @status == 200 end
Private Instance Methods
extract_options!(options)
click to toggle source
# File lib/open_weather/base.rb, line 26 def extract_options!(options) valid_options = [ :id, :lat, :lon, :cnt, :city, :lang, :units, :APPID, :country, :bbox] 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
parse_response(response)
click to toggle source
# File lib/open_weather/base.rb, line 41 def parse_response(response) return if response.nil? @weather_info = JSON.parse(response) @status = @weather_info['cod'] @message = @weather_info['message'] unless @status @weather_info end
send_request(url=nil)
click to toggle source
# File lib/open_weather/base.rb, line 49 def send_request(url=nil) url = url || @url uri = URI(url) uri.query = URI.encode_www_form(options) Net::HTTP.get(uri) end