module EA::AreaLookup::Finders

Constants

DEFAULT_API_PARAMS

Public Instance Methods

find_admin_area_by_coordinates(coords) click to toggle source
# File lib/ea/area_lookup/finders.rb, line 8
def find_admin_area_by_coordinates(coords)
  find_area_by_coordinates(coords, "ea-wfs-area_public_face_inspire")
end
find_water_management_area_by_coordinates(coords) click to toggle source
# File lib/ea/area_lookup/finders.rb, line 12
def find_water_management_area_by_coordinates(coords)
  find_area_by_coordinates(coords, "ea-wfs-area_water_management_inspire")
end

Private Instance Methods

api_url() click to toggle source
# File lib/ea/area_lookup/finders.rb, line 37
def api_url
  EA::AreaLookup.config.area_api_url
end
fetch_area_from_api(coords, typename) click to toggle source
# File lib/ea/area_lookup/finders.rb, line 46
def fetch_area_from_api(coords, typename)
  params = DEFAULT_API_PARAMS
           .merge(Filter: "(#{filter_xml(coords)})")
           .merge(TYPENAME: typename)

  full_url = %(#{api_url}?#{params.map { |k, v| "#{k}=#{v}" }.join('&')})
  open full_url, proxy: ENV["http_proxy"]
rescue => e
  raise EA::AreaLookup::ApiConnectionError,
        "Failed with error #{e.inspect} trying to GET #{full_url}"
end
filter_xml(coords) click to toggle source
# File lib/ea/area_lookup/finders.rb, line 58
def filter_xml(coords)
  "<Filter><Intersects>" \
  "<PropertyName>Geometry</PropertyName>" \
  "<gml:Point><gml:coordinates>#{coords.easting},#{coords.northing}</gml:coordinates></gml:Point>" \
  "</Intersects></Filter>"
end
find_area_by_coordinates(coords, typename) click to toggle source
# File lib/ea/area_lookup/finders.rb, line 18
def find_area_by_coordinates(coords, typename)
  validate_config!
  return unless coords && coords.valid?
  xml = fetch_area_from_api(coords, typename)
  parse_xml(xml, typename)
end
parse_xml(response, typename) click to toggle source
# File lib/ea/area_lookup/finders.rb, line 65
def parse_xml(response, typename)
  xml = Nokogiri::XML response
  validate_xml(xml)

  result = %i(area_id code area_name short_name long_name).each_with_object({}) do |path, hash|
    hash[path] = xml.xpath("//gml:featureMember/ms:#{typename}/ms:#{path}").text
  end
  result.tap { |h| EA::AreaLookup.logger.debug(h) }
end
validate_config!() click to toggle source
# File lib/ea/area_lookup/finders.rb, line 41
def validate_config!
  return if api_url
  raise EA::AreaLookup::InvalidConfigError, "Missing area_api_url"
end
validate_xml(xml) click to toggle source
# File lib/ea/area_lookup/finders.rb, line 75
def validate_xml(xml)
  if xml.namespaces["xmlns:ows"] && xml.xpath("//ows:Exception").count > 0
    raise EA::AreaLookup::ApiInvalidRequestError,
          "API returned an exception: #{xml.text && xml.text.strip}"
  end
end