bin/barometer in attack-barometer-0.3.2 vs bin/barometer in attack-barometer-0.5.0
- old
+ new
@@ -29,13 +29,14 @@
# -V, --verbose Verbose output
# -t, --timeout seconds until service queries will timeout
# -g, --geocode Force Geocoding of query
# -m, --metric measure in metric
# -i, --imperial measure in imperial
-# --no-wunderground DONT use default wunderground as source
-# --yahoo use yahoo as source
-# --google use google as source
+# --wunderground add wunderground as a source
+# --yahoo add yahoo as a source
+# --google add google as a source
+# --weather add weather.com as a source
# -p, --pop pop threshold used to determine wet?
# -s, --wind wind speed threshold used to determine windy?
# -a, --at time/date used to determine when to calculate summary
#
# Web Demo:
@@ -61,29 +62,29 @@
require 'date'
require 'yaml'
# file where API keys are stored
KEY_FILE = File.expand_path(File.join('~', '.barometer'))
+BAROMETER_VERSION = '0.5.0'
class App
- VERSION = '0.3.2'
attr_reader :options
def initialize(arguments, stdin)
@arguments = arguments.dup
# Set defaults
@options = OpenStruct.new
@options.timeout = 15
@options.geocode = false
- @options.skip_graticule = false
@options.metric = true
- @options.sources = [:wunderground]
+ @options.sources = []
@options.verbode = false
@options.web = false
- @options.at = Time.now.utc
+ @options.at = nil
+ @options.default = true
# thresholds
@options.windy_m = 10
@options.windy_i = 7
@options.pop = 50
@@ -116,36 +117,59 @@
opt.on('-v', '--version') { output_version ; exit 0 }
opt.on('-h', '--help') { output_help }
opt.on('-V', '--verbose') { @options.verbose = true }
opt.on('-a n', '--at n') {|n| @options.at = Time.parse(n.to_s) }
opt.on('-t n', '--timeout n') {|n| @options.timeout = n }
- opt.on('-w', '--web') { @options.web = true; ARGV.shift }
opt.on('-g', '--geocode') { @options.geocode = true }
- opt.on('--skip') { @options.skip_graticule = true }
opt.on('-m', '--metric') { @options.metric = true }
opt.on('-i', '--imperial') { @options.metric = false }
- opt.on('--no-wunderground') { @options.sources = @options.sources.delete_if{|s| s == :wunderground} }
- opt.on('--yahoo') { @options.sources << :yahoo }
- opt.on('--google') { @options.sources << :google }
+ opt.on('--wunderground') { @options.sources << :wunderground; @options.default = false }
+ opt.on('--yahoo') { @options.sources << :yahoo; @options.default = false }
+ opt.on('--google') { @options.sources << :google; @options.default = false }
+ opt.on('--weather') { @options.sources << :weather_dot_com; @options.default = false }
opt.on('-p n', '--pop n') {|n| @options.pop = n.to_i || 50 }
opt.on('-s n', '--wind n') {|n| @options.metric ? @options.windy_m = n.to_f || 10 : @options.windy_i = n.to_f || 7 }
# pass these onto vegas
+ opt.on('-w', '--web') { @options.web = true; ARGV.shift }
opt.on('-k', '--kill') { @options.web = true }
opt.on('-S', '--status') { @options.web = true }
opt.parse!(@arguments) rescue return false
process_options
true
end
+
+ def config_weather_dot_com
+ if File.exists?(KEY_FILE)
+ keys = YAML.load_file(KEY_FILE)
+ if keys["weather"] && keys["weather"]["partner"] && keys["weather"]["license"]
+ partner_key = keys["weather"]["partner"].to_s
+ license_key = keys["weather"]["license"].to_s
+ else
+ weather_key_message
+ exit
+ end
+ else
+ File.open(KEY_FILE, 'w') {|f| f << "\nweather:\n partner: PARTNER_KEY\n license: LICENSE_KEY" }
+ weather_key_message
+ exit
+ end
+ { :weather_dot_com => { :keys => { :partner => partner_key, :license => license_key } } }
+ end
# Performs post-parse processing on options
def process_options
+ @options.sources << :wunderground if @options.default
+ @options.sources = @options.sources.uniq
Barometer.force_geocode = @options.geocode
- Barometer.selection = { 1 => @options.sources.uniq }
- Barometer.skip_graticule = @options.skip_graticule
+ if @options.sources.include?(:weather_dot_com)
+ @options.sources.delete(:weather_dot_com)
+ @options.sources << config_weather_dot_com
+ end
+ Barometer.config = { 1 => @options.sources }
Barometer.timeout = @options.timeout
end
def output_options
puts "Options:\n"
@@ -182,13 +206,14 @@
puts " -V, --verbose Verbose output"
puts " -t, --timeout seconds until service queries will timeout"
puts " -g, --geocode Force Geocoding of query"
puts " -m, --metric measure in metric"
puts " -i, --imperial measure in imperial"
- puts " --no-wunderground DONT use default wunderground as source"
- puts " --yahoo use yahoo as source"
- puts " --google use google as source"
+ puts " --wunderground add wunderground as a source"
+ puts " --yahoo add yahoo as a source"
+ puts " --google add google as a source"
+ puts " --weather add weather.com as a source"
puts " -p, --pop pop threshold used to determine wet?"
puts " -s, --wind wind speed threshold used to determine windy?"
puts " -a, --at time/date used to determine when to calculate summary"
puts
puts " Web Demo:"
@@ -196,11 +221,11 @@
puts " -k, --kill stop the web demo background process"
puts " -S, --status show the web demo status"
end
def output_version
- puts "#{File.basename(__FILE__)} version #{VERSION}"
+ puts "#{File.basename(__FILE__)} version #{BAROMETER_VERSION}"
end
def process_command
if @options.web
run_web_mode(@arguments.join(" "))
@@ -272,18 +297,24 @@
end
end
def pretty_query(q)
return unless q
- section("QUERY", 2) do
- pretty_hash({"Format" => q.format})
+ section("ORIGINAL QUERY", 1) do
pretty_hash({
- "Address" => q.geo.address,
- "Locality" => q.geo.locality, "Region" => q.geo.region,
- "Country" => q.geo.country, "Country Code" => q.geo.country_code,
- "Latitude" => q.geo.latitude, "Longitude" => q.geo.longitude }) if q.geo
+ "Query" => q.q, "Format" => q.format,
+ "Country Code" => q.country_code })
end
+ if q.geo
+ section("GEO", 2) do
+ pretty_hash({
+ "Address" => q.geo.address, "Query" => q.geo.query,
+ "Locality" => q.geo.locality, "Region" => q.geo.region,
+ "Country" => q.geo.country, "Country Code" => q.geo.country_code,
+ "Latitude" => q.geo.latitude, "Longitude" => q.geo.longitude })
+ end
+ end
end
def pretty_location(l)
return unless l
section("LOCATION", 2) do
@@ -317,35 +348,48 @@
def pretty_current(c)
return unless c
section("CURRENT", 2) do
pretty_hash({
- "Time" => c.time, "Local Time" => c.local_time,
+ "Measured at" => c.current_at, "Updated at" => c.updated_at,
"Humidity" => c.humidity, "Icon" => c.icon,
"Condition" => c.condition, "Temperature" => c.temperature,
"Dew Point" => c.dew_point, "Heat Index" => c.heat_index,
- "Pressure" => c.pressure, "Visibility" => c.visibility })
- pretty_hash({ "Wind Chill" => c.wind_chill, "Wind" => c.wind,
- "Wind Direction" => c.wind.direction, "Degrees" => c.wind.degrees }) if c.wind
+ "Pressure" => c.pressure, "Visibility" => c.visibility,
+ "Wind Chill" => c.wind_chill })
+ pretty_hash({ "Wind" => c.wind, "Wind Direction" => c.wind.direction,
+ "Wind Degrees" => c.wind.degrees }) if c.wind
pretty_hash({ "Sun Rise" => c.sun.rise, "Sun Set" => c.sun.set }) if c.sun
end
end
def pretty_forecast(f)
return unless f
section("FOR: #{f.date}", 3) do
pretty_hash({
"Date" => f.date, "Icon" => f.icon,
"Condition" => f.condition, "High" => f.high,
- "Low" => f.low, "POP" => f.pop })
+ "Low" => f.low, "POP" => f.pop, "Humidity" => f.humidity })
+ pretty_hash({ "Wind" => f.wind, "Wind Direction" => f.wind.direction,
+ "Wind Degrees" => f.wind.degrees }) if f.wind
pretty_hash({ "Sun Rise" => f.sun.rise, "Sun Set" => f.sun.set }) if f.sun
+ if f.night
+ puts
+ title("NIGHT", 4)
+ pretty_hash({
+ "Date" => f.night.date, "Icon" => f.night.icon,
+ "Condition" => f.night.condition, "POP" => f.night.pop,
+ "Humidity" => f.night.humidity })
+ pretty_hash({ "Wind" => f.night.wind, "Wind Direction" => f.night.wind.direction,
+ "Wind Degrees" => f.night.wind.degrees }) if f.night.wind
+ end
end
end
def pretty_forecasts(forecasts)
return unless forecasts
- section("FORECAST", 3, false) do
+ section("FORECAST(s)", 3, false) do
blank
forecasts.each do |forecast|
pretty_forecast(forecast)
end
end
@@ -353,13 +397,17 @@
def pretty_measurement(m)
return unless m
section(m.source.to_s, 1) do
pretty_hash({
- "Source" => m.source, "Time" => m.time,
+ "Measured At" => m.measured_at,
+ "Source" => m.source, "Time Stamp" => m.utc_time_stamp,
"Metric" => m.metric?, "Success" => m.success? })
end
+ section("MODIFIED QUERY", 2) do
+ pretty_hash({ "Query" => m.query, "Format" => m.format })
+ end
pretty_location(m.location)
pretty_station(m.station)
pretty_timezone(m.timezone)
pretty_current(m.current)
pretty_forecasts(m.forecast)
@@ -376,11 +424,11 @@
end
def pretty_info
title("INFO", 1)
value("GitHub", "http://github.com/attack/barometer")
- value("Barometer Version", VERSION)
+ value("Barometer Version", BAROMETER_VERSION)
end
def pretty_output(barometer)
weather = barometer.weather
if weather
@@ -398,10 +446,11 @@
end
end
def run_web_mode(query=nil)
+ require 'rubygems'
require File.expand_path(File.dirname(__FILE__) + '/../lib/demometer/demometer.rb')
require 'vegas'
Vegas::Runner.new(Demometer, 'demometer') do |opts, app|
# opts is an option parser object
@@ -409,27 +458,41 @@
end
end
def geocode_google_key_message
puts
+ puts "MISSING KEYS !!!"
puts "Please update the key_file '#{KEY_FILE}' with your google api key"
puts "Get it here: http://code.google.com/apis/maps/signup.html"
- puts "The, add this line to the file:"
- puts "geocode_google: YOUR_KEY_KERE"
+ puts "Then, add this line to the file:"
+ puts "google:"
+ puts " geocode: YOUR_KEY_KERE"
puts
end
+def weather_key_message
+ puts
+ puts "MISSING KEYS !!!"
+ puts "Please update the key_file '#{KEY_FILE}' with your weather api keys"
+ puts "Get it here: ???"
+ puts "Then, add these lines to the file:"
+ puts "weather:"
+ puts " partner: PARTNER_KEY"
+ puts " license: LICENSE_KEY"
+ puts
+end
+
# set API keys
if File.exists?(KEY_FILE)
keys = YAML.load_file(KEY_FILE)
- if keys["geocode_google"]
- Barometer.google_geocode_key = keys["geocode_google"]
+ if keys["google"] && keys["google"]["geocode"]
+ Barometer.google_geocode_key = keys["google"]["geocode"]
else
geocode_google_key_message
exit
end
else
- File.open(KEY_FILE, 'w') {|f| f << "geocode_google: YOUR_KEY_KERE" }
+ File.open(KEY_FILE, 'w') {|f| f << "google: geocode: YOUR_KEY_KERE" }
geocode_google_key_message
exit
end
# Create and run the application