<commit_before>require "windward/version"
require 'mechanize'

module Windward

  class Weather

    def initialize
      @departments = departments
      @cities = cities
      @regions = load_data
    end

    def regions
      @regions.keys
    end

    def region(name)
      @regions[name]
    end

    def previsions(name)
      @regions[name]["previsions"]
    end

    def reload
      @regions = load_data
    end

    def self.root
      File.expand_path('../..', __FILE__)
    end

    private

    def departments
      load_data_library({ file: "lib/data/regions.xml", root: "province", code: "code", name: "name_province" })
    end

    def cities
      load_data_library({ file: "lib/data/cities.xml", root: "city", code: "zip_code", name: "name" })
    end

    def load_data_library options={ file: nil, root: nil, code: nil, name: nil }
      doc = Nokogiri::XML(open(File.join Weather.root, options[:file]))
      root_elements = doc.root.xpath(options[:root])
      data = Hash.new
      root_elements.each do |p|
        data[p.at_xpath(options[:code]).text] = p.at_xpath(options[:name]).text
      end
      data
    end

    def load_data
      a = Mechanize.new
      page = a.get('http://www.meteofrance.com/accueil')
      page.encoding = 'utf-8'
      data  = page.parser.css("div.select-region")
      regions = Hash.new
      data.css("option").each do |option|
        if  option['data-type'] == "REG_FRANCE"
          name = option.content
          slug = option['data-slug']
          value = option['value']
          regions[name] = { "slug" => slug, "value" => value }
        end
      end
      regions.each do |name, values|
        data = page.parser.css("div##{values['value']}")
        previsions = Hash.new
        data.each do |datum|
          temps = datum.css('span.picTemps').first.content.split(' - ').last.strip
          temper = datum.css('span.temper').first.content.strip
          department = @departments[datum['data-insee'][0..1]]
          city = @cities[datum['data-insee'][0..4]]
          previsions[department] = { "temps" => temps, "temper" => temper, "city" => city }
        end
        regions[name] = regions[name].merge({ "previsions" => previsions })
      end
      regions
    end

  end

end<commit_msg>Some refactoring, try to avoid complexity on load_regions_data<commit_after>require "windward/version"
require 'mechanize'

module Windward

  class Weather

    def initialize
      @departments = load_departments_data
      @cities = load_cities_data
      @regions = load_regions_data
    end

    def regions
      @regions.keys
    end

    def region(name)
      @regions[name]
    end

    def previsions(name)
      @regions[name]["previsions"]
    end

    def reload
      @regions = load_data
    end

    def self.root
      File.expand_path('../..', __FILE__)
    end

    private

    def load_departments_data
      load_data_library({ file: "lib/data/regions.xml", root: "province", code: "code", name: "name_province" })
    end

    def load_cities_data
      load_data_library({ file: "lib/data/cities.xml", root: "city", code: "zip_code", name: "name" })
    end

    def load_regions_data
      # Get webpage
      page = meteofrance_webpage
      # Store regions data
      data = page.parser.css('div.select-region')
      # Organize rgions data
      regions = parse_regions data
      # Get previsions for each region
      regions.each do |name, values|
        data = page.parser.css("div##{values['value']}")
        previsions = region_previsions data
        regions[name] = regions[name].merge({ 'previsions' => previsions })
      end
      regions
    end

    # Return a hash that contains xml data file content
    def load_data_library options={ file: nil, root: nil, code: nil, name: nil }
      doc = Nokogiri::XML(open(File.join Weather.root, options[:file]))
      root_elements = doc.root.xpath(options[:root])
      data = Hash.new
      root_elements.each do |p|
        data[p.at_xpath(options[:code]).text] = p.at_xpath(options[:name]).text
      end
      data
    end

    # Get and parse webpage 'www.meteofrance.com/accueil'
    def meteofrance_webpage
      a = Mechanize.new
      page = a.get('http://www.meteofrance.com/accueil')
      page.encoding = 'utf-8'
      page
    end

    # Return a hash for each region like { 'Alsace' => {'slug' => 'alsace', 'value' => 'REGI42' } }
    def parse_regions data
      regions = Hash.new
      data.css('option').each do |option|
        if  option['data-type'] == 'REG_FRANCE'
          name = option.content
          slug = option['data-slug']
          value = option['value']
          regions[name] = { 'slug' => slug, 'value' => value }
        end
      end
      regions
    end

    # Complete regions hash with previsions key like :
    # { 'Alsace' =>
    #   { 'slug' => 'alsace', 'value' => 'REGI42',
    #     'previsions' => {'Rhin (Bas)' => {'temps'=>'Éclaircies', 'temper' => '10', 'city' => 'Strasbourg'}}
    #   }
    # }
    def region_previsions data
      previsions = Hash.new
      data.each do |datum|
        temps = datum.css('span.picTemps').first.content.split(' - ').last.strip
        temper = datum.css('span.temper').first.content.strip
        department = @departments[datum['data-insee'][0..1]]
        city = @cities[datum['data-insee'][0..4]]
        previsions[department] = { 'temps' => temps, 'temper' => temper, 'city' => city }
      end
      previsions
    end
  end
end
{'fraction_non_alphanumeric': 0.13656227239621269, 'fraction_numerical': 0.0029133284777858705, 'mean_word_length': 2.420298879202989, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2172851', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1773, 'n_words': 405}