Skip to content

Commit

Permalink
Merge pull request #352 from turingschool/2405_b3
Browse files Browse the repository at this point in the history
Fix outstanding references to Propublica
  • Loading branch information
jamisonordway authored May 8, 2024
2 parents 43f7d6b + 38024fd commit 634eba9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions module3/lessons/refactoring_api_consumption.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ We will start with the house-salad-7 app's [`api-consumption-complete` branch](h
Instructions on using Rails Encrypted Credentials can be found [here](./rails_encrypted_credentials.md).

<section class="call-to-action">
NOTE: This lesson assumes that Rails Encrypted Credentials have already been set up with a `congress[:key]` ready to go. If you haven't already done so, get a Propublica API Key [here](https://api.congress.gov/sign-up/).
NOTE: This lesson assumes that Rails Encrypted Credentials have already been set up with a `congress[:key]` ready to go. If you haven't already done so, get a Congress API Key [here](https://api.congress.gov/sign-up/).
</section>

## Learning Goals
Expand Down Expand Up @@ -244,17 +244,19 @@ Let's do a little bit of declarative programming and write code that represents
class SearchController < ApplicationController
def index
@facade = SearchFacade.new(params[:state])
# state = params[:state]

# conn = Faraday.new(url: "https://api.propublica.org") do |faraday|
# faraday.headers["X-API-Key"] = Rails.application.credentials.propublica[:key]
state = params[:state]
# conn = Faraday.new(url: "https://api.congress.gov") do |faraday|
# faraday.headers["X-API-Key"] = Rails.application.credentials.congress[:key]
# end

# response = conn.get("/congress/v1/members/house/#{state}/current.json")

# response = conn.get("/v3/member?limit=250")
# json = JSON.parse(response.body, symbolize_names: true)
# @members = json[:results].map do |member_data|
# Member.new(member_data)
# @members_by_state = []
# json[:members].each do |member_data|
# if member_data[:state] == state
# @members_by_state << member_data
# end
# end
end
end
Expand Down Expand Up @@ -467,7 +469,7 @@ It’s important to note that we did not move over the creation of the `Member`
**Keep your service objects super simple. Hit an endpoint, and get the facade a response. That is IT.**
Let's make one more refactor in our service. If we ever need to hit a different Congress API endpoint, for instance, to get members of the Senate, it would be nice if we could reuse that Faraday connection object. This object sets up the base url for the api and the api key, both things that will be consistent across API calls to Propublica, which makes it the perfect candidate to increase reusability. Since our members_of_house method is a class method, our `conn` method will also need to be a class method.
Let's make one more refactor in our service. If we ever need to hit a different Congress API endpoint, for instance, to get members of the Senate, it would be nice if we could reuse that Faraday connection object. This object sets up the base url for the api and the api key, both things that will be consistent across API calls to the Congress API, which makes it the perfect candidate to increase reusability. Since our members_of_house method is a class method, our `conn` method will also need to be a class method.

*app/services/congress_service.rb*

Expand Down
2 changes: 1 addition & 1 deletion module3/lessons/testing_tools_for_api_consumption.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ Run your tests and they should be passing. If you look under `spec/fixtures/vcr

## Filtering Sensitive Data

If you look closely in that `.yml` file you can see our API key in there. We will be pushing these cassettes to GitHub, so we don't want the actual API key to be recorded for the same reasons we don't want our `application.yml` file pushed and we don't want to hardcode the API key in our code. We will use a VCR option to replace the actual API key with a placeholder. Open up your `rails_helper.rb` and add another line to the VCR configuration:
If you look closely in that `.yml` file you can see our API key in there. We will be pushing these cassettes to GitHub, so we don't want the actual API key to be recorded for the same reasons we don't want to hardcode the API key in our code. We will use a VCR option to replace the actual API key with a placeholder. Open up your `rails_helper.rb` and add another line to the VCR configuration:

*spec/rails_helper.rb*

Expand Down

0 comments on commit 634eba9

Please sign in to comment.