-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
206 - add endpoint to return list of schools with cache (#211)
* 206 - Add endpoint to fetch schools * 206 - Add School Model * 206 - Add endpoint to sync schools * add cache --------- Co-authored-by: Edimo <[email protected]>
- Loading branch information
1 parent
3eb1386
commit 575af6f
Showing
13 changed files
with
141 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
REDIS_URL=redis://redis:6379/0 | ||
REDIS_CACHE_URL=redis://redis:6379/1 | ||
POSTGRES_USER=postgres | ||
POSTGRES_PASSWORD=postgres | ||
POSTGRES_HOST=database | ||
ELASTICSEARCH_HOST=http://elasticsearch:9200 | ||
COLLEGE_SCORE_CARD_API_KEY= | ||
COLLEGE_SCORE_CARD_API_KEY= find_it_here_https://github.com/RTICWDT/open-data-maker/blob/master/API.md |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module SchoolContracts | ||
class Index < ApplicationContract | ||
params do | ||
required(:school_name_like).filled(:string) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class SchoolsController < Api::V1::ApiController | ||
skip_before_action :authenticate_devise_api_token! | ||
skip_after_action :verify_authorized | ||
|
||
def index | ||
schools = Rails.cache.fetch(school_index_contract.to_json) do | ||
FetchSchools.result(school_index_contract:).data | ||
end | ||
render json: schools, status: :ok | ||
end | ||
|
||
private | ||
|
||
def school_index_contract | ||
@school_index_contract ||= SchoolContracts::Index.call(permitted_params(:school_name_like)) | ||
end | ||
|
||
def school_name_like | ||
school_index_contract[:school_name_like] | ||
end | ||
end | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe "Schools" do | ||
describe "GET /api/v1/schools" do | ||
let(:do_request) { get "/api/v1/schools", params: } | ||
let(:result) { double("Result", data: schools) } | ||
let(:school_name_like) { "Harvard" } | ||
let(:schools) do | ||
[ | ||
{ "id" => "1", | ||
"school.name" => school_name_like, | ||
"location.lat" => 42.374471, | ||
"location.lon" => -71.118313 } | ||
] | ||
end | ||
let(:params) do | ||
{ | ||
school_name_like: | ||
} | ||
end | ||
let(:school_index_contract) { SchoolContracts::Index.call(params) } | ||
|
||
context "when schools are not cached" do | ||
before do | ||
allow(FetchSchools).to receive(:result).with(school_index_contract:).and_return(result) | ||
do_request | ||
end | ||
|
||
it { expect(response.parsed_body).to match(schools) } | ||
end | ||
|
||
context "when schools are cached" do | ||
let(:school_index_contract) { SchoolContracts::Index.call(params) } | ||
|
||
before do | ||
Rails.cache.fetch(school_index_contract.to_json) do | ||
schools | ||
end | ||
end | ||
|
||
it "does not calls FetchSchools.result" do | ||
expect(FetchSchools).not_to receive(:result) | ||
do_request | ||
end | ||
|
||
it "returns schools from cache" do | ||
do_request | ||
expect(response.parsed_body).to match(schools) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.configure do |config| | ||
config.before(:suite) do | ||
Rails.cache.clear | ||
end | ||
end |