| @@ -2,13 +2,13 @@ require 'csv' | |||||
| module UKPlanningScraper | module UKPlanningScraper | ||||
| class Authority | class Authority | ||||
| attr_reader :name, :tags, :url | |||||
| attr_reader :name, :url | |||||
| @@authorities = [] | @@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 | @applications = [] # Application objects | ||||
| end | end | ||||
| @@ -61,6 +61,21 @@ module UKPlanningScraper | |||||
| output # Single point of successful exit | output # Single point of successful exit | ||||
| end | 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) | def tagged?(tag) | ||||
| @tags.include?(tag) | @tags.include?(tag) | ||||
| end | end | ||||
| @@ -99,14 +114,14 @@ module UKPlanningScraper | |||||
| # Tagged x | # Tagged x | ||||
| def self.tagged(tag) | def self.tagged(tag) | ||||
| found = [] | found = [] | ||||
| @@authorities.each { |a| found << a if a.tags.include?(tag) } | |||||
| @@authorities.each { |a| found << a if a.tagged?(tag) } | |||||
| found | found | ||||
| end | end | ||||
| # Not tagged x | # Not tagged x | ||||
| def self.not_tagged(tag) | def self.not_tagged(tag) | ||||
| found = [] | found = [] | ||||
| @@authorities.each { |a| found << a unless a.tags.include?(tag) } | |||||
| @@authorities.each { |a| found << a unless a.tagged?(tag) } | |||||
| found | found | ||||
| end | end | ||||
| @@ -120,14 +135,10 @@ module UKPlanningScraper | |||||
| def self.load | def self.load | ||||
| # Don't run this method more than once | # Don't run this method more than once | ||||
| return unless @@authorities.empty? | return unless @@authorities.empty? | ||||
| # FIXME hardcoded file path | |||||
| CSV.foreach(File.join(File.dirname(__dir__), 'uk_planning_scraper', 'authorities.csv')) do |line| | 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 | @@authorities << auth | ||||
| end | end | ||||
| end | end | ||||