-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes to API service to get individual providers based on code
- Loading branch information
Jamie
committed
Dec 22, 2023
1 parent
9ab506f
commit abdfae3
Showing
36 changed files
with
240 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 12 additions & 8 deletions
20
app/controllers/placements/support/provider_suggestions_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
class Placements::Support::ProviderSuggestionsController < Placements::Support::ApplicationController | ||
def index | ||
accredited_providers = AccreditedProviderApi.call | ||
filtered_providers = filter_providers(accredited_providers, params[:query]) | ||
filtered_providers = filter_providers(accredited_providers, query_params) | ||
providers = | ||
filtered_providers.map { |provider| formatted_provider(provider) } | ||
render json: providers | ||
end | ||
|
||
private | ||
|
||
def query_params | ||
params.require(:query) | ||
end | ||
|
||
def filter_providers(providers, query) | ||
return providers if query.blank? | ||
|
||
downcase_query = query.downcase | ||
providers.select do |provider| | ||
[ | ||
provider["attributes"]["name"], | ||
provider["attributes"]["postcode"], | ||
provider["attributes"]["urn"], | ||
provider["attributes"]["ukprn"] | ||
provider.dig("attributes", "name"), | ||
provider.dig("attributes", "postcode"), | ||
provider.dig("attributes", "urn"), | ||
provider.dig("attributes", "ukprn"), | ||
].any? { |attribute| attribute&.downcase&.include?(downcase_query) } | ||
end | ||
end | ||
|
||
def formatted_provider(provider) | ||
{ | ||
id: provider["id"], | ||
name: provider["attributes"]["name"], | ||
code: provider["attributes"]["code"] | ||
id: provider.fetch("id"), | ||
name: provider.dig("attributes", "name"), | ||
code: provider.dig("attributes", "code"), | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,47 @@ | ||
class AccreditedProviderApi | ||
include ServicePattern | ||
|
||
def initialize(code = nil) | ||
@code = code | ||
end | ||
|
||
attr_reader :code | ||
|
||
def call | ||
code.present? ? provider_list(code) : provider_details | ||
end | ||
|
||
private | ||
|
||
def provider_list(code) | ||
Rails | ||
.cache | ||
.fetch("accredited_provider_api", expires_in: 24.hours) do | ||
response = HTTParty.get(provider_url) | ||
.fetch("accredited_provider_details_#{code}", expires_in: 24.hours) do | ||
response = HTTParty.get(provider_details_url(code)) | ||
response = JSON.parse(response.to_s) | ||
response["data"] | ||
end | ||
end | ||
|
||
private | ||
def provider_details | ||
Rails | ||
.cache | ||
.fetch("all_accredited_providers", expires_in: 24.hours) do | ||
response = HTTParty.get(all_providers_url) | ||
response = JSON.parse(response.to_s) | ||
response["data"] | ||
end | ||
end | ||
|
||
def all_providers_url | ||
"#{ENV["PUBLISH_BASE_URL"]}/api/public/v1/recruitment_cycles/#{next_year}/providers?filter[is_accredited_body]=true" | ||
end | ||
|
||
def provider_details_url(code) | ||
"#{ENV["PUBLISH_BASE_URL"]}/api/public/v1/recruitment_cycles/#{next_year}/providers/#{code}" | ||
end | ||
|
||
def provider_url | ||
year = Time.current.next_year.year | ||
"https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/#{year}/providers?filter[is_accredited_body]=true" | ||
def next_year | ||
Time.current.next_year.year | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.