1# frozen_string_literal: true
2
3require "json"
4
5require_relative "geo_code_repo"
6
7class AreaCodeRepo
8 def initialize(db: DB, geo_code_repo: GeoCodeRepo.new)
9 @db = db
10 @geo_code_repo = geo_code_repo
11 end
12
13 def find(q, limit: 3)
14 @geo_code_repo.find(q).then { |geo|
15 log.debug "AreaCodeRepo#find(#{q.inspect})"
16 @db.query_defer(<<~SQL, [geo.country, geo.sql_point, limit])
17 SELECT area_code FROM area_codes
18 WHERE country=$1
19 ORDER BY location <-> $2
20 LIMIT $3
21 SQL
22 }.then { |rows| rows.map { |row| row["area_code"] } }
23 end
24end