bin/barometer in barometer-0.7.3 vs bin/barometer in barometer-0.8.0
- old
+ new
@@ -1,19 +1,19 @@
#!/usr/bin/env ruby
-# == Barometer
+# == Barometer
# This is the command line interface to the barometer gem.
#
# == Examples
# This command will measure the weather for the given query.
# barometer berlin
#
# Other examples:
# barometer --yahoo 90210
# barometer --verbose 'new york'
#
-# == Usage
+# == Usage
# barometer [options] query
#
# For help use: barometer -h
#
# Options:
@@ -24,23 +24,22 @@
# -z, --timezone Enhance Timezone
# -m, --metric measure in metric
# -i, --imperial measure in imperial
# --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
# --bug add weather_bug as a source
+# --noaa add NOAA 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
#
# == Author
# Mark G
# http://github.com/attack/barometer
#
# == Copyright
-# Copyright (c) 2009-2011 Mark G. Licensed under the MIT License:
+# Copyright (c) 2009-2013 Mark G. Licensed under the MIT License:
# http://www.opensource.org/licenses/mit-license.php
require 'rubygems'
require 'barometer'
#require '/Users/mark/code/gems/barometer/lib/barometer'
@@ -51,60 +50,57 @@
require 'date'
require 'yaml'
# file where API keys are stored
KEY_FILE = File.expand_path(File.join('~', '.barometer'))
-BAROMETER_VERSION = '0.7.3'
class App
-
attr_reader :options
def initialize(arguments, stdin)
@arguments = arguments.dup
-
+
# Set defaults
@options = OpenStruct.new
@options.timeout = 15
@options.geocode = false
@options.timezone = false
@options.metric = true
@options.sources = []
@options.verbose = false
@options.at = nil
@options.default = true
-
+
# thresholds
@options.windy_m = 10
@options.windy_i = 7
@options.pop = 50
end
# Parse options, check arguments, then process the command
def run
- if parsed_options? && arguments_valid?
+ if parsed_options? && arguments_valid?
puts "Start at #{DateTime.now}\n\n" if @options.verbose
output_options if @options.verbose # [Optional]
-
- process_arguments
+
process_command
-
+
puts "\nFinished at #{DateTime.now}" if @options.verbose
else
output_usage
end
end
-
+
protected
-
+
# future options
#
# time: -a --at
#
def parsed_options?
# Specify options
- opt = OptionParser.new
+ opt = OptionParser.new
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 }
@@ -112,45 +108,26 @@
opt.on('-z', '--timezone') { @options.timezone = true }
opt.on('-m', '--metric') { @options.metric = true }
opt.on('-i', '--imperial') { @options.metric = false }
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('--bug') { @options.sources << :weather_bug; @options.default = false }
+ opt.on('--noaa') { @options.sources << :noaa; @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 }
-
+
opt.parse!(@arguments) rescue return false
-
+
process_options
- true
+ 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
-
+
def config_weather_bug
if File.exists?(KEY_FILE)
- keys = YAML.load_file(KEY_FILE)
- if keys["weather_bug"] && keys["weather_bug"]["code"]
- code = keys["weather_bug"]["code"].to_s
+ keys = YAML.load_file(KEY_FILE)
+ if keys["weather_bug"] && keys["weather_bug"]["code"]
+ code = keys["weather_bug"]["code"].to_s
else
bug_key_message
exit
end
else
@@ -165,46 +142,36 @@
def process_options
@options.sources << :wunderground if @options.default
@options.sources = @options.sources.uniq
Barometer.force_geocode = @options.geocode
Barometer.enhance_timezone = @options.timezone
- if @options.sources.include?(:weather_dot_com)
- @options.sources.delete(:weather_dot_com)
- @options.sources << config_weather_dot_com
- end
if @options.sources.include?(:weather_bug)
@options.sources.delete(:weather_bug)
@options.sources << config_weather_bug
end
Barometer.config = { 1 => @options.sources }
Barometer.timeout = @options.timeout
end
-
+
def output_options
puts "Options:\n"
-
- @options.marshal_dump.each do |name, val|
+
+ @options.marshal_dump.each do |name, val|
puts " #{name} = #{val}"
end
puts
end
# True if required arguments were provided
def arguments_valid?
true if (@arguments.length >= 1 || @options.web)
end
-
- # Setup the arguments
- def process_arguments
- #puts @arguments.inspect
- end
-
+
def output_help
output_version
- #output_usage
end
-
+
def output_usage
puts "Usage: "
puts "barometer [options] query"
puts
puts "For help use: barometer -h"
@@ -217,22 +184,21 @@
puts " -z, --timezone Force timezone query"
puts " -m, --metric measure in metric"
puts " -i, --imperial measure in imperial"
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 " --bug add weather_bug as a source"
+ puts " --noaa add NOAA 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"
end
-
+
def output_version
- puts "#{File.basename(__FILE__)} version #{BAROMETER_VERSION}"
+ puts "#{File.basename(__FILE__)} version #{Barometer::VERSION}"
end
-
+
def process_command
barometer = Barometer.new(@arguments.join(" "))
begin
if @options.verbose
Barometer::debug!
@@ -357,11 +323,11 @@
def pretty_current(c)
return unless c
section("CURRENT", 2) do
pretty_hash({
- "Measured at" => c.current_at, "Updated at" => c.updated_at,
+ "Measured at" => c.current_at, "Updated at" => c.updated_at.to_s(true),
"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,
"Wind Chill" => c.wind_chill })
@@ -373,11 +339,11 @@
def pretty_forecast(f)
return unless f
section("FOR: #{f.date}", 3) do
pretty_hash({
- "Valid From" => f.valid_start_date.to_s(true),
+ "Valid From" => f.valid_start_date.to_s(true),
"Valid Until" => f.valid_end_date.to_s(true),
"Icon" => f.icon, "Description" => f.description,
"Condition" => f.condition, "High" => f.high,
"Low" => f.low, "POP" => f.pop, "Humidity" => f.humidity })
pretty_hash({ "Wind" => f.wind, "Wind Direction" => f.wind.direction,
@@ -426,11 +392,11 @@
end
def pretty_info(w)
title("INFO", 1)
value("GitHub", "http://github.com/attack/barometer")
- value("Barometer Version", BAROMETER_VERSION)
+ value("Barometer Version", Barometer::VERSION)
value("Total Time", "#{(w.end_at - w.start_at)} s")
end
def pretty_output(barometer)
weather = barometer.weather
@@ -447,22 +413,10 @@
pretty_info(weather)
div("-")
end
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
-
def bug_key_message
puts
puts "MISSING KEYS !!!"
puts "Please update the key_file '#{KEY_FILE}' with your weather_bug api key"
puts "Get it here: ???"
@@ -472,16 +426,15 @@
puts
end
# set API keys
if File.exists?(KEY_FILE)
- keys = YAML.load_file(KEY_FILE)
- if keys["yahoo"] && keys["yahoo"]["app_id"]
- Barometer.yahoo_placemaker_app_id = keys["yahoo"]["app_id"]
+ keys = YAML.load_file(KEY_FILE)
+ if keys["yahoo"] && keys["yahoo"]["app_id"]
+ Barometer.yahoo_placemaker_app_id = keys["yahoo"]["app_id"]
end
else
- File.open(KEY_FILE, 'w') {|f| f << "yahoo: app_id: YOUR_KEY_KERE" }
- exit
+ File.open(KEY_FILE, 'w') {|f| f << "yahoo:\n app_id: YOUR_KEY_KERE" }
end
# Create and run the application
app = App.new(ARGV, STDIN)
app.run