DEV Community

armstrca
armstrca

Posted on

Umbrella project

I think the hardest part of this project was keeping track of which sets of information in the external APIs were arrays and which were hashes. The second hardest part was printing out the "in x hours from now, there's a y% chance of precipitation" bit.
At times, the code block variable "|x|" feels like a magic tool that just intuits whatever I'm thinking for it, so I think I need to review how it works.
Additionally, there were a couple different times when I tried to just output incremented integers from 1-12 rather than, as the "possible solution" does, do it based on the relative time. But I seemed to have trouble figuring out how to do that in the brief and simple way I felt like I should be able to do. I would say that the first half of the code I wrote feels self-generated enough to me, whereas for the second half I relied more and more on the "possible_solution.rb" file.
For the precipitation chart, I didn't really feel like I had much choice but to copy and paste most of the code, and then had to work with the GPT to figure out how to add x-axis labels. I also tried for a while to get the code to generate a visible y-axis even if all 12 hours showed 0 precipitation probability, but after various attempts with and without GPT help I gave up on that.
The one last thing I might like to do to improve the code is to include a conditional for cases in which the user input is either not specific enough (i.e. "Springfield") or too poorly misspelled for Google's api to correct. Right now it just throws an error about an index being out of bounds.

Here's the code in full:

require "http"
require "json"
require "ascii_charts"
pp "What location would you like weather info for?"
user_location = gets.chomp.gsub(" ","_")
gmaps_key = ENV.fetch("GMAPS_KEY")
google_url = "https://maps.googleapis.com/maps/api/geocode/json?address=#{user_location}&key=#{gmaps_key}"
user_location = user_location.gsub("_"," ")
gmaps_data = HTTP.get(google_url)
parsed_gmaps_data = JSON.parse(gmaps_data)
results = parsed_gmaps_data.fetch("results")
results2 = results.fetch(0)
geometry = results2.fetch("geometry")
location = geometry.fetch("location")
lat = location.fetch("lat")
lng = location.fetch("lng")

weather_key = ENV.fetch("PIRATE_WEATHER_KEY")
weather_url = "https://api.pirateweather.net/forecast/#{weather_key}/#{lat},#{lng}"

weather_data = HTTP.get(weather_url)
parsed_weather_data = JSON.parse(weather_data)
currently = parsed_weather_data.fetch("currently")
current_temperature = currently.fetch("temperature")
hourly = parsed_weather_data.fetch("hourly")
next_hour_summary = hourly.fetch("summary")
hourly_data_array = hourly.fetch("data")
hourly_data_hash = hourly_data_array.at(0)
first_hourly_precip = hourly_data_hash.fetch("precipProbability")

twelvehour_data_hash = hourly_data_array[1..12]


pp "The current temperature in #{user_location} is #{current_temperature} degrees Fahrenheit."
pp "The forecast for the next hour in #{user_location} is #{next_hour_summary}."
pp "The precipitation probability for the next hour in #{user_location} is #{(first_hourly_precip*100).round}%."

yesrainy = false
precipprob_array = []  
preciptime_array = []

twelvehour_data_hash.each do |hourly|
  precipprob = hourly.fetch("precipProbability")
  precipprob_array << precipprob  

  if precipprob > 0.1
    yesrainy = true
    precip_time = Time.at(hourly.fetch("time"))
    seconds_from_now = precip_time - Time.now
    hours_from_now = seconds_from_now / 60 / 60
    pp "In #{hours_from_now.round} hours, there is a #{(precipprob*100).round}% chance of precipitation."
  else
    precip_time = Time.at(hourly.fetch("time"))
    seconds_from_now = precip_time - Time.now
    preciptime_array << hours_from_now = (seconds_from_now / 60 / 60).round
  end
end


if yesrainy
  pp "You might want an umbrella today!"
else
  pp "You probably won't need an umbrella today."
end

x_axis_labels = (1..12).to_a
if precipprob_array.all? { |value| ((value < 0.1) == true) }
data = precipprob_array.map.with_index { |value, index| [x_axis_labels[index], 0] };
  y_axis_labels = (0..50).step(5).to_a.reverse
  chart = AsciiCharts::Cartesian.new(data, title: "Hourly Precipitation Probability Chart for #{user_location}:", x_axis_labels: x_axis_labels)
  puts chart.draw
else
  data = precipprob_array.map.with_index { |value, index| [x_axis_labels[index], value * 100] };
  chart = AsciiCharts::Cartesian.new(data, title: "Hourly Precipitation Probability Chart for #{user_location}:", x_axis_labels: x_axis_labels)
  puts chart.draw
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)