bandwidth_tn_reservation_repo.rb

 1# frozen_string_literal: true
 2
 3require "forwardable"
 4require "ruby-bandwidth-iris"
 5require "faraday/em_synchrony"
 6Faraday.default_adapter = :em_synchrony
 7
 8class BandwidthTnReservationRepo
 9	FOUR_HOURS = 60 * 60 * 4
10
11	def initialize(redis: REDIS)
12		@redis = redis
13	end
14
15	def ensure(customer, tel)
16		REDIS.exists(key(customer, tel)).then do |exists|
17			unless exists == 1
18				reservation_id = BandwidthIris::TnReservation.create(tel).reservation_id
19				REDIS.setex(key(customer, tel), FOUR_HOURS, reservation_id)
20			end
21		end
22	end
23
24	def get(customer, tel)
25		REDIS.get(key(customer, tel))
26	end
27
28	def key(customer, tel)
29		"jmp_customer_tn_reservation_#{tel}-#{customer.customer_id}"
30	end
31end