From 44764bd5a896dba55b0014b6b15afe0bd88c8c2f Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Sun, 30 Sep 2018 18:02:18 +0100 Subject: [PATCH] Handle tags through methods not directly on the instance variable --- lib/uk_planning_scraper/authority.rb | 39 ++++++++++++++++++---------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/uk_planning_scraper/authority.rb b/lib/uk_planning_scraper/authority.rb index 2f4e5f8..06257d0 100644 --- a/lib/uk_planning_scraper/authority.rb +++ b/lib/uk_planning_scraper/authority.rb @@ -2,13 +2,13 @@ require 'csv' module UKPlanningScraper class Authority - attr_reader :name, :tags, :url + attr_reader :name, :url @@authorities = [] - def initialize(name, url, tags) - @name = name - @url = url - @tags = tags + def initialize(name, url) + @name = name.strip + @url = url.strip + @tags = [] # Strings in arbitrary order @applications = [] # Application objects end @@ -61,6 +61,21 @@ module UKPlanningScraper output # Single point of successful exit end + def tags + @tags.sort + end + + # Add multiple tags to existing tags + def add_tags(tags) + tags.each { |t| add_tag(t) } + end + + # Add a single tag to existing tags + def add_tag(tag) + clean_tag = tag.strip.downcase.gsub(' ', '') + @tags << clean_tag unless tagged?(clean_tag) # prevent duplicates + end + def tagged?(tag) @tags.include?(tag) end @@ -99,14 +114,14 @@ module UKPlanningScraper # Tagged x def self.tagged(tag) found = [] - @@authorities.each { |a| found << a if a.tags.include?(tag) } + @@authorities.each { |a| found << a if a.tagged?(tag) } found end # Not tagged x def self.not_tagged(tag) found = [] - @@authorities.each { |a| found << a unless a.tags.include?(tag) } + @@authorities.each { |a| found << a unless a.tagged?(tag) } found end @@ -120,14 +135,10 @@ module UKPlanningScraper def self.load # Don't run this method more than once return unless @@authorities.empty? - # FIXME hardcoded file path CSV.foreach(File.join(File.dirname(__dir__), 'uk_planning_scraper', 'authorities.csv')) do |line| - auth = Authority.new( - line[0].strip, - line[1].strip, - line[2..-1].map { |e| e.strip }) - auth.tags << auth.system unless auth.tagged?(auth.system) - auth.tags.sort! + auth = Authority.new(line[0], line[1]) + auth.add_tags(line[2..-1]) + auth.add_tag(auth.system) @@authorities << auth end end