Skip to content

Commit

Permalink
Move $worldwide_api global variable to Services module method
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper committed Oct 20, 2015
1 parent ff469d5 commit efa1b46
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 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
9 changes: 0 additions & 9 deletions config/initializers/worldwide_api.rb

This file was deleted.

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

module Services
def self.publishing_api
Expand All @@ -9,4 +10,14 @@ def self.publishing_api
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
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 efa1b46

Please sign in to comment.