class DogBiscuits::Importers::Authority

Attributes

authority[R]
authority_name[R]

Public Class Methods

new(authority_name) click to toggle source
# File lib/dog_biscuits/importers/authority.rb, line 9
def initialize(authority_name)
  @authority_name = authority_name
  @authority = Qa::LocalAuthority.find_or_create_by(name: authority_name) if valid_authority?
end

Public Instance Methods

create_record(label, uri = nil) click to toggle source

Create an authority record in the given local authority

@param label [String] the label @param uri [String] the uri, optional

# File lib/dog_biscuits/importers/authority.rb, line 25
def create_record(label, uri = nil)
  uri = construct_uri(label) if uri.blank?
  Qa::LocalAuthorityEntry.create(local_authority: @authority,
                                 label: cleanup_label(label),
                                 uri: uri)
rescue ActiveRecord::RecordNotUnique
  Rails.logger.warn("Duplicate record: #{label}")
end
valid_authority?() click to toggle source
# File lib/dog_biscuits/importers/authority.rb, line 14
def valid_authority?
  true if Qa::Authorities::Local.subauthority_for(@authority_name)
rescue Qa::InvalidSubAuthority
  Rails.logger.error("This sub-authority is not registered: #{@authority_name}")
  false
end

Private Instance Methods

cleanup_label(label) click to toggle source

Replace & with &

@param label [String] the label for cleanup @return [String] cleaned up label

# File lib/dog_biscuits/importers/authority.rb, line 40
def cleanup_label(label)
  label.gsub('&', '&')
end
construct_uri(label) click to toggle source

Create a default URI for the term

@param label [String] the label for cleanup @return [String] cleaned up label

# File lib/dog_biscuits/importers/authority.rb, line 58
def construct_uri(label)
  hostname = `hostname`.delete("\n")
  hostname = "example.com" if hostname.include?('localhost')
  "http://#{hostname}/#{@authority_name}/#{uri_ifiy_label(label)}"
end
uri_ifiy_label(label) click to toggle source

Create a version of the label for use as a uri:

replace all non-alphanumeric characters with whitespace
trim to 50 characters and replace whitespace with a dash

@param label [String] the label for uri-ification @return [String] uri-ified label

# File lib/dog_biscuits/importers/authority.rb, line 50
def uri_ifiy_label(label)
  cleanup_label(label).gsub(/[^\w\s\d]/, ' ')[0..50].parameterize
end