From dc7707149322c63c4192405d041a6c38588f2fe9 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 23 Sep 2018 12:09:32 +0200 Subject: [PATCH] use repository info via api. datacite/datacite#336 --- app/controllers/repositories_controller.rb | 40 ---------- app/models/client.rb | 4 +- app/models/concerns/cacheable.rb | 9 ++- app/models/repository.rb | 87 ---------------------- app/serializers/client_serializer.rb | 2 +- app/serializers/repository_serializer.rb | 7 +- config/routes.rb | 4 - 7 files changed, 14 insertions(+), 139 deletions(-) delete mode 100644 app/controllers/repositories_controller.rb delete mode 100644 app/models/repository.rb diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb deleted file mode 100644 index d190fa15a..000000000 --- a/app/controllers/repositories_controller.rb +++ /dev/null @@ -1,40 +0,0 @@ -class RepositoriesController < ApplicationController - def index - @repositories = Repository.where(params) - - options = {} - options[:meta] = { - total: @repositories.dig(:meta, :total), - "total-pages" => @repositories.dig(:meta, :total_pages), - page: @repositories.dig(:meta, :page) - }.compact - - options[:links] = { - self: request.original_url, - next: @repositories[:data].blank? ? nil : request.base_url + "/repositories?" + { - "page[number]" => params.dig(:page, :number).to_i + 1, - "page[size]" => params.dig(:page, :size), - sort: params[:sort] }.compact.to_query - }.compact - options[:include] = @include - options[:is_collection] = true - - render json: RepositorySerializer.new(@repositories[:data], options).serialized_json, status: :ok - end - - def show - @repository = Repository.where(id: params[:id]) - fail AbstractController::ActionNotFound unless @repository.present? - - options = {} - options[:is_collection] = false - - render json: RepositorySerializer.new(@repository[:data], options).serialized_json, status: :ok - end - - def badge - id = "http://www.re3data.org/public/badges/s/light/" + params[:id][3..-1] - result = Maremma.get(id, accept: "image/svg+xml", raw: true) - render body: result.body.fetch("data", nil), content_type: "image/svg+xml" - end -end diff --git a/app/models/client.rb b/app/models/client.rb index 2d8796745..c10a48db0 100644 --- a/app/models/client.rb +++ b/app/models/client.rb @@ -113,12 +113,12 @@ def as_indexed_json(options={}) "updated" => updated, "deleted_at" => deleted_at, "provider" => provider.as_indexed_json, - "repository" => repository.try(:as_indexed_json) + "repository" => repository } end def self.query_fields - ['symbol^10', 'name^10', 'contact_name^10', 'contact_email^10', 'domains', 'url', '_all'] + ['symbol^10', 'name^10', 'contact_name^10', 'contact_email^10', 'domains', 'url', 'repository.software.name^3', '_all'] end def self.query_aggregations diff --git a/app/models/concerns/cacheable.rb b/app/models/concerns/cacheable.rb index 437a50490..f98823786 100644 --- a/app/models/concerns/cacheable.rb +++ b/app/models/concerns/cacheable.rb @@ -129,9 +129,12 @@ def cached_resource_type_response(id) end def cached_repository_response(id, options={}) - Rails.cache.fetch("repository_response/#{id}", expires_in: 7.days) do - repository = Repository.where(id: id) - repository.present? ? repository[:data] : nil + Rails.cache.fetch("repository_response/#{id}", expires_in: 1.day) do + url = Rails.env.production? ? "https://api.datacite.org" : "https://api.test.datacite.org" + response = Maremma.get(url + "/repositories/" + id) + attributes = response.body.dig("data", "attributes").to_h + attributes = attributes.transform_keys! { |key| key.tr('-', '_') } + attributes.merge("id" => id, "cache_key" => "repositories/#{id}-#{attributes["updated"]}") end end end diff --git a/app/models/repository.rb b/app/models/repository.rb deleted file mode 100644 index 8e5c94b0b..000000000 --- a/app/models/repository.rb +++ /dev/null @@ -1,87 +0,0 @@ -class Repository - include Searchable - include Bolognese::Utils - - RE3DATA_DATE = "2017-10-07" - - attr_reader :id, :name, :additional_name, :description, :repository_url, :repository_contact, :subject, :repository_software, :created, :updated - - def initialize(item, options={}) - @id = item.fetch("id", nil) || item.fetch("re3data.orgIdentifier", nil) - @name = item.fetch("name", nil) || parse_attributes(item.fetch("repositoryName", nil)) - @additional_name = parse_attributes(item.fetch("additionalName", nil)) - @description = parse_attributes(item.fetch("description", nil)) - @repository_url = item.fetch("repositoryURL", nil) - @repository_contact = Array.wrap(item.fetch("repositoryContact", nil)).first - @repository_software = item.dig("software", "softwareName") - @subject = Array.wrap(parse_attributes(item.fetch("subject", []))).map do |s| - k, v = s.split(" ", 2) - { "id" => k.to_i, "name" => v } - end.sort { |a, b| a.fetch("id") <=> b.fetch("id") }.presence - @created = item.fetch("entryDate", nil).present? ? item.fetch("entryDate") + "T00:00:00Z" : nil - @updated = (item.fetch("lastUpdate", nil) || RE3DATA_DATE) + "T00:00:00Z" - end - - def as_indexed_json(options={}) - { - "id" => id, - "name" => name, - "additional_name" => additional_name, - "description" => description, - "repository_url" => repository_url, - "repository_contact" => repository_contact, - "repository_software" => repository_software, - "subject" => subject, - "cache_key" => cache_key, - "created" => created, - "updated" => updated - } - end - - def cache_key - "repositories/#{id}-#{updated}" - end - - def self.get_query_url(options={}) - if options[:id].present? - "#{ENV["RE3DATA_URL"]}/repository/#{options[:id]}" - else - params = { query: options.fetch(:query, nil) }.compact - url + "?" + URI.encode_www_form(params) - end - end - - def self.parse_data(result, options={}) - return nil if result.blank? || result['errors'] - - if options[:id].present? - item = result.body.fetch("data", {}).fetch("re3data", {}).fetch("repository", []) - return nil unless item.present? - - { data: parse_item(item) } - else - items = Array.wrap(result.body.fetch("data", []).fetch("list", {}).fetch("repository", [])) - - # sort by name - items = items.sort { |a, b| a.fetch("name") <=> b.fetch("name") } - - # pagination - page = options[:page] || {} - page[:number] = page[:number] && page[:number].to_i > 0 ? page[:number].to_i : 1 - page[:size] = page[:size] && (1..1000).include?(page[:size].to_i) ? page[:size].to_i : 25 - total = items.size - - items = Kaminari.paginate_array(items).page(page[:number]).per(page[:size]) - - meta = { total: total, - total_pages: items.total_pages, - page: page[:number].to_i } - - { data: parse_items(items), meta: meta } - end - end - - def self.url - "#{ENV["RE3DATA_URL"]}/repositories" - end -end diff --git a/app/serializers/client_serializer.rb b/app/serializers/client_serializer.rb index ea09c420f..d4fc1429b 100644 --- a/app/serializers/client_serializer.rb +++ b/app/serializers/client_serializer.rb @@ -3,7 +3,7 @@ class ClientSerializer set_key_transform :dash set_type :clients set_id :uid - cache_options enabled: true, cache_length: 24.hours + #cache_options enabled: true, cache_length: 24.hours attributes :name, :symbol, :year, :contact_name, :contact_email, :domains, :url, :created, :updated diff --git a/app/serializers/repository_serializer.rb b/app/serializers/repository_serializer.rb index 6df9c152a..65a80d7a8 100644 --- a/app/serializers/repository_serializer.rb +++ b/app/serializers/repository_serializer.rb @@ -2,7 +2,10 @@ class RepositorySerializer include FastJsonapi::ObjectSerializer set_key_transform :dash set_type :repositories - cache_options enabled: true, cache_length: 24.hours + #cache_options enabled: true, cache_length: 24.hours - attributes :name, :additional_name, :description, :repository_url, :repository_contact, :subject, :repository_software, :created, :updated + attributes :repository_name, :repository_url, :repository_contacts, :description, :certificates, :types, + :additional_names, :subjects, :content_types, :provider_types, + :keywords, :institutions, :data_accesses, :data_uploads, :data_upload_licenses, :pid_systems, + :apis, :pid_systems, :software, :start_date, :end_date, :created, :updated end diff --git a/config/routes.rb b/config/routes.rb index c8b557710..84be43a3c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,10 +48,6 @@ end resources :providers, constraints: { :id => /.+/ } - # re3data - resources :repositories, only: [:show, :index] - get "/repositories/:id/badge", to: "repositories#badge", format: :svg - resources :resource_types, path: 'resource-types', only: [:show, :index] # custom routes for maintenance tasks