Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2128 from DMPRoadmap/feature/research-grant-id
Add typeahead field for Research Grant ID
- Loading branch information
Showing
12 changed files
with
176 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class ResearchProjectsController < ApplicationController | ||
|
||
|
||
DEFAULT_FUNDER_TYPE = "H2020" | ||
|
||
def index | ||
render json: research_projects | ||
end | ||
|
||
def search | ||
@results = research_projects.select { |r| r.description.match(params[:description]) } | ||
render json: @results | ||
end | ||
|
||
private | ||
|
||
def research_projects | ||
@research_projects ||= begin | ||
Rails.cache.fetch(["research_projects", funder_type], expires_in: 1.day) do | ||
Thread.new { OpenAireRequest.new(funder_type).get!.results }.value | ||
end | ||
end | ||
end | ||
|
||
def funder_type | ||
params.fetch(:type, DEFAULT_FUNDER_TYPE) | ||
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,13 @@ | ||
# frozen_string_literal | ||
|
||
class ResearchProject < Struct.new(:grant_id, :description) | ||
|
||
def to_json(val = nil) | ||
{ grant_id: grant_id, description: description }.to_json | ||
end | ||
|
||
def id | ||
object_id | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal | ||
|
||
require "open-uri" | ||
require "nokogiri" | ||
|
||
class OpenAireRequest | ||
|
||
API_URL = "https://api.openaire.eu/projects/dspace/%s/ALL/ALL" | ||
|
||
attr_reader :funder_type | ||
|
||
def initialize(funder_type) | ||
@funder_type = funder_type | ||
end | ||
|
||
def get! | ||
Rails.logger.info("Fetching fresh data from #{API_URL % funder_type}") | ||
data = open(API_URL % funder_type) | ||
Rails.logger.info("Fetched fresh data from #{API_URL % funder_type}") | ||
@results = Nokogiri::XML(data).xpath("//pair/displayed-value").map do |node| | ||
parts = node.content.split("-") | ||
grant_id = parts.shift.to_s.strip | ||
description = parts.join(" - ").strip | ||
ResearchProject.new(grant_id, description) | ||
end | ||
return self | ||
end | ||
|
||
def results | ||
Array(@results) | ||
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