1

I am using a dashing weather widget but it seems to be creating an error when I run the Ruby job file. The following is the Ruby file

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

and it gives the error /forecast.rb:3: invalid multibyte char (US-ASCII) (SyntaxError)

which is weird because line 3 is blank in the file.

So I decided to add # encoding: utf-8 at the beginning of the file to try to fix this but then I get the error uninitialized constant on line 4 (again a blank line). can someone help me out with this?

The complete error message was

/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)
user1893354
  • 5,778
  • 12
  • 46
  • 83
  • How are you generating this file? Is the encoding consistent between your editor and the Ruby runtime? – Vidya Nov 18 '13 at 21:33
  • the forecast.rb file is in the jobs folder of the dashing instance. When dashing is initialized, this file is run. I'm not sure what the encoding of the Ruby runtime is. How should I check this? – user1893354 Nov 18 '13 at 21:37
  • Can you past the complete error message ? Can you check syntax & warnings with `ruby -c -w /forecast.rb` ? – David Unric Nov 18 '13 at 22:04
  • Btw. tried to fetch `forecast.rb` from https://gist.github.com/toddq/5422352/raw/e885ccc84af72669e220704afd457a34dff6be6d/forecast.rb and the file is processed without any issue. – David Unric Nov 18 '13 at 22:08
  • I tried this widget instead https://gist.github.com/mjamieson/5274790 which seems to be the original, its working for me and I think I'm going to stick with it. – user1893354 Nov 19 '13 at 14:46

1 Answers1

2

I changed SCHEDULER to Dashing.scheduler and was able to get past this error.

John Naegle
  • 8,077
  • 3
  • 38
  • 47