Class: Qa::TermsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/qa/terms_controller.rb

Overview

This controller is used for all requests to all authorities. It will verify params and figure out which class to instantiate based on the “vocab” param. All the authority classes inherit from a super class so they implement the same methods.

Instance Method Summary collapse

Instance Method Details

#check_query_paramObject



61
62
63
# File 'app/controllers/qa/terms_controller.rb', line 61

def check_query_param
  head :not_found unless params[:q].present?
end

#check_vocab_paramObject



36
37
38
# File 'app/controllers/qa/terms_controller.rb', line 36

def check_vocab_param
  head :not_found unless params[:vocab].present?
end

#indexObject

If the subauthority supports it, return a list of all terms in the authority



13
14
15
16
17
18
19
20
# File 'app/controllers/qa/terms_controller.rb', line 13

def index
  cors_allow_origin_header(response)
  render json: begin
    @authority.all
  rescue NotImplementedError
    nil
  end
end

#init_authorityObject

rubocop:disable Metrics/MethodLength



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/qa/terms_controller.rb', line 40

def init_authority # rubocop:disable Metrics/MethodLength
  begin
    mod = authority_class.camelize.constantize
  rescue NameError
    logger.warn "Unable to initialize authority #{authority_class}"
    head :not_found
    return
  end
  begin
    @authority = if mod.is_a? Class
                   mod.new
                 else
                   raise Qa::MissingSubAuthority, "No sub-authority provided" if params[:subauthority].blank?
                   mod.subauthority_for(params[:subauthority])
                 end
  rescue Qa::InvalidSubAuthority, Qa::MissingSubAuthority => e
    logger.warn e.message
    head :not_found
  end
end

#searchObject

Return a list of terms based on a query



23
24
25
26
27
# File 'app/controllers/qa/terms_controller.rb', line 23

def search
  terms = @authority.method(:search).arity == 2 ? @authority.search(url_search, self) : @authority.search(url_search)
  cors_allow_origin_header(response)
  render json: terms
end

#showObject

If the subauthority supports it, return all the information for a given term



30
31
32
33
34
# File 'app/controllers/qa/terms_controller.rb', line 30

def show
  term = @authority.find(params[:id])
  cors_allow_origin_header(response)
  render json: term
end