Class: BlacklightInternetArchive::ResponseAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/blacklight_internet_archive/response_adapter.rb

Class Method Summary collapse

Class Method Details

.adapt_response(response_body, base_url) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/blacklight_internet_archive/response_adapter.rb', line 5

def self.adapt_response(response_body, base_url)
  response_body_string = convert_highlighting(response_body.to_s)
  res_data_json = JSON.parse(response_body_string)

  entities = res_data_json['results']['entities']
  # processed_entities = process_entities(entities, res_data_json['results']['searchedFacets'], base_url)
  # response_docs = { 'response' => { 'docs' => processed_entities } }

  # response_docs = EntityProcessor.run(res_data_json, base_url)
  response_docs = { 'response' => { 'docs' => EntityProcessor.run(res_data_json, base_url) } }



  response_docs.merge!('facet_counts' => { 'facet_queries' => {},
                                           'facet_fields' => reformat_facets(res_data_json), 'facet_dates' => {} })
  set_paging_stats(response_docs, res_data_json)
  response_docs
end

.convert_highlighting(response_string) ⇒ Object



24
25
26
27
28
# File 'lib/blacklight_internet_archive/response_adapter.rb', line 24

def self.convert_highlighting(response_string)
  response_string.gsub!('$high%', "<span class='highlight'>")
  response_string.gsub!('/$light%', '</span>')
  response_string
end

.reformat_facets(response_json) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/blacklight_internet_archive/response_adapter.rb', line 39

def self.reformat_facets(response_json)
  facets_hash = {}
  facets = response_json['results']['searchedFacets']
  facets.each do |f|
    key_name = f['id']
    facets_hash[key_name] = reformat_item(f['results'])
  end
  facets_hash
end

.reformat_item(item_arr) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/blacklight_internet_archive/response_adapter.rb', line 49

def self.reformat_item(item_arr)
  new_item_arr = []
  item_arr.each do |item_hash|
    new_item_arr << item_hash['name']
    new_item_arr << item_hash['count']
  end
  new_item_arr
end

.set_paging_stats(response_docs, res_data_json) ⇒ Object



31
32
33
34
35
36
# File 'lib/blacklight_internet_archive/response_adapter.rb', line 31

def self.set_paging_stats(response_docs, res_data_json)
  response_docs['response']['numFound'] = res_data_json['results']['totalResultCount']
  response_docs['response']['page'] = res_data_json['pageParams']['page']
  response_docs['response']['rows'] = res_data_json['pageParams']['pageSize']
  response_docs
end