lib/forecast.rb in forecast-0.0.7 vs lib/forecast.rb in forecast-0.0.8
- old
+ new
@@ -1,142 +1,74 @@
+require 'ostruct'
require "forecast/version"
require "forecast/config"
-require "forecast/model"
+require "forecast/utils"
require "forecast/collection"
require "forecast/adapter"
require "forecast/adapters/yahoo_adapter"
require "forecast/adapters/open_weather_map_adapter"
require "forecast/adapters/wunderground_adapter"
-require "yaml"
-require "redis"
+require "forecast/adapters/forecast_io_adapter"
+require "forecast/http"
class Forecast
- # instance
+ PROVIDERS = Dir.glob(File.expand_path(File.dirname(__FILE__) + '/forecast/adapters/*.*')).map{ |f| File.basename(f, '_adapter.rb') };
- include Forecast::Model
+ def method_missing(method, *args, &block)
+ @source.send(method, *args, &block)
+ end
- attr_accessor :latitude, :longitude, :date, :temp, :temp_min, :temp_max, :condition, :orig_condition
+ def initialize(attrs = {})
+ @source = OpenStruct.new(attrs)
+ end
+ def as_json(options = nil)
+ @source.table.as_json(options)
+ end
+
+ def to_json *a
+ self.marshal_dump.to_json a
+ end
+
+ def icon
+ # Pick icon from theme
+ if self.condition != nil && Forecast.config.theme.is_a?(Hash)
+ icon_prefix = Forecast.config.theme.has_key?('prefix') ? Forecast.config.theme['prefix'] : ''
+ icon_suffix = Forecast.config.theme.has_key?('suffix') ? Forecast.config.theme['suffix'] : ''
+ icon_name = Forecast.config.theme['conditions'].has_key?(self.condition) ? Forecast.config.theme['conditions'][self.condition] : self.condition
+ # Dasherize
+ icon_name = icon_name.to_s.gsub(/(.)([A-Z])/,'\1-\2').gsub(/\s*/, '').downcase
+ icon = icon_prefix + icon_name + icon_suffix
+ return icon != nil ? icon : self.icon
+ end
+ # Slugified condition as icon name
+ self.condition.is_a?(String) && self.condition.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
+ end
+
# class
class << self
def current(latitude, longitude)
- cache_key = "current:#{latitude},#{longitude}"
- forecast = read_cache(cache_key)
- if forecast == nil
- forecast = adapter.current(latitude, longitude)
- write_cache(cache_key, forecast)
- end
- return forecast
+ return adapter.current(latitude, longitude)
end
def hourly(latitude, longitude)
- cache_key = "hourly:#{latitude},#{longitude}"
- forecasts = read_cache(cache_key)
- if forecasts == nil
- forecasts = adapter.hourly(latitude, longitude)
- write_cache(cache_key, forecasts)
- end
- return forecasts
+ return adapter.hourly(latitude, longitude)
end
def daily(latitude, longitude)
- cache_key = "daily:#{latitude},#{longitude}"
- forecasts = read_cache(cache_key)
- if forecasts == nil
- forecasts = adapter.daily(latitude, longitude)
- write_cache(cache_key, forecasts)
- end
- return forecasts
+ return adapter.daily(latitude, longitude)
end
- private
-
- @adapter = nil
-
+ private
+
def adapter
if @adapter == nil
@adapter = Forecast::Adapter.instance
end
return @adapter
end
-
-
- @cache = nil
-
- def cache
- cache = Forecast.config.cache
- if @cache == nil && ( cache != nil || (!!cache == cache) && cache == true )
- if !!cache == cache
- Forecast.config.cache = {
- expire: 5,
- prefix: :forecast,
- host: "127.0.0.1",
- port: "6379",
- url: nil
- }
- end
- cache_config = Forecast.config.cache
- begin
- if cache_config['url'] != nil
- redis_url = cache_config['url']
- uri = URI.parse(redis_url)
- @cache = Redis.new(host: uri.host, port: uri.port, password: uri.password)
- else
- @cache = Redis.new(host: cache_config['host'], port: cache_config['port'])
- end
- @cache.ping
- rescue
- puts "error connecting to redis"
- end
- end
- return @cache
- end
-
- def cache_key(key)
- cache_prefix = Forecast.config.cache[:prefix]
- return "#{cache_prefix.to_s}:#{key}"
- end
-
- def cache_expire
- cache_expire = Forecast.config.cache[:expire]
- end
-
- def write_cache(key, data)
- if cache == nil
- return;
- end
- puts "WRITE TO CACHE... " + cache_key(key).to_s + ", cache_expire: " + cache_expire.to_s
- cache.set(cache_key(key), data.to_json)
- cache.expire(cache_key(key), cache_expire)
- end
-
- def read_cache(key)
- if cache == nil
- return nil;
- end
- cached_result = cache.get(cache_key(key))
- result = nil
- if cached_result != nil
- puts "READ FROM CACHE: " + cache_key(key).to_s + ", cache_expire: " + cache_expire.to_s
- json = JSON.parse(cached_result)
- if json.is_a?(Array)
- result = Forecast::Collection.new
- json.each do |obj|
- forecast = Forecast.new
- forecast.from_json(obj)
- result << forecast
- end
- elsif json.is_a?(Object)
- result = Forecast.new
- result.from_json(json)
- end
- end
- return result
- end
-
-
end
end