module KfcLocation
Constants
- AU_STATE_ABBREVS
- STATE_ALIASES
- STATE_TOKEN_PATTERN
Public Instance Methods
Source
# File lib/kfc_location.rb, line 90 def expand_for_api?(location) text = location.to_s.strip return false if text.empty? return false if postcode_query?(text) return false if text.match?(/\baustralia\b/i) true end
Source
# File lib/kfc_location.rb, line 63 def extract_state_abbrev(location) text = location.to_s.strip return nil if text.empty? if (match = text.match(/\b(QLD|NSW|VIC|SA|WA|TAS|NT|ACT)\b/i)) return match[1].upcase end downcased = text.downcase STATE_ALIASES.each do |alias_name, abbrev| next unless downcased.match?(/\b#{Regexp.escape(alias_name)}\b/) return abbrev end nil end
Source
# File lib/kfc_location.rb, line 114 def format_location_token(token) key = token.downcase STATE_ALIASES[key] || token.capitalize end
Source
# File lib/kfc_location.rb, line 55 def format_search_location(location) text = location.to_s.strip return "" if text.empty? tokens = tokenize_location(text) tokens.map { |token| format_location_token(token) }.join(" ") end
Source
# File lib/kfc_location.rb, line 106 def geocoded_query?(query) query.to_s.match?(/,\s*australia\b/i) end
True for a disambiguated query that mirrors what KFC’s own store locator geocodes (e.g. “Kingaroy QLD, Australia”). The API then returns the nearest store first, so such a query can be trusted to resolve the right town.
Source
# File lib/kfc_location.rb, line 81 def location_base_without_state(location) text = location.to_s.strip text = text.gsub(STATE_TOKEN_PATTERN, "") text.gsub(/\baustralia\b/i, "") .gsub(/[,]+/, " ") .gsub(/\s+/, " ") .strip end
Source
# File lib/kfc_location.rb, line 29 def location_search_queries(location) raw = location.to_s.strip return [] if raw.empty? return [raw] if postcode_query?(raw) queries = [raw] titled = format_search_location(raw) queries << titled unless titled.casecmp?(raw) return queries.uniq unless expand_for_api?(raw) state = extract_state_abbrev(raw) base = location_base_without_state(raw) titled_base = format_search_location(base) titled_base = titled if titled_base.empty? if state queries << "#{titled_base} #{state}, Australia" else queries << "#{titled_base} QLD, Australia" queries << "#{titled_base}, Australia" end queries.map(&:strip).reject(&:empty?).uniq end
Source
# File lib/kfc_location.rb, line 99 def postcode_query?(location) location.to_s.strip.match?(/\A\d{4}\z/) end
Source
# File lib/kfc_location.rb, line 110 def tokenize_location(location) location.to_s.strip.split(/\s+/) end