Factor out identical part of keepgo requests

Stephen Paul Weber created

Change summary

lib/sim_repo.rb | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

Detailed changes

lib/sim_repo.rb 🔗

@@ -17,10 +17,7 @@ class SIMRepo
 	}.freeze
 
 	def find(iccid)
-		EM::HttpRequest.new(
-			"https://myaccount.keepgo.com/api/v2/line/#{iccid}/get_details",
-			tls: { verify_peer: true }
-		).aget(head: KEEPGO_HEADERS).then { |req|
+		req("line/#{iccid}/get_details").aget(head: KEEPGO_HEADERS).then { |req|
 			SIM.extract(JSON.parse(req.response)&.dig("sim_card"))
 		}
 	end
@@ -28,10 +25,7 @@ class SIMRepo
 	def refill(sim, **kwargs)
 		iccid = sim.is_a?(String) ? sim : sim.iccid
 
-		EM::HttpRequest.new(
-			"https://myaccount.keepgo.com/api/v2/line/#{iccid}/refill",
-			tls: { verify_peer: true }
-		).apost(
+		req("line/#{iccid}/refill").apost(
 			head: KEEPGO_HEADERS,
 			body: kwargs.to_json
 		).then { |req| JSON.parse(req.response) }
@@ -60,4 +54,13 @@ class SIMRepo
 			SELECT iccid FROM sims WHERE customer_id IS NULL LIMIT 1
 		SQL
 	end
+
+protected
+
+	def req(path)
+		EM::HttpRequest.new(
+			"https://myaccount.keepgo.com/api/v2/#{path}",
+			tls: { verify_peer: true }
+		)
+	end
 end