Class: BlacklightApi

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
app/models/blacklight_api.rb

Overview

BlacklightApi

Instance Method Summary collapse

Constructor Details

#initialize(request, args) ⇒ BlacklightApi

Returns a new instance of BlacklightApi.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/blacklight_api.rb', line 10

def initialize(request, args)
  defaults = {
    q: "*",
    page: 1,
    sort: "score+desc%2C+dc_title_sort+asc",
    rows: 20
  }
  @request = request
  @options = defaults.merge(args)
  append_facets(@options[:f], @options)
  append_daterange(@options[:f], @options)
  @options.compact!
end

Instance Method Details

#facetsObject



32
33
34
# File 'app/models/blacklight_api.rb', line 32

def facets
  fetch["included"]&.select { |s| s["type"] == "facet" }
end

#fetchObject



24
25
26
# File 'app/models/blacklight_api.rb', line 24

def fetch
  @fetch ||= self.class.get("#{@request}#{BLACKLIGHT_JSON_API}", query: @options)
end


44
45
46
# File 'app/models/blacklight_api.rb', line 44

def links
  fetch["links"]
end

#load_allObject



48
49
50
# File 'app/models/blacklight_api.rb', line 48

def load_all
  results.map { |result| Document.find_by(friendlier_id: result["id"]) }
end

#metaObject



40
41
42
# File 'app/models/blacklight_api.rb', line 40

def meta
  fetch["meta"]
end

#pluck(field) ⇒ Object



52
53
54
# File 'app/models/blacklight_api.rb', line 52

def pluck(field)
  load_all.pluck(field.to_sym)
end

#resultsObject



28
29
30
# File 'app/models/blacklight_api.rb', line 28

def results
  fetch["data"]
end

#sortsObject



36
37
38
# File 'app/models/blacklight_api.rb', line 36

def sorts
  fetch["included"].select { |s| s["type"] == "sort" }
end