6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/firstclasspostcodes/operations/get_lookup.rb', line 6
def get_lookup(params)
raise StandError, "Expected hash, received: #{params}" unless params.is_a?(Hash)
error_object = nil
parse_f = ->(val) { val.to_f.to_s == val ? val.to_f : nil }
within = ->(lat, lng) { (lat >= -90 && lat <= 90) && (lng >= -180 && lng <= 180) }
unless params[:latitude] || params[:longitude]
error_object = {
message: "Missing required parameters, expected { latitude, longitude }.",
docUrl: "https://docs.firstclasspostcodes.com/operation/getLookup",
}
end
latitude = parse_f.call(params[:latitude])
longitude = parse_f.call(params[:longitude])
radius = parse_f.call(params[:radius]) || 0.1
query_params = { latitude: latitude, longitude: longitude, radius: radius }
unless latitude && longitude && within.call(latitude, longitude)
error_object = {
message: "Parameter is invalid: #{query_params}",
docUrl: "https://docs.firstclasspostcodes.com/operation/getLookup",
}
end
request_params = { path: "/lookup", method: :get, query_params: query_params }
@config.logger.debug("Executing operation getLookup: #{request_params}") if @config.debug?
emit("operation:getLookup", request_params)
if error_object
error = StandardError.new(error_object)
@config.logger.debug("Encountered ParameterValidationError: #{error}")
emit("error", error)
raise error
end
request(request_params)
end
|