ebsco-eds 0.2.8.pre → 0.2.9.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d7e8263996ac2590dc36a49873794a1740e86d0
4
- data.tar.gz: 2665e08e2b58fcc2097ef26888b3f514373d0973
3
+ metadata.gz: c181e960b6639886aa6ef318e40e78f9f8db0846
4
+ data.tar.gz: b56327ab29bb0032bc6914cc674f41cf330d3609
5
5
  SHA512:
6
- metadata.gz: 5838084f1135e91be2041fbe72a65b608744a2f2a3aa455297d59d339398b58a56be8580239d3ffa5fca7bdb2d6e43096b0e87300313cdaaa0e2b3852509629d
7
- data.tar.gz: edb0d711e2d58f84770c2374c8f4c533b4dec61fb10fdbfc336a22c9d2af4e72c468942c3bca1184553bc3b9d6c1c2f75249409b318e4de4cddc1e9e9d60bfa0
6
+ metadata.gz: 578554408924a621087faeb1d83ee418f7238ce9fc9fd171084853b9566620c03ccfeeeb20ae320fb08f7798b2959b7305d101862fa3205bd46521bfb4a19719
7
+ data.tar.gz: 9cc7a39fd98a1d6cb67e99d41e71c8befe0105b90655a2427be75d46f6dbabbdb02431afd9d75563af5a10ef58b3d1cd3458dba90dbc990838a4c3afe9b02ab0
@@ -228,8 +228,79 @@ module EBSCO
228
228
  # false
229
229
  # end
230
230
  # end
231
-
232
- end
231
+
232
+
233
+ # query-1=AND,volcano&sort=relevance&includefacets=y&searchmode=all&autosuggest=n&view=brief&resultsperpage=20&pagenumber=1&highlight=y
234
+ def to_query_string
235
+ qs = ''
236
+
237
+ # SEARCH CRITERIA:
238
+
239
+ # query
240
+ # if @SearchCriteria.Queries[0].has_key? :BooleanOperator
241
+ # qs << 'query=' + @SearchCriteria.Queries[0][:BooleanOperator]
242
+ # else
243
+ # qs << 'query=AND'
244
+ # end
245
+ # if @SearchCriteria.Queries[0].has_key? :FieldCode
246
+ # qs << ',' + @SearchCriteria.Queries[0][:FieldCode]
247
+ # end
248
+ qs << 'query=' + @SearchCriteria.Queries[0][:Term]
249
+
250
+ # mode
251
+ qs << '&searchmode=' + @SearchCriteria.SearchMode
252
+
253
+ # facets
254
+ qs << '&includefacets=' + @SearchCriteria.IncludeFacets
255
+
256
+ # sort
257
+ qs << '&sort=' + @SearchCriteria.Sort
258
+
259
+ # auto-suggest
260
+ qs << '&autosuggest=' + @SearchCriteria.AutoSuggest
261
+
262
+ # limiters
263
+ unless @SearchCriteria.Limiters.nil?
264
+ qs << '&limiter=' + @SearchCriteria.Limiters
265
+ end
266
+
267
+ # expanders
268
+ qs << '&expander=' + @SearchCriteria.Expanders.join(',')
269
+
270
+ # facet filters
271
+ unless @SearchCriteria.FacetFilters.nil?
272
+ qs << '&facetfilter=1,' + @SearchCriteria.FacetFilters
273
+ end
274
+
275
+ # related content
276
+ unless @SearchCriteria.RelatedContent.nil?
277
+ qs << '&relatedcontent=' + @SearchCriteria.RelatedContent.join(',')
278
+ end
279
+
280
+ # Retrieval Criteria
281
+ unless @RetrievalCriteria.View.nil?
282
+ qs << '&view=' + @RetrievalCriteria.View
283
+ end
284
+ unless @RetrievalCriteria.ResultsPerPage.nil?
285
+ qs << '&resultsperpage=' + @RetrievalCriteria.ResultsPerPage.to_s
286
+ end
287
+ unless @RetrievalCriteria.PageNumber.nil?
288
+ qs << '&pagenumber=' + @RetrievalCriteria.PageNumber.to_s
289
+ end
290
+ unless @RetrievalCriteria.Highlight.nil?
291
+ qs << '&highlight=' + @RetrievalCriteria.Highlight
292
+ end
293
+
294
+ unless @Actions.nil?
295
+ @Actions.each do |action|
296
+ qs << '&action=' + action
297
+ end
298
+ end
299
+ qs
300
+
301
+ end
302
+
303
+ end
233
304
 
