-
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.
186-Add actor that fetchs external api data (#204)
* 186-Add actor that fetchs external api data * 186-add location to fetch_schools --------- Co-authored-by: Edimo <[email protected]>
- Loading branch information
1 parent
38a3c80
commit 3eb1386
Showing
9 changed files
with
142 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
class FetchSchools < Actor | ||
FETCH_SCHOOLS_URL = "https://api.data.gov/ed/collegescorecard/v1/schools" | ||
|
||
input :school_name_like | ||
|
||
output :data | ||
|
||
def call | ||
fail!(error: :missing_college_score_card_api_key) if api_key.blank? | ||
|
||
self.data = fetch_schools | ||
end | ||
|
||
private | ||
|
||
def fetch_schools | ||
response = Faraday.get(FETCH_SCHOOLS_URL, params) | ||
json_response = JSON.parse(response.body) | ||
json_response["results"] | ||
end | ||
|
||
def params | ||
{ | ||
api_key:, | ||
fields: "id,school.name,location", | ||
page: 0, | ||
"school.name" => school_name_like | ||
} | ||
end | ||
|
||
def api_key | ||
ENV.fetch("COLLEGE_SCORE_CARD_API_KEY", nil) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe FetchSchools, type: :actor do | ||
describe ".call" do | ||
let(:school_name) { "The best school" } | ||
|
||
context "when setup is valid" do | ||
let(:valid_params) do | ||
{ | ||
api_key:, | ||
fields: "id,school.name,location", | ||
page: 0, | ||
"school.name" => school_name | ||
} | ||
end | ||
|
||
let(:success_response) { { results: schools }.to_json } | ||
|
||
let(:schools) do | ||
[ | ||
{ "id" => "1", | ||
"school.name" => school_name, | ||
"location.lat" => 42.374471, | ||
"location.lon" => -71.118313 } | ||
] | ||
end | ||
|
||
let(:api_key) { "secret_api_key" } | ||
|
||
before do | ||
allow_any_instance_of(described_class).to receive(:api_key).and_return(api_key) | ||
stub_request(:get, described_class::FETCH_SCHOOLS_URL) | ||
.with(query: valid_params) | ||
.to_return(status: 200, body: success_response, headers: { "Content-Type" => "application/json" }) | ||
end | ||
|
||
it "is successful" do | ||
result = described_class.result(school_name_like: school_name) | ||
expect(result.success?).to be true | ||
end | ||
|
||
it "returns schools list" do | ||
result = described_class.result(school_name_like: school_name) | ||
expect(result.data).to eq(schools) | ||
end | ||
end | ||
|
||
context "when setup is not valid" do | ||
context "when api_key is nil" do | ||
before do | ||
allow_any_instance_of(described_class).to receive(:api_key).and_return("") | ||
end | ||
|
||
it "is failure" do | ||
result = described_class.result(school_name_like: school_name) | ||
expect(result.failure?).to be true | ||
end | ||
end | ||
|
||
it "returns error object" do | ||
result = described_class.result(school_name_like: school_name) | ||
expect(result.error).to eq(:missing_college_score_card_api_key) | ||
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,4 @@ | ||
# frozen_string_literal: true | ||
|
||
require "webmock/rspec" | ||
WebMock.disable_net_connect!(allow_localhost: true) |