module AecResults
Constants
- BASE_URL
- DATA_PATH
- DEFAULT_ELECTION_ID
- ELECTION_CACHE_TTL_SEC
- STYLE_TITLE
Public Instance Methods
Source
# File lib/aec_results.rb, line 30 def discover_election_id(fetch: nil, time: Time.now) if @election_id && @election_id_fetched_at && (time - @election_id_fetched_at) < ELECTION_CACHE_TTL_SEC return @election_id end html = fetch ? fetch.call("#{BASE_URL}/") : RabbotHttp.fetch("#{BASE_URL}/") match = html.to_s.match(/HouseDivisionFirstPrefs-(\d+)-/i) @election_id = match ? match[1] : DEFAULT_ELECTION_ID @election_id_fetched_at = time @election_id rescue StandardError DEFAULT_ELECTION_ID end
Source
# File lib/aec_results.rb, line 60 def division_url(slug, election_id: nil) id = election_id || discover_election_id "#{BASE_URL}/#{id}/Website/HouseDivisionFirstPrefs-#{id}-#{slug.capitalize}.htm" end
Source
# File lib/aec_results.rb, line 17 def divisions @divisions ||= JSON.parse(File.read(DATA_PATH)) end
Source
# File lib/aec_results.rb, line 65 def fetch_seat(name, fetch: nil, election_id: nil) division = find_division(name) return nil unless division url = division_url(division["slug"], election_id: election_id) html = fetch ? fetch.call(url) : RabbotHttp.fetch(url) parse_seat_html(html, division: division) rescue StandardError nil end
Source
# File lib/aec_results.rb, line 44 def find_division(query) needle = query.to_s.strip.downcase return nil if needle.empty? exact = divisions.find do |row| row["name"].to_s.downcase == needle || row["slug"].to_s.downcase == needle end return exact if exact divisions.find do |row| name = row["name"].to_s.downcase slug = row["slug"].to_s.downcase name.include?(needle) || needle.include?(name) || slug.include?(needle.gsub(/\s+/, "")) end end
Source
# File lib/aec_results.rb, line 91 def format_seat(result) if result.nil? return [ IrcFormat.report_header(tag: "AEC", title: "Division not found", style: STYLE_TITLE), "Try a seat name from the divisions list.", IrcFormat.report_footer("Australian Electoral Commission") ].join("\n") end lines = [ IrcFormat.report_header(tag: "AEC", title: "#{result[:name]} (#{result[:state]})", style: STYLE_TITLE) ] if result[:tcp] lines << "Two-candidate-preferred: #{result[:tcp]}%" else lines << "Results not available — may be outside election period." end lines << "Swing: #{result[:swing]}%" if result[:swing] lines << IrcFormat.report_footer("results.aec.gov.au") lines.join("\n") end
Source
# File lib/aec_results.rb, line 76 def parse_seat_html(html, division:) doc = Nokogiri::HTML(html.to_s) text = doc.text.gsub(/\s+/, " ") tcp_match = text.match(/Two candidate preferred.*?(?:ALP|LIB|NAT|GRN|IND|LNP|CLP)[^%]*?(\d+\.\d)\s*%/i) swing_match = text.match(/Swing\s*([+-]?\d+\.\d)\s*%/i) { name: division["name"], state: division["state"], tcp: tcp_match ? tcp_match[1] : nil, swing: swing_match ? swing_match[1] : nil } end
Source
# File lib/aec_results.rb, line 21 def reset_divisions! @divisions = nil end
Source
# File lib/aec_results.rb, line 25 def reset_election_cache! @election_id = nil @election_id_fetched_at = nil end