Get list of all SIMs from Keepgo
Stephen Paul Weber
created
This loads the whole list into memory for now, should be good until we get to
tens of thousands of SIMs.
Change summary
lib/sim.rb | 2 +-
lib/sim_repo.rb | 17 +++++++++++++++++
2 files changed, 18 insertions(+), 1 deletion(-)
Detailed changes
@@ -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
@@ -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"))