	

    require 'nokogiri'
    require 'open-uri'
     
    # Bom Fetcher Module
    # ------------------
    # Fetches the locations from BoM or updates the location weather
    # from BoM.
     
    module BomFetcher
      # The source name, and URL to fetch the data from.
      SOURCE = "bom"
      URL = "http://www.bom.gov.au/vic/observations/melbourne.shtml"
     
      def self.get_location_dom
        # Opens the HTML document (give it the URL of the HTML file)
        doc = Nokogiri::HTML(open(URL))
     
        # Fetches the DOM for the locations
        return doc.css("#tMELBOURNE tbody tr")
      end
     
      # get_locations
      # -------------
      # Deletes all the current locations in the database (if any) and
      # creates new records with the locations on the HTML page specified by
      # the URL.
      def self.get_locations
     
        # Delete all locations from the db.
        Location.delete_all
     
        locations = get_location_dom
     
        # For each location DOM, scrape the name and create a new
        # location.
        locations.each do |location|
          name = location.css('th a').text
          location = Location.new(name: name)
          location.save
          puts "Location #{name} successfully fetched"
        end
      end
     
      # get_location_weather
      # --------------------
      # Scrapes the location weather data from the HTML page given by the URL.
      def self.get_location_weather
        locations = get_location_dom
     
        # For each location DOM, scrape the relevant weather data and then store it
        # into the database.
        locations.each do |location|
          # Initialise a weather data hash
          weather_data = {}
     
          # Get the name of the location
          name = location.css('th a').text
     
          # Get the tag of the location
          location_tag = location.css('th')[0]["id"]
     
          # Get the location data from the database
          location_data = Location.where(name: name).first
     
          # Set the weather data to the relevant values
          weather_data[:time] = Time.current
          weather_data[:date] = "#{weather_data[:time].day}-#{weather_data[:time].month}-#{weather_data[:time].year}"
          weather_data[:curr_temp] = location.css("td").at("td[headers='obs-temp #{location_tag}']").text.to_f
          weather_data[:dew_point] = location.css("td").at("td[headers='obs-dewpoint #{location_tag}']").text.to_f
          weather_data[:rainfall] = Rainfall.from9am_mm_to_mmhr(location.css("td").at("td[headers='obs-rainsince9am #{location_tag}']").text.to_f)
          weather_data[:wind_dir] = location.css("td").at("td[headers='obs-wind obs-wind-dir #{location_tag}']").text
          weather_data[:wind_speed] = location.css("td").at("td[headers='obs-wind obs-wind-spd-kph #{location_tag}']").text.to_f
     
          # Insert the weather data into the database
          location_data.insert_weather_data(SOURCE, weather_data)
        end
      end
    end

