Class: Whenissunrise

Inherits:
Object
  • Object
show all
Defined in:
lib/whenissunrise.rb

Class Method Summary collapse

Class Method Details

.sunriseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/whenissunrise.rb', line 10

def self.sunrise
  puts "-----"
  puts "Time to find out your sunrise!"
  puts "-----"
  puts "You will need a latitude and longitude, which you can get by going to https://maps.google.com and selecting 'What's here?' after right-clicking on the map. If you're really lazy you can use 45.249814 and -75.7700702 for Ottawa or 53.2839064 and -9.0837658 for Galway :)"
  puts "-----"

  # get data from user
  puts "Enter your latitude"
  lat = gets.chomp
  puts "Enter your longitude"
  long = gets.chomp

  puts "getting data..."

  url = "https://api.sunrise-sunset.org"
  date = "today"

  # do the api call
  data = HTTParty.get("#{url}/json?lat=#{lat}&lng=#{long}&date=#{date}")

  # format the data
  dataHash = JSON[data.body]['results']
  sunrise = dataHash['sunrise']
  sunset = dataHash['sunset']

  # display the data
  puts "Sunrise is at #{sunrise}"
  puts "Sunset is at #{sunset}"

  puts "-----"
  puts "Thank you to https://sunrise-sunset.org for providing the data :)"
end