class Sunrset::TimeAndSun

Attributes

date[R]
sun_data[R]
time_data[R]

Public Class Methods

new(longitude, latitude) click to toggle source
# File lib/sunrset.rb, line 10
def initialize longitude, latitude
  url_time_api = "http://www.earthtools.org/timezone/#{longitude}/#{latitude}"
  time_xml_data = Net::HTTP.get_response(URI.parse(url_time_api)).body
  @time_data = XmlSimple.xml_in(time_xml_data)
  @date = DateTime.parse @time_data["localtime"].first

  url_sun_api = "http://www.earthtools.org/sun/#{longitude}/#{latitude}/#{"%02d" % @date.day}/#{"%02d" % @date.month}/#{time_zone}/#{dst? ? 1 : 0}"
  sun_xml_data = Net::HTTP.get_response(URI.parse(url_sun_api)).body
  @sun_data = XmlSimple.xml_in(sun_xml_data)
end

Public Instance Methods

dst?() click to toggle source
# File lib/sunrset.rb, line 21
def dst?
  @time_data["dst"].first.eql?("True") ? true : false
end
sunrise() click to toggle source
# File lib/sunrset.rb, line 30
def sunrise
  DateTime.parse "#{@date.to_date.to_s} #{@sun_data["morning"].first["sunrise"].first}"
end
sunset() click to toggle source
# File lib/sunrset.rb, line 34
def sunset
  DateTime.parse "#{@date.to_date.to_s} #{@sun_data["evening"].first["sunset"].first}"
end
time_zone() click to toggle source
# File lib/sunrset.rb, line 25
def time_zone
  offset = @time_data["offset"].first
  offset.to_i > 0 ? "+#{offset}" : offset
end