From 207418741b74f0cc38faca3b8f5f8140ea9e9b88 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 27 Jun 2023 15:59:50 -0500 Subject: [PATCH] Get list of all SIMs from Keepgo This loads the whole list into memory for now, should be good until we get to tens of thousands of SIMs. --- lib/sim.rb | 2 +- lib/sim_repo.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/sim.rb b/lib/sim.rb index 7318d2fe35874c01ce71e3a1b532773527e423a0..e5e6816c8db334dc27275a60575360d2403c7429 100644 --- a/lib/sim.rb +++ b/lib/sim.rb @@ -5,7 +5,7 @@ require "value_semantics/monkey_patched" class SIM value_semantics do iccid(/\A\d+\Z/) - lpa_code(/\ALPA:/) + lpa_code Either(/\ALPA:/, nil), default: nil remaining_usage_kb Integer remaining_days Integer notes String diff --git a/lib/sim_repo.rb b/lib/sim_repo.rb index b9cd3f4cb8b5f2b94986910fa72b8a11bf008f16..599ad081023a858cf374801d4eef43524e52269d 100644 --- a/lib/sim_repo.rb +++ b/lib/sim_repo.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require "em-http" +require "em-synchrony/em-http" # For apost vs post +require "json" require "lazy_object" require "value_semantics/monkey_patched" @@ -16,6 +19,20 @@ class SIMRepo "accessToken" => CONFIG[:keepgo][:access_token] }.freeze + def all(start_page=1) + req("lines").aget( + head: KEEPGO_HEADERS, query: { page: start_page, per_page: 1000 } + ).then { |req| + result = JSON.parse(req.response) + sims = result.dig("sim_cards", "items")&.map(&SIM.method(:extract)) + + next sims if result.dig("sim_cards", "last_page") == start_page + next [] if !sims || sims.empty? + + all(start_page + 1).then { |next_page| sims + next_page } + } + end + def find(iccid) req("line/#{iccid}/get_details").aget(head: KEEPGO_HEADERS).then { |req| SIM.extract(JSON.parse(req.response)&.dig("sim_card"))