Skip to content

Commit

Permalink
Merge pull request #2025 from alphagov/move-global-variables-to-servi…
Browse files Browse the repository at this point in the history
…ces-module

Move global variables to Services module
  • Loading branch information
floehopper committed Oct 21, 2015
2 parents a58dad8 + 13fc6a2 commit d59d882
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/models/world_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def self.reset_cache

def self.all
cache_fetch("all") do
$worldwide_api.world_locations.with_subsequent_pages.map do |l|
Services.worldwide_api.world_locations.with_subsequent_pages.map do |l|
new(l) if l.format == "World location" and l.details and l.details.slug.present?
end.compact
end
end

def self.find(location_slug)
cache_fetch("find_#{location_slug}") do
data = $worldwide_api.world_location(location_slug)
data = Services.worldwide_api.world_location(location_slug)
self.new(data) if data
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/worldwide_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class WorldwideOrganisation
extend Forwardable

def self.for_location(location_slug)
$worldwide_api.organisations_for_world_location(location_slug).map do |org|
Services.worldwide_api.organisations_for_world_location(location_slug).map do |org|
new(org)
end
end
Expand Down
3 changes: 0 additions & 3 deletions config/initializers/imminence.rb

This file was deleted.

9 changes: 0 additions & 9 deletions config/initializers/worldwide_api.rb

This file was deleted.

16 changes: 16 additions & 0 deletions lib/services.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
require 'gds_api/publishing_api'
require 'gds_api/imminence'
require 'gds_api/worldwide'

module Services
def self.publishing_api
@publishing_api ||= GdsApi::PublishingApi.new(Plek.new.find('publishing-api'))
end

def self.imminence_api
@imminence_api ||= GdsApi::Imminence.new(Plek.new.find('imminence'))
end

def self.worldwide_api
# In development, point at the public version of the API
# as we won't normally have whitehall running
if Rails.env.development?
@worldwide_api ||= GdsApi::Worldwide.new("https://www.gov.uk")
else
@worldwide_api ||= GdsApi::Worldwide.new(Plek.new.find('whitehall-admin'))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LandlordImmigrationCheckCalculator
)

def self.valid_postcode(postcode)
response = $imminence.areas_for_postcode(postcode)
response = Services.imminence_api.areas_for_postcode(postcode)
return false unless response and response.code == 200
areas = response.to_hash["results"]
! areas.find { |a| VALID_BOROUGHS.include?(a["slug"]) }.nil?
Expand Down
2 changes: 1 addition & 1 deletion test/data/landlord-immigration-check-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ lib/smart_answer_flows/landlord-immigration-check/outcomes/outcome_check_not_nee
lib/smart_answer_flows/landlord-immigration-check/outcomes/outcome_check_not_needed_when_hostel_refuge.govspeak.erb: 6c068528a1a1c08d957d6446d03c6c7e
lib/smart_answer_flows/landlord-immigration-check/outcomes/outcome_check_not_needed_when_mobile_home.govspeak.erb: 76921608102f56db3c0a2e7de007a989
lib/smart_answer_flows/landlord-immigration-check/outcomes/outcome_check_not_needed_when_under_18.govspeak.erb: 31a94b4f17d48afac83ba42eab361cab
lib/smart_answer/calculators/landlord_immigration_check_calculator.rb: bd0670bd986a0032d2a21297bc3616e5
lib/smart_answer/calculators/landlord_immigration_check_calculator.rb: ece61292e6e20bdfb86ef479108d019b
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LandlordImmigrationCheckCalculatorTest < ActiveSupport::TestCase
stub_request(:get, %r{\A#{Plek.new.find('imminence')}/areas/E15\.json}).
to_return(body: { _response_info: { status: 404 }, total: 0, results: [] }.to_json)

response = $imminence.areas_for_postcode("E15")
response = Services.imminence_api.areas_for_postcode("E15")

assert_equal 404, response["_response_info"]["status"]
assert_equal 0, response["total"]
Expand Down
2 changes: 1 addition & 1 deletion test/unit/world_location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class WorldLocationTest < ActiveSupport::TestCase
details = {"results" => [loc1, loc2, loc3, loc4]}
response = GdsApi::ListResponse.new(stub(body: details.to_json, headers: {}), nil)

$worldwide_api.stubs(:world_locations).returns(response)
Services.worldwide_api.stubs(:world_locations).returns(response)

results = WorldLocation.all
assert_equal %w(location-1 location-4), results.map(&:slug)
Expand Down

0 comments on commit d59d882

Please sign in to comment.