~singpolyma/sgx-jmp

6f92e5c93c7d7d75695bfea3e5c0d292b7a83f50 — Stephen Paul Weber 14 days ago 5373e65
Do not crash when bad geo cache
2 files changed, 7 insertions(+), 1 deletions(-)

M lib/area_code_repo.rb
M lib/geo_code.rb
M lib/area_code_repo.rb => lib/area_code_repo.rb +3 -1
@@ 12,7 12,9 @@ class AreaCodeRepo

	def find(q, limit: 3)
		@geo_code_repo.find(q).then { |geo|
			log.debug "AreaCodeRepo#find(#{q.inspect})"
			log.error "AreaCodeRepo#find(#{q.inspect})" unless geo.valid?
			next [] unless geo.valid?

			@db.query_defer(<<~SQL, [geo.country, geo.sql_point, limit])
				SELECT area_code FROM area_codes
				WHERE country=$1

M lib/geo_code.rb => lib/geo_code.rb +4 -0
@@ 21,4 21,8 @@ class GeoCode
	def sql_point
		"POINT(#{'%.10f' % @data['longt']} #{'%.10f' % @data['latt']})"
	end

	def valid?
		@data["longt"] && @data["latt"]
	end
end