-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ROR API query to route through server to protect user IP
- Loading branch information
1 parent
04cbde3
commit 35adcc5
Showing
4 changed files
with
80 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module RORClient | ||
class Client | ||
ENDPOINT = 'https://api.ror.org'.freeze | ||
|
||
def initialize(endpoint = ENDPOINT) | ||
@endpoint = RestClient::Resource.new(endpoint) | ||
end | ||
|
||
# Search for an institution by name | ||
def query_name(query) | ||
encoded_query = URI.encode_www_form_component(query) | ||
request("organizations?query=#{encoded_query}") | ||
end | ||
|
||
# Fetch institution details by ROR ID | ||
def fetch_by_id(ror_id) | ||
request("organizations/#{ror_id}") | ||
end | ||
|
||
private | ||
|
||
# Generic method to make requests and handle errors | ||
def request(path) | ||
response = @endpoint[path].get(accept: 'application/json') | ||
JSON.parse(response.body) | ||
rescue RestClient::ExceptionWithResponse => e | ||
{ error: "ROR API error: #{e.response}" } | ||
rescue StandardError => e | ||
{ error: "Unexpected error: #{e.message}" } | ||
end | ||
end | ||
end |