234
305
  class SearchCriteria
235
306
  include JSONable
@@ -1,5 +1,6 @@
1
1
  require 'ebsco/eds/record'
2
2
  require 'yaml'
3
+ require 'cgi'
3
4
 
4
5
  module EBSCO
5
6
 
@@ -9,7 +10,7 @@ module EBSCO
9
10
  class Results
10
11
 
11
12
  # Raw results as Hash
12
- attr_reader :results
13
+ attr_accessor :results
13
14
  # Array of EBSCO::EDS::Record results
14
15
  attr_accessor :records
15
16
  # Array of EBSCO::EDS::Record Research Starters
@@ -19,13 +20,15 @@ module EBSCO
19
20
 
20
21
  attr_accessor :stat_total_hits
21
22
 
23
+ attr_reader :raw_options
24
+
22
25
  # Creates search results from the \EDS API search response. It includes information about the results and a list
23
26
  # of Record items.
24
- def initialize(search_results, additional_limiters = {}, raw_options = {})
27
+ def initialize(search_results, additional_limiters = {}, options = {})
25
28
 
26
29
  @results = search_results
27
30
  @limiters = additional_limiters
28
- @raw_options = raw_options
31
+ @raw_options = options
29
32
 
30
33
  # convert all results to a list of records
31
34
  @records = []
@@ -286,7 +289,21 @@ module EBSCO
286
289
  # "AutoSuggest"=>"n"
287
290
  # }
288
291
  def search_criteria
289
- @results['SearchRequest']['SearchCriteria']
292
+ if @results['SearchRequestGet']['QueryString']
293
+ params = CGI::parse(@results['SearchRequestGet']['QueryString'])
294
+ sc = {}
295
+ sc['SearchMode'] = params['searchmode'].nil? ? 'all' : params['searchmode'][0].to_s
296
+ sc['IncludeFacets'] = params['includefacets'].nil? ? 'y' : params['includefacets'][0].to_s
297
+ sc['Sort'] = params['sort'].nil? ? 'relevance' : params['sort'][0].to_s
298
+ sc['AutoSuggest'] = params['autosuggest'].nil? ? 'n' : params['autosuggest'][0].to_s
299
+ sc['Expanders'] = params['expander'].nil? ? [] : params['expander'][0].to_s.split(',')
300
+ sc['RelatedContent'] = params['relatedcontent'].nil? ? [] : params['relatedcontent'][0].to_s.split(',')
301
+ query1 = params['query-1'][0].to_s.split(',')
302
+ sc['Queries'] = [{'BooleanOperator'=>query1[0], 'Term'=>query1[1]}]
303
+ sc
304
+ else
305
+ @results['SearchRequest']['SearchCriteria']
306
+ end
290
307
  end
291
308
 
292
309
  # Search criteria actions applied.
@@ -304,24 +321,43 @@ module EBSCO
304
321
  # ==== Example
305
322
  # {"View"=>"brief", "ResultsPerPage"=>20, "PageNumber"=>1, "Highlight"=>"y"}
306
323
  def retrieval_criteria
307
- @results['SearchRequest']['RetrievalCriteria']
324
+
325
+ # GET METHOD: requires the hash to be built from scratch
326
+ rc = {}
327
+ if @results['SearchRequestGet']
328
+ if @results['SearchRequestGet']['QueryString']
329
+ params = CGI::parse(@results['SearchRequestGet']['QueryString'])
330
+ rc['PageNumber'] = params['pagenumber'].nil? ? 1 : params['pagenumber'][0].to_i
331
+ rc['ResultsPerPage'] = params['resultsperpage'].nil? ? 20 : params['resultsperpage'][0].to_i
332
+ rc['Highlight'] = params['highlight'].nil? ? 'n' : params['highlight'][0].to_s
333
+ rc['View'] = params['view'].nil? ? 'brief' : params['view'][0].to_s
334
+ end
335
+ else
336
+ if @results['SearchRequest']
337
+ if @results['SearchRequest']['RetrievalCriteria']
338
+ rc = @results['SearchRequest']['RetrievalCriteria']
339
+ end
340
+ end
341
+ end
342
+ rc
343
+
308
344
  end
