local_calling_guide_repo.rb

 1# frozen_string_literal: true
 2
 3require "nokogiri"
 4
 5class LocalCallingGuideRepo
 6	# @param npa [String] area code (first 3 digits after country code)
 7	# @param nxx [String] exchange (digits 4-6)
 8	# @return [EMPromise<Hash{Symbol => Float}>] :lat and :lon of the rate center
 9	# @raise [RuntimeError] if no prefix data found
10	def find(npa, nxx)
11		EM::HttpRequest.new(
12			"https://localcallingguide.com/xmlprefix.php",
13			tls: { verify_peer: true }
14		).aget(query: { npa: npa, nxx: nxx }).then { |res|
15			doc = Nokogiri::XML.parse(res.response)
16			prefix = doc.at("prefixdata")
17			raise "No prefix data for #{npa}-#{nxx}" unless prefix
18
19			{
20				lat: prefix.at("rc-lat").text.to_f,
21				lon: prefix.at("rc-lon").text.to_f
22			}
23		}
24	end
25end