From 3d61f4ab4f37a42db6ef92119b9da0916c2666f8 Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Wed, 20 May 2026 16:25:11 -0400 Subject: [PATCH] refactor: fetch snikket invites from repo class --- lib/snikket.rb | 42 ++++++++++++++++++++++++++++++++---------- test/test_snikket.rb | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/lib/snikket.rb b/lib/snikket.rb index 2f63ab28a1863104bdbcfaa30cd87e578a6040d8..d12fef3c556faf443eb772bad980aeb868567b23 100644 --- a/lib/snikket.rb +++ b/lib/snikket.rb @@ -277,6 +277,37 @@ module Snikket end class CustomerInstance + class InviteRepo + def initialize(bootstrap_uri:) + @bootstrap_uri = bootstrap_uri + end + + def fetch + url = bootstrap_uri + req(url).then { |res| + parse_invite(url, res) + }.catch { nil } + end + + protected + + attr_reader :bootstrap_uri + + def req(url) + EM::HttpRequest.new( + url, tls: { verify_peer: true } + ).ahead(redirects: 5) + end + + def parse_invite(url, res) + LinkHeaderParser.parse( + Array(res.response_header["LINK"]), base_uri: url + ).group_by_relation_type["alternate"]&.find do |header| + URI.parse(header.target_uri).scheme == "xmpp" + end&.target_uri + end + end + def self.for(customer, domain, launched) new( instance_id: launched.instance_id, @@ -298,16 +329,7 @@ module Snikket end def fetch_invite - url = bootstrap_uri - EM::HttpRequest.new( - url, tls: { verify_peer: true } - ).ahead(redirects: 5).then { |res| - LinkHeaderParser.parse( - Array(res.response_header["LINK"]), base_uri: url - ).group_by_relation_type["alternate"]&.find do |header| - URI.parse(header.target_uri).scheme == "xmpp" - end&.target_uri - }.catch { nil } + InviteRepo.new(bootstrap_uri: bootstrap_uri).fetch end end diff --git a/test/test_snikket.rb b/test/test_snikket.rb index f1f48e57a1cff634cf8249ebd9fa670312577408..f80bee01394620fa2ddeb563ea4a815e46663d10 100644 --- a/test/test_snikket.rb +++ b/test/test_snikket.rb @@ -52,6 +52,20 @@ class TestSnikket < Minitest::Test ) end + def test_customer_instance_bootstrap_uri + instance = Snikket::CustomerInstance.new( + instance_id: "si-1234", + bootstrap_token: "fZLy6iTh", + customer_id: "test", + domain: "example.com" + ) + + assert_equal( + "https://example.com/invites_bootstrap?token=fZLy6iTh", + instance.bootstrap_uri + ) + end + def test_instance_result instance = Blather::XMPPNode.parse(<<~XML) @@ -75,17 +89,25 @@ class TestSnikket < Minitest::Test assert_equal :up, instance.status end - def test_bootstrap_uri - instance = Snikket::CustomerInstance.new( - instance_id: "si-1234", - bootstrap_token: "fZLy6iTh", - customer_id: "test", - domain: "example.com" + def test_invite_repo_fetch_returns_xmpp_alternate_link + stub_request( + :head, + "https://example.com/invites_bootstrap?token=fZLy6iTh" + ).to_return( + status: 200, + headers: { + "Link" => [ + "; rel=\"alternate\"", + "; rel=\"alternate\"" + ].join(", ") + } ) - assert_equal( - "https://example.com/invites_bootstrap?token=fZLy6iTh", - instance.bootstrap_uri - ) + invite = Snikket::CustomerInstance::InviteRepo.new( + bootstrap_uri: "https://example.com/invites_bootstrap?token=fZLy6iTh" + ).fetch.sync + + assert_equal "xmpp:test@example.com?join", invite end + em :test_invite_repo_fetch_returns_xmpp_alternate_link end