Module: GdsApi::TestHelpers::LocationsApi

Defined in:
lib/gds_api/test_helpers/locations_api.rb

Constant Summary collapse

LOCATIONS_API_ENDPOINT =
Plek.current.find("locations-api")

Instance Method Summary collapse

Instance Method Details

#stub_locations_api_does_not_have_a_bad_postcode(postcode) ⇒ Object


32
33
34
35
# File 'lib/gds_api/test_helpers/locations_api.rb', line 32

def stub_locations_api_does_not_have_a_bad_postcode(postcode)
  stub_request(:get, "#{LOCATIONS_API_ENDPOINT}/v1/locations?postcode=#{postcode}")
   .to_return(body: { "code" => 400, "error" => "Postcode '#{postcode}' is not valid." }.to_json, status: 400)
end

#stub_locations_api_has_location(postcode, locations) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gds_api/test_helpers/locations_api.rb', line 6

def stub_locations_api_has_location(postcode, locations)
  results = []
  locations.each do |l|
    results << {
      "latitude" => l["latitude"],
      "longitude" => l["longitude"],
      "postcode" => postcode,
      "local_custodian_code" => l["local_custodian_code"],
    }
  end

  response = {
    "average_latitude" => results.sum { |r| r["latitude"] } / results.size.to_f,
    "average_longitude" => results.sum { |r| r["longitude"] } / results.size.to_f,
    "results" => results,
  }

  stub_request(:get, "#{LOCATIONS_API_ENDPOINT}/v1/locations?postcode=#{postcode}")
    .to_return(body: response.to_json, status: 200)
end

#stub_locations_api_has_no_location(postcode) ⇒ Object


27
28
29
30
# File 'lib/gds_api/test_helpers/locations_api.rb', line 27

def stub_locations_api_has_no_location(postcode)
  stub_request(:get, "#{LOCATIONS_API_ENDPOINT}/v1/locations?postcode=#{postcode}")
    .to_return(body: { "results" => nil }.to_json, status: 200)
end