309
345
 
310
346
  # Queries used to produce the results. Returns an array of query hashes.
311
347
  # ==== Example
312
348
  # [{"BooleanOperator"=>"AND", "Term"=>"volcano"}]
313
349
  def search_queries
314
- @results['SearchRequest']['SearchCriteria']['Queries']
350
+ search_criteria['Queries']
315
351
  end
316
352
 
317
353
  # Current page number for the results. Returns an integer.
318
354
  def page_number
319
- @results['SearchRequest']['RetrievalCriteria']['PageNumber'] || 1
355
+ retrieval_criteria['PageNumber'] || 1
320
356
  end
321
357
 
322
358
  # Results per page. Returns an integer.
323
359
  def results_per_page
324
- @results['SearchRequest']['RetrievalCriteria']['ResultsPerPage'] || 20
360
+ retrieval_criteria['ResultsPerPage'] || 20
325
361
  end
326
362
 
327
363
  # List of facets applied to the search.
@@ -9,6 +9,7 @@ require 'json'
9
9
  require 'active_support'
10
10
  require 'faraday_eds_middleware'
11
11
  require 'ebsco/eds/configuration'
12
+ require 'digest/md5'
12
13
 
13
14
  module EBSCO
14
15
 
@@ -188,16 +189,17 @@ module EBSCO
188
189
  #
189
190
  # results = session.search({query: 'abraham lincoln', results_per_page: 5, related_content: ['rs','emp']})
190
191
  # results = session.search({query: 'volcano', results_per_page: 1, publication_id: 'eric', include_facets: false})
191
- def search(options = {}, add_actions = false)
192
-
192
+ def search(options = {}, add_actions = false, increment_page = true)
193
193
  # use existing/updated SearchOptions
194
194
  if options.empty?
195
195
  if @search_options.nil?
196
196
  @search_results = EBSCO::EDS::Results.new(empty_results)
197
197
  else
198
- _response = do_request(:post, path: @config[:search_url], payload: @search_options)
198
+ _response = do_request(:post, path: '/edsapi/rest/Search', payload: @search_options)
199
199
  @search_results = EBSCO::EDS::Results.new(_response, @info.available_limiters, options)
200
- @current_page = @search_results.page_number
200
+ if increment_page
201
+ @current_page = @search_results.page_number
202
+ end
201
203
  @search_results
202
204
  end
203
205
  else
@@ -208,9 +210,11 @@ module EBSCO
208
210
  if @search_options.nil? || !add_actions
209
211
  @search_options = EBSCO::EDS::Options.new(options, @info)
210
212
  end
211
- _response = do_request(:post, path: @config[:search_url], payload: @search_options)
213
+ _response = do_request(:post, path: '/edsapi/rest/Search', payload: @search_options)
212
214
  @search_results = EBSCO::EDS::Results.new(_response, @info.available_limiters, options)
213
- @current_page = @search_results.page_number
215
+ if increment_page
216
+ @current_page = @search_results.page_number
217
+ end
214
218
  @search_results
215
219
  else
216
220
  @search_results = EBSCO::EDS::Results.new(empty_results)
@@ -218,6 +222,8 @@ module EBSCO
218
222
  end
219
223
  end
220
224
 
225
+ # todo: need a lookahead and lookbehind option for blacklight next/previous detail page - do search but don't change the current_page
226
+
221
227
  # :category: Search & Retrieve Methods
222
228
  # Performs a simple search. All other search options assume default values.
223
229
  #
@@ -250,7 +256,9 @@ module EBSCO
250
256
  #
251
257
  def retrieve(dbid:, an:, highlight: nil, ebook: 'ebook-pdf')
252
258
  payload = { DbId: dbid, An: an, HighlightTerms: highlight, EbookPreferredFormat: ebook }
