Skip to content

Commit 9c886fa

Browse files
Rough WIP - broken tests
1 parent 0c0ae54 commit 9c886fa

File tree

8 files changed

+52
-0
lines changed

8 files changed

+52
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,16 @@ mode (e.g., `GDT=false` will still enable GDT features). Note that this is curre
120120
may have unexpected consequences if applied to other TIMDEX UI apps.
121121
- `GLOBAL_ALERT`: The main functionality for this comes from our theme gem, but when set the value will be rendered as
122122
safe html above the main header of the site.
123+
- `ORIGINS`: sets origins for CORS (currently used only for TACOS API calls).
123124
- `PLATFORM_NAME`: The value set is added to the header after the MIT Libraries logo. The logic and CSS for this comes from our theme gem.
124125
- `REQUESTS_PER_PERIOD` - number of requests that can be made for general throttles per `REQUEST_PERIOD`
125126
- `REQUEST_PERIOD` - time in minutes used along with `REQUESTS_PER_PERIOD`
126127
- `REDIRECT_REQUESTS_PER_PERIOD`- number of requests that can be made that the query string starts with our legacy redirect parameter to throttle per `REQUEST_PERIOD`
127128
- `REDIRECT_REQUEST_PERIOD`- time in minutes used along with `REDIRECT_REQUEST_PERIOD`
128129
- `SENTRY_DSN`: Client key for Sentry exception logging.
129130
- `SENTRY_ENV`: Sentry environment for the application. Defaults to 'unknown' if unset.
131+
- `TACOS_URL`: The GraphQL endpoint for the [TACOS API](https://github.com/mitlibraries/tacos/). When set, the
132+
application will log search terms to TACOS (and eventually return suggested resources that TACOS detects).
130133
- `TIMDEX_INDEX`: Name of the index, or alias, to provide to the GraphQL endpoint. Defaults to `nil` which will let TIMDEX determine the best index to use. Wildcard values can be set, for example `rdi*` would search any indexes that begin with `rdi` in the underlying OpenSearch instance behind TIMDEX.
131134
- `TIMDEX_SOURCES`: Comma-separated list of sources to display in the advanced-search source selection element. This
132135
overrides the default which is set in ApplicationHelper.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class TacosController < ApplicationController
2+
layout false
3+
4+
def analyze
5+
return unless ApplicationHelper.tacos_enabled?
6+
7+
Tacos.call(params[:q])
8+
end
9+
end

app/helpers/application_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module ApplicationHelper
2+
def tacos_enabled?
3+
ENV.fetch('TACOS_URL', false)
4+
end
5+
module_function :tacos_enabled?
6+
27
def timdex_sources
38
ENV.fetch('TIMDEX_SOURCES', timdex_source_defaults).split(',')
49
end

app/models/tacos.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Tacos
2+
TACOS_URL = ENV.fetch('TACOS_URL', nil).freeze
3+
ORIGINS = ENV.fetch('ORIGINS', nil).freeze
4+
5+
def self.call(term)
6+
tacos_http = HTTP.persistent(TACOS_URL)
7+
.headers(accept: 'application/json',
8+
'Content-Type': 'application/json',
9+
origin: ORIGINS)
10+
query = '{ "query": "{ logSearchEvent(searchTerm: \"' + clean_term(term) + '\", sourceSystem: \"use\" ) { phrase detectors { suggestedResources { title url } } } }" }'
11+
raw_response = tacos_http.timeout(http_timeout).post(TACOS_URL, body: query)
12+
JSON.parse(raw_response.to_s)
13+
end
14+
15+
def self.clean_term(term)
16+
term.gsub('"', '\'')
17+
end
18+
19+
def self.http_timeout
20+
ENV.fetch('TIMDEX_TIMEOUT', 6).to_f
21+
end
22+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<% return unless (tacos_enabled? and @enhanced_query[:q].present?) %>
2+
3+
<% data_url = "/analyze?q=#{URI.encode_www_form_component(@enhanced_query[:q])}" %>
4+
5+
<div class="tacos-container"
6+
data-controller="content-loader"
7+
data-content-loader-url-value=<%= data_url %>>
8+
</div>

app/views/search/results.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,6 @@
6868
<% end %>
6969
</div>
7070

71+
<%= render(partial: 'trigger_tacos') if tacos_enabled? %>
72+
7173
<%= javascript_include_tag "filters" %>

app/views/tacos/analyze.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- Result of TACOS analysis would go here -->

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
get 'issn', to: 'fact#issn'
99
get 'pmid', to: 'fact#pmid'
1010

11+
get 'analyze', to: 'tacos#analyze'
12+
1113
get 'record/(:id)',
1214
to: 'record#view',
1315
as: 'record',

0 commit comments

Comments
 (0)