我正在使用一个潇洒的天气小部件,但是当我运行 Ruby 作业文件时它似乎正在创建一个错误。以下是Ruby文件
require 'net/https'
require 'json'
# Forecast API Key from https://developer.forecast.io
forecast_api_key = ""
# Latitude, Longitude for location
forecast_location_lat = "45.429522"
forecast_location_long = "-75.689613"
# Unit Format
# "us" - U.S. Imperial
# "si" - International System of Units
# "uk" - SI w. windSpeed in mph
forecast_units = "si"
SCHEDULER.every '5m', :first_in => 0 do |job|
http = Net::HTTP.new("api.forecast.io", 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
response = http.request(Net::HTTP::Get.new("/forecast/#{forecast_api_key}/#{forecast_location_lat},#{forecast_location_long}?units=#{forecast_units}"))
forecast = JSON.parse(response.body)
forecast_current_temp = forecast["currently"]["temperature"].round
forecast_current_icon = forecast["currently"]["icon"]
forecast_current_desc = forecast["currently"]["summary"]
if forecast["minutely"] # sometimes this is missing from the response. I don't know why
forecast_next_desc = forecast["minutely"]["summary"]
forecast_next_icon = forecast["minutely"]["icon"]
else
puts "Did not get minutely forecast data again"
forecast_next_desc = "No data"
forecast_next_icon = ""
end
forecast_later_desc = forecast["hourly"]["summary"]
forecast_later_icon = forecast["hourly"]["icon"]
send_event('forecast', { current_temp: "#{forecast_current_temp}°", current_icon: "#{forecast_current_icon}", current_desc: "#{forecast_current_desc}", next_icon: "#{forecast_next_icon}", next_desc: "#{forecast_next_desc}", later_icon: "#{forecast_later_icon}", later_desc: "#{forecast_later_desc}"})
end
它给出了错误/forecast.rb:3: invalid multibyte char (US-ASCII) (SyntaxError)
这很奇怪,因为文件中的第 3 行是空白的。
所以我决定# encoding: utf-8
在文件的开头添加以尝试解决这个问题,但后来我uninitialized constant
在第 4 行得到了错误(又是一个空行)。有人可以帮我解决这个问题吗?
完整的错误消息是
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:3: warning: method redefined; discarding old default_dir
/usr/lib/ruby/1.9.1/rubygems/defaults.rb:25: warning: previous definition of default_dir was here
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:7: warning: method redefined; discarding old default_bindir
/usr/lib/ruby/1.9.1/rubygems/defaults.rb:96: warning: previous definition of default_bindir was here
/home/pi/xxxxx/jobs/forecast.rb:3: invalid multibyte char (US-ASCII)