class OpenWeatherAPI::Resources::Base
Attributes
api_obj[R]
Public Class Methods
new(api_obj)
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 7 def initialize(api_obj) @api_obj = api_obj end
Public Instance Methods
execute(**hash, &block)
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 11 def execute(**hash, &block) @parameters = hash setup_indifferent_access(@parameters) # Let's use json response = RestClient.send :get, base_url, params: build_params(@parameters) raise "Invalid response." unless response.code == 200 # Handle the response format response = self.send "handle_response_#{mode}", response # Handle the block return block.call(response) if block_given? response end
Private Instance Methods
base_url()
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 50 def base_url "http://api.openweathermap.org/data/#{API::VERSION || '2.5'}/" end
build_params(parameters = {})
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 59 def build_params(parameters = {}) { APPID: @api_obj.api_key, lang: @api_obj.default_language }.merge(parameters) end
handle_response_html(response)
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 46 def handle_response_html(response) response.body end
handle_response_json(response)
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 37 def handle_response_json(response) json = JSON.parse(response.body) setup_indifferent_access(json) end
handle_response_raw(response)
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 33 def handle_response_raw(response) response end
handle_response_xml(response)
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 42 def handle_response_xml(response) response.body end
mode()
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 29 def mode (@parameters[:mode] || :json).to_s.to_sym end
setup_indifferent_access(sub_hash)
click to toggle source
# File lib/open-weather-api/resources/base.rb, line 54 def setup_indifferent_access(sub_hash) sub_hash.default_proc = proc{|h, k| h.key?(k.to_s) ? h[k.to_s] : nil} sub_hash.each { |k, v| setup_indifferent_access(v) if v.is_a?(Hash) } end