tel_selections.rb

  1# frozen_string_literal: true
  2
  3require "ruby-bandwidth-iris"
  4Faraday.default_adapter = :em_synchrony
  5
  6require_relative "form_template"
  7
  8class TelSelections
  9	THIRTY_DAYS = 60 * 60 * 24 * 30
 10
 11	def initialize(redis: REDIS)
 12		@redis = redis
 13	end
 14
 15	def set(jid, tel)
 16		@redis.setex("pending_tel_for-#{jid}", THIRTY_DAYS, tel)
 17	end
 18
 19	def delete(jid)
 20		@redis.del("pending_tel_for-#{jid}")
 21	end
 22
 23	def [](jid)
 24		@redis.get("pending_tel_for-#{jid}").then do |tel|
 25			tel ? HaveTel.new(tel) : ChooseTel.new
 26		end
 27	end
 28
 29	class HaveTel
 30		def initialize(tel)
 31			@tel = tel
 32		end
 33
 34		def choose_tel
 35			EMPromise.resolve(@tel)
 36		end
 37	end
 38
 39	class ChooseTel
 40		def choose_tel(error: nil)
 41			Command.reply { |reply|
 42				reply.allowed_actions = [:next]
 43				reply.command << FormTemplate.render("tn_search", error: error)
 44			}.then do |iq|
 45				choose_from_list(AvailableNumber.for(iq.form).tns)
 46			rescue StandardError
 47				choose_tel(error: $!.to_s)
 48			end
 49		end
 50
 51		def choose_from_list(tns)
 52			raise "No numbers found, try another search." if tns.empty?
 53
 54			Command.reply { |reply|
 55				reply.allowed_actions = [:next, :prev]
 56				reply.command << FormTemplate.render("tn_list", tns: tns)
 57			}.then { |iq|
 58				next choose_tel if iq.prev?
 59
 60				iq.form.field("tel").value.to_s.strip
 61			}
 62		end
 63
 64		class AvailableNumber
 65			def self.for(form)
 66				new(
 67					Q
 68					.for(form.field("q").value.to_s.strip).iris_query
 69					.merge(enableTNDetail: true, LCA: false)
 70					.merge(Quantity.for(form).iris_query)
 71				)
 72			end
 73
 74			def initialize(iris_query)
 75				@iris_query = iris_query
 76			end
 77
 78			def tns
 79				Command.log.debug("BandwidthIris::AvailableNumber.list", @iris_query)
 80				BandwidthIris::AvailableNumber.list(@iris_query).map(&Tn.method(:new))
 81			end
 82
 83			class Quantity
 84				def self.for(form)
 85					rsm_max = form.find(
 86						"ns:set/ns:max",
 87						ns: "http://jabber.org/protocol/rsm"
 88					).first
 89					if rsm_max
 90						new(rsm_max.content.to_i)
 91					else
 92						Default.new
 93					end
 94				end
 95
 96				def initialize(quantity)
 97					@quantity = quantity
 98				end
 99
100				def iris_query
101					{ quantity: @quantity }
102				end
103
104				# NOTE: Gajim sends back the whole list on submit, so big
105				# lists can cause issues
106				class Default
107					def iris_query
108						{ quantity: 10 }
109					end
110				end
111			end
112		end
113
114		class Tn
115			attr_reader :tel
116
117			def initialize(full_number:, city:, state:, **)
118				@tel = "+1#{full_number}"
119				@locality = city
120				@region = state
121			end
122
123			def formatted_tel
124				@tel =~ /\A\+1(\d{3})(\d{3})(\d+)\Z/
125				"(#{$1}) #{$2}-#{$3}"
126			end
127
128			def option
129				op = Blather::Stanza::X::Field::Option.new(value: tel, label: to_s)
130				op << reference
131				op
132			end
133
134			def reference
135				Nokogiri::XML::Builder.new { |xml|
136					xml.reference(
137						xmlns: "urn:xmpp:reference:0",
138						begin: 0,
139						end: formatted_tel.length - 1,
140						type: "data",
141						uri: "tel:#{tel}"
142					)
143				}.doc.root
144			end
145
146			def to_s
147				"#{formatted_tel} (#{@locality}, #{@region})"
148			end
149		end
150
151		class Q
152			def self.register(regex, &block)
153				@queries ||= []
154				@queries << [regex, block]
155			end
156
157			def self.for(q)
158				@queries.each do |(regex, block)|
159					match_data = (q =~ regex)
160					return block.call($1 || $&, *$~.to_a[2..-1]) if match_data
161				end
162
163				raise "Format not recognized: #{q}"
164			end
165
166			def initialize(q)
167				@q = q
168			end
169
170			{
171				areaCode: [:AreaCode, /\A[2-9][0-9]{2}\Z/],
172				npaNxx: [:NpaNxx, /\A(?:[2-9][0-9]{2}){2}\Z/],
173				npaNxxx: [:NpaNxxx, /\A(?:[2-9][0-9]{2}){2}[0-9]\Z/],
174				zip: [:PostalCode, /\A\d{5}(?:-\d{4})?\Z/],
175				localVanity: [:LocalVanity, /\A~(.+)\Z/]
176			}.each do |k, args|
177				klass = const_set(
178					args[0],
179					Class.new(Q) {
180						define_method(:iris_query) do
181							{ k => @q }
182						end
183					}
184				)
185
186				args[1..-1].each do |regex|
187					register(regex) { |q| klass.new(q) }
188				end
189			end
190
191			class State
192				Q.register(/\A[a-zA-Z]{2}\Z/, &method(:new))
193
194				STATE_MAP = {
195					"QC" => "PQ"
196				}.freeze
197
198				def initialize(state)
199					@state = STATE_MAP.fetch(state.upcase, state.upcase)
200				end
201
202				def iris_query
203					{ state: @state }
204				end
205			end
206
207			class CityState
208				Q.register(/\A([^,]+)\s*,\s*([a-zA-Z]{2})\Z/, &method(:new))
209
210				CITY_MAP = {
211					"ajax" => "Ajax-Pickering",
212					"kitchener" => "Kitchener-Waterloo",
213					"new york" => "New York City",
214					"pickering" => "Ajax-Pickering",
215					"sault ste marie" => "sault sainte marie",
216					"sault ste. marie" => "sault sainte marie",
217					"south durham" => "Durham",
218					"township of langley" => "Langley",
219					"waterloo" => "Kitchener-Waterloo",
220					"west durham" => "Durham"
221				}.freeze
222
223				def initialize(city, state)
224					@city = CITY_MAP.fetch(city.downcase, city)
225					@state = State.new(state)
226				end
227
228				def iris_query
229					@state.iris_query.merge(city: @city)
230				end
231			end
232		end
233	end
234end