class DogBiscuits::TermsService

Public Class Methods

new(_authority = nil) click to toggle source
# File lib/dog_biscuits/services/terms_service.rb, line 5
def initialize(_authority = nil)
  @authority = self
end

Public Instance Methods

all() click to toggle source

Returns all items in the authority using preflabel_si to get exact match (string instead of token)

# File lib/dog_biscuits/services/terms_service.rb, line 22
def all
  sort_order = 'preflabel_si asc'
  parse_authority_response(
    ActiveFedora::SolrService.get(
      "inScheme_ssim:\"#{terms_id}\"",
      fl: 'id,preflabel_tesim,definition_tesim,broader_ssim',
      rows: 1000,
      sort: sort_order
    )
  )
end
find(id) click to toggle source

Find an authority term by id

@param id [String] for the requested authority term @return [Hash] all info about the authority

# File lib/dog_biscuits/services/terms_service.rb, line 38
def find(id)
  parse_authority_response(
    ActiveFedora::SolrService.get(
      "id:\"#{id}\"",
      fq: "inScheme_ssim:\"#{terms_id}\"",
      fl: 'id,preflabel_tesim,definition_tesim,broader_ssim',
      rows: 1
    )
  )
end
find_id(term) click to toggle source

Find the id for an authority term using preflabel_si to get exact match (string instead of token)

@param term [String] the authority term preflabel @return the term id

# File lib/dog_biscuits/services/terms_service.rb, line 68
def find_id(term)
  parse_terms_id_response(
    ActiveFedora::SolrService.get(
      "preflabel_si:\"#{term}\"",
      fq: "inScheme_ssim:\"#{terms_id}\"",
      fl: 'id',
      rows: 1
    )
  )
end
find_id_with_alts(term) click to toggle source

Find the id for an authority term using preflabel and alt_label (use preflabel_si to get exact match (string instead of token))

@param term [String] the authority term preflabel or alt_label @return the term id

# File lib/dog_biscuits/services/terms_service.rb, line 83
def find_id_with_alts(term)
  parse_terms_id_response(
    ActiveFedora::SolrService.get(
      "preflabel_si:\"#{term}\" OR altlabel_tesim:\"#{term}\")",
      fq: "inScheme_ssim:\"#{terms_id}\"",
      fl: 'id',
      rows: 1
    )
  )
end
find_label_string(id) click to toggle source

Find an authority term by id

@param id [String] for the requested authority term @return [String] the preflabel

# File lib/dog_biscuits/services/terms_service.rb, line 98
def find_label_string(id)
  parse_string(
    ActiveFedora::SolrService.get(
      "id:\"#{id}\"",
      fq: "inScheme_ssim:\"#{terms_id}\"",
      fl: 'preflabel_tesim',
      rows: 1
    )
  )
end
select_all_options() click to toggle source
# File lib/dog_biscuits/services/terms_service.rb, line 109
def select_all_options
  all.map { |e| [e[:label], e[:id]] }
end
terms_id() click to toggle source

Returns the ConceptScheme id for a given Scheme name

@return id of the ConceptScheme object

# File lib/dog_biscuits/services/terms_service.rb, line 12
def terms_id
  parse_terms_id_response(
    ActiveFedora::SolrService.get(
      "has_model_ssim:\"DogBiscuits::ConceptScheme\" AND preflabel_tesim:\"#{terms_list} \"",
      {}
    )
  )
end

Private Instance Methods

parse_authority_response(response) click to toggle source

Reformats the data received from the service

@param response [SolrResponse] for the Solr query @return [Hash] authority data

# File lib/dog_biscuits/services/terms_service.rb, line 119
def parse_authority_response(response)
  response['response']['docs'].map do |result|
    hash = {
      id: result['id']
    }

    hash[:label] = result['preflabel_tesim'].join if result['preflabel_tesim']
    hash[:definition] = result['definition_tesim'].join if result['definition_tesim']

    # Only add broader where it exists (ie. subjects)
    # Assumes only one broader
    if result['broader_ssim']
      hash[:broader_id] = result['broader_ssim'].join
      hash[:broader_label] = find_label_string(result['broader_ssim'].join).join
    end
    hash
  end
end
parse_string(response) click to toggle source

Parse the preflabel from the solr response

@param response [SolrResponse] for the Solr query @return [String] preflabel

# File lib/dog_biscuits/services/terms_service.rb, line 154
def parse_string(response)
  str = ''
  response['response']['docs'].map do |result|
    str = result['preflabel_tesim']
  end
  str
end
parse_terms_id_response(response) click to toggle source

Parse the id from the solr response

@param response [SolrResponse] for the Solr query @return [String] id

# File lib/dog_biscuits/services/terms_service.rb, line 142
def parse_terms_id_response(response)
  id = ''
  response['response']['docs'].map do |result|
    id = result['id']
  end
  id
end