From acb52cb870037b6a679b2ba415ff80e6d13f8d1c Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Fri, 18 Oct 2024 17:27:15 -0700 Subject: [PATCH] Move NCMall and NeoPass services into a Neopets module Just a bit more clarity of grouping! I'm also thinking about extracting modeling APIs into a service file like this too, in which case I think this would help clarify what it is. --- app/models/auth_user.rb | 2 +- app/services/{ => neopets}/nc_mall.rb | 2 +- app/services/{ => neopets}/neopass.rb | 2 +- config/initializers/inflections.rb | 4 ++-- lib/tasks/nc_mall.rake | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) rename app/services/{ => neopets}/nc_mall.rb (99%) rename app/services/{ => neopets}/neopass.rb (98%) diff --git a/app/models/auth_user.rb b/app/models/auth_user.rb index a326b8eb..5894e749 100644 --- a/app/models/auth_user.rb +++ b/app/models/auth_user.rb @@ -161,7 +161,7 @@ class AuthUser < AuthRecord # means we can wrap it in a `with_timeout` block!) neopets_username = Sync do |task| task.with_timeout(5) do - NeoPass.load_main_neopets_username(auth.credentials.token) + Neopets::NeoPass.load_main_neopets_username(auth.credentials.token) end rescue Async::TimeoutError nil # If the request times out, just move on! diff --git a/app/services/nc_mall.rb b/app/services/neopets/nc_mall.rb similarity index 99% rename from app/services/nc_mall.rb rename to app/services/neopets/nc_mall.rb index e42df5d0..86ae2b09 100644 --- a/app/services/nc_mall.rb +++ b/app/services/neopets/nc_mall.rb @@ -1,7 +1,7 @@ require "addressable/template" require "async/http/internet/instance" -module NCMall +module Neopets::NCMall # Share a pool of persistent connections, rather than reconnecting on # each request. (This library does that automatically!) INTERNET = Async::HTTP::Internet.instance diff --git a/app/services/neopass.rb b/app/services/neopets/neopass.rb similarity index 98% rename from app/services/neopass.rb rename to app/services/neopets/neopass.rb index 728c285c..681a0964 100644 --- a/app/services/neopass.rb +++ b/app/services/neopets/neopass.rb @@ -2,7 +2,7 @@ require "async/http/internet/instance" # While most of our NeoPass logic is built into Devise -> OmniAuth -> OIDC # OmniAuth plugin, NeoPass also offers some supplemental APIs that we use here. -module NeoPass +module Neopets::NeoPass # Share a pool of persistent connections, rather than reconnecting on # each request. (This library does that automatically!) INTERNET = Async::HTTP::Internet.instance diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index d68fdd2f..33e25628 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -19,10 +19,10 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| # Teach Zeitwerk that `RocketAMF` is what to expect in `lib/rocketamf`. inflect.acronym "RocketAMF" - # Teach Zeitwerk that `NeoPass` is what to expect in `app/services/neopass.rb`. + # Teach Zeitwerk that `NeoPass` is what to expect in `neopass.rb`. inflect.acronym "NeoPass" - # Teach Zeitwerk that "NCMall" is what to expect in `app/services/nc_mall.rb`. + # Teach Zeitwerk that "NCMall" is what to expect in `nc_mall.rb`. # (We do this by teaching it the word "NC".) inflect.acronym "NC" end diff --git a/lib/tasks/nc_mall.rake b/lib/tasks/nc_mall.rake index 837e12fd..3a90cbeb 100644 --- a/lib/tasks/nc_mall.rake +++ b/lib/tasks/nc_mall.rake @@ -77,17 +77,17 @@ end def load_all_nc_mall_pages Sync do # First, start loading the homepage. - homepage_task = Async { NCMall.load_home_page } + homepage_task = Async { Neopets::NCMall.load_home_page } # Next, load the page links for different categories etc. - links = NCMall.load_page_links + links = Neopets::NCMall.load_page_links # Next, load the linked pages, 10 at a time. barrier = Async::Barrier.new semaphore = Async::Semaphore.new(10, parent: barrier) begin linked_page_tasks = links.map do |link| - semaphore.async { NCMall.load_page link[:type], link[:cat] } + semaphore.async { Neopets::NCMall.load_page link[:type], link[:cat] } end barrier.wait # Load all the pages. ensure