253
- retrieve_response = do_request(:post, path: @config[:retrieve_url], payload: payload)
259
+ # retrieve_response = do_request(:post, path: @config[:retrieve_url], payload: payload)
260
+ retrieve_params = "?an=#{an}&dbid=#{dbid}&ebookpreferredformat=#{ebook}"
261
+ retrieve_response = do_request(:get, path: @config[:retrieve_url] + retrieve_params)
254
262
  record = EBSCO::EDS::Record.new(retrieve_response)
255
263
  # puts 'RECORD: ' + record.pretty_inspect
256
264
  record
@@ -258,21 +266,64 @@ module EBSCO
258
266
 
259
267
  # Create a result set with just the record before and after the current detailed record
260
268
  def solr_retrieve_previous_next(options = {})
261
- records = []
262
- hits = search(options).stat_total_hits
263
- # don't try to return a previous result if it's the first record
264
- if options['previous-next-index'].to_i > 1
265
- options.update(:results_per_page => 1,
266
- 'page' => (options['previous-next-index'].to_i) - 1)
267
- records.push search(options).records.first
269
+
270
+ rid = options['previous-next-index']
271
+
272
+ # set defaults if missing
273
+ if options['page'].nil?
274
+ options['page'] = '1'
268
275
  end
269
- options.update(:results_per_page => 1,
270
- 'page' => (options['previous-next-index'].to_i) + 1)
271
- records.push search(options).records.first
272
- r = empty_results(hits)
276
+ if options['per_page'].nil?
277
+ options['per_page'] = '20'
278
+ end
279
+
280
+ rpp = options['per_page'].to_i
281
+
282
+ # determine result page and update options
283
+ goto_page = rid / rpp
284
+ if (rid % rpp) > 0
285
+ goto_page += 1
286
+ end
287
+ options['page'] = goto_page.to_s
288
+ pnum = options['page'].to_i
289
+
290
+ max = rpp * pnum
291
+ min = max - rpp + 1
292
+ result_index = rid - min
293
+ cached_results = search(options)
294
+
295
+ # last result in set, get next result
296
+ if rid == max
297
+ @search_options.add_actions("GoToPage(#{cached_results.page_number+1})", @info)
298
+ next_result_set = search({}, true, false)
299
+ result_next = next_result_set.records.first
300
+ else
301
+ result_next = cached_results.records[result_index+1]
302
+ end
303
+
304
+ if result_next.nil?
305
+ puts 'ERROR: result_next is nil'
306
+ end
307
+
308
+ # first result in set that's not the very first result, get previous result
309
+ if result_index == 0 and rid != 1
310
+ @search_options.add_actions("GoToPage(#{cached_results.page_number-1})", @info)
311
+ previous_result_set = search({}, true, false)
312
+ result_prev = previous_result_set.records.last
313
+ else
314
+ result_prev = cached_results.records[result_index-1]
315
+ end
316
+
317
+ if result_prev.nil?
318
+ puts 'ERROR: result_prev is nil'
319
+ end
320
+
321
+ # return json result set with just the previous and next records in it
322
+ r = empty_results(cached_results.stat_total_hits)
273
323
  results = EBSCO::EDS::Results.new(r)
274
- results.records = records
324
+ results.records = [result_prev, result_next]
275
325
  results.to_solr
326
+
276
327
  end
277
328
 
278
329
  def solr_retrieve_list(list: [], highlight: nil)
@@ -608,12 +659,27 @@ module EBSCO
608
659
  resp = connection.send(method) do |req|
609
660
  case method
610
661
  when :get
662
+ unless payload.nil?
663
+ qs = CGI.escape(payload.to_query_string)
664
+ path << '?' + qs
665
+ end
611
666
  req.url path
612
667
  when :post
613
- req.url path
614
668
  unless payload.nil?
615
- req.body = JSON.generate(payload)
669
+ json_payload = JSON.generate(payload)
670
+ # add a cache_id to post search requests to make them cacheable
671
+ if path == '/edsapi/rest/Search'
672
+ # replacements to avoid duplicate caches
673
+ json_payload = json_payload.gsub(/"PageNumber":[0-9]+,/,'')
674
+ json_payload = json_payload.gsub(/"ResultsPerPage":"([0-9]+)"/,'"ResultsPerPage":\1')
675
+ json_payload = json_payload.gsub(/"Actions":\[]/,'"Actions":["GoToPage(1)"]')
676
+ # puts 'CHECKSUM PAYLOAD: ' + json_payload.inspect
677
+ checksum = Digest::MD5.hexdigest json_payload
678
+ path << '?cache_id=' + checksum
679
+ end
680
+ req.body = json_payload
616
681
  end
682
+ req.url path
617
683
  else
618
684
  raise EBSCO::EDS::ApiError, "EBSCO API error: Method #{method} not supported for endpoint #{path}"
619
685
  end
@@ -1,5 +1,5 @@
1
1
  module EBSCO
2
2
  module EDS
3
- VERSION = '0.2.8.pre'
3
+ VERSION = '0.2.9.pre'
4
4
  end
5
5
  end
@@ -7,6 +7,8 @@ module Faraday
7
7
 
8
8
  INFO_URI = URI.parse('https://eds-api.ebscohost.com/edsapi/rest/Info')
9
9
  AUTH_URI = URI.parse('https://eds-api.ebscohost.com/authservice/rest/uidauth')
10
+ SEARCH_URI = URI.parse('https://eds-api.ebscohost.com/edsapi/rest/Search?')
11
+ RETRIEVE_URI = URI.parse('https://eds-api.ebscohost.com/edsapi/rest/Retrieve?')
10
12
 
11
13
  def initialize(app, *args)
12
14
  super(app)
@@ -43,6 +45,7 @@ module Faraday
43
45
  end
44
46
 
45
47
  def cache_response(env)
48
+ #puts 'ENV: ' + env.inspect
46
49
  return unless cacheable?(env) && !env.request_headers['x-faraday-eds-cache']
47
50
 
48
51
  info "Cache WRITE: #{key(env)}"
@@ -59,12 +62,24 @@ module Faraday
59
62
  info "#{uri} - Setting custom expires: #{custom_expires_in}"
60
63
  end
61
64
 
65
+ if uri.request_uri.start_with?(SEARCH_URI.request_uri)
66
+ custom_expires_in = 1800 # 30 minutes
67
+ info "#{uri} - Setting custom expires: #{custom_expires_in}"
68
+ end
69
+
70
+ if uri.request_uri.start_with?(RETRIEVE_URI.request_uri)
71
+ custom_expires_in = 1800 # 30 minutes
72
+ info "#{uri} - Setting custom expires: #{custom_expires_in}"
73
+ end
74
+
62
75
  @store.write(key(env), env, expires_in: custom_expires_in)
63
76
  end
64
77
 
65
78
  def cacheable?(env)
66
79
  uri = env.url
67
- if uri == AUTH_URI || uri == INFO_URI
80
+ if uri == AUTH_URI || uri == INFO_URI ||
81
+ uri.request_uri.start_with?(SEARCH_URI.request_uri) ||
82
+ uri.request_uri.start_with?(RETRIEVE_URI.request_uri)
68
83
  info "CACHEABLE URI: #{uri}"
69
84
  true
70
85
  else
@@ -74,6 +89,7 @@ module Faraday
74
89
  end
75
90
 
76
91
  def cached_response(env)
92
+
77
93
  if cacheable?(env) && !env.request_headers['x-faraday-eds-cache']
78
94
  response_env = @store.fetch(key(env))
79
95
  end
@@ -83,9 +99,6 @@ module Faraday
83
99
  else
84
100
  info "Cache MISS: #{key(env)}"
85
101
  end
86
-
87
- # info "CACHE: #{response_env.inspect}"
88
-
89
102
  response_env
90
103
  end
91
104
 
@@ -99,7 +112,6 @@ module Faraday
99
112
 
100
113
  def initialize_store
101
114
  return unless @store.is_a? Symbol
102
-
103
115
  require 'active_support/cache'
104
116
  @store = ActiveSupport::Cache.lookup_store(@store, @store_options)
105
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ebsco-eds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8.pre
4
+ version: 0.2.9.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bill McKinney
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-07-27 00:00:00.000000000 Z
12
+ date: 2017-07-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday