From cd7c3df2a35d07e665957c562d445e1d4e2ac007 Mon Sep 17 00:00:00 2001 From: nruest Date: Wed, 18 Oct 2017 05:53:07 -0400 Subject: [PATCH] Add Replay link; Resolves #19 * Create an accessor that uses to Solr fields to query Time Travel API * Display accessor as a field * Create a test * Update Rubocop to ignore somethings; TODO create tickets, and come back to this later --- .rubocop.yml | 10 ++++++++++ app/models/concerns/warclight/solr_document.rb | 13 +++++++++++++ .../warclight/templates/catalog_controller.rb | 1 + .../concerns/warclight/solr_document_spec.rb | 17 +++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 spec/models/concerns/warclight/solr_document_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index bb8cbf6..21ffc33 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -17,6 +17,7 @@ Metrics/LineLength: Max: 120 Exclude: - 'Gemfile' + - 'spec/models/concerns/warclight/solr_document_spec.rb' Metrics/ModuleLength: Max: 120 @@ -34,12 +35,17 @@ Metrics/BlockLength: - 'spec/**/*' - 'lib/warclight/custom_document.rb' +Metrics/AbcSize: + Exclude: + - 'app/models/concerns/warclight/solr_document.rb' #Come back to this later + Performance/RegexpMatch: Enabled: false Rails/OutputSafety: Exclude: - 'app/controllers/concerns/warclight/field_config_helpers.rb' # html_safe needs to be called to mimic BL JOIN behavior + - 'app/models/concerns/warclight/solr_document.rb' RSpec/ExampleLength: Enabled: false @@ -50,6 +56,10 @@ RSpec/MultipleExpectations: RSpec/NestedGroups: Max: 4 +Rails/TimeZone: + Exclude: + - 'app/models/concerns/warclight/solr_document.rb' #Come back to this later + Style/Documentation: Exclude: - 'spec/**/*' diff --git a/app/models/concerns/warclight/solr_document.rb b/app/models/concerns/warclight/solr_document.rb index ccdc52f..c1d7044 100644 --- a/app/models/concerns/warclight/solr_document.rb +++ b/app/models/concerns/warclight/solr_document.rb @@ -5,5 +5,18 @@ module Warclight # Extends Blacklight::Solr::Document to provide Warclight specific behavior module SolrDocument extend Blacklight::Solr::Document + + def replay_link + time_travel_base_url = 'http://timetravel.mementoweb.org/api/json/' + time_travel_time_format = '%Y%m%d%H%M%S' + time_travel_time = (Time.parse(first(:crawl_date)).strftime time_travel_time_format).to_s + time_travel_request_url = time_travel_base_url + time_travel_time + '/' + first(:url).to_s + time_travel_request = URI(time_travel_request_url) + time_travel_response = Net::HTTP.get(time_travel_request) + time_travel_response_json = JSON.parse(time_travel_response) + replay_url = time_travel_response_json['mementos']['closest']['uri'][0] + replay_url_link = ''"#{replay_url}"' 🔗' + replay_url_link.html_safe + end end end diff --git a/lib/generators/warclight/templates/catalog_controller.rb b/lib/generators/warclight/templates/catalog_controller.rb index 949e014..97433af 100644 --- a/lib/generators/warclight/templates/catalog_controller.rb +++ b/lib/generators/warclight/templates/catalog_controller.rb @@ -80,6 +80,7 @@ class CatalogController < ApplicationController # solr fields to be displayed in the show (single result) view # The ordering of the field names is the order of the display config.add_show_field 'url', label: 'URL', helper_method: :url_to_link + config.add_show_field 'replay_url', label: 'Replay URL', accessor: :replay_link config.add_show_field 'resourcename', label: 'Resource Name', link_to_facet: true config.add_show_field 'host', label: 'Host', link_to_facet: true config.add_show_field 'institution', label: 'Institution', link_to_facet: true diff --git a/spec/models/concerns/warclight/solr_document_spec.rb b/spec/models/concerns/warclight/solr_document_spec.rb new file mode 100644 index 0000000..398b4f5 --- /dev/null +++ b/spec/models/concerns/warclight/solr_document_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Warclight::SolrDocument do + let(:document) { SolrDocument.new } + + describe '#replay_link' do + let(:document) do + SolrDocument.new(crawl_date: '2015-01-13T16:36:01Z', url: 'http://www.library.yorku.ca/cms/steacie/about-the-library/hackfest/') + end + + it 'writes a replay url based on memento time travel response' do + expect(document.replay_link).to eq 'https://digital.library.yorku.ca/wayback/20150113163601/http://www.library.yorku.ca/cms/steacie/about-the-library/hackfest/ 🔗' + end + end +end