class Arbetsformedlingen::API::OntologyClient

API client for ontology @see ontologi.arbetsformedlingen.se/ontology/v1/?url=swagger.json

Constants

BASE_URL

Base API URL

VALID_TYPES

Valid types

Attributes

request[R]

Public Class Methods

new(request: Request.new(base_url: BASE_URL)) click to toggle source

Initialize client

# File lib/arbetsformedlingen/api/ontology_client.rb, line 17
def initialize(request: Request.new(base_url: BASE_URL))
  @request = request
end

Public Instance Methods

concept(uuid) click to toggle source

GET /concept/:uuid} - Fetch a concept given an uuid @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 31
def concept(uuid)
  request.get("/concept/#{uuid}")
end
concept_relations(concept_uuid, type, offset: nil, limit: nil) click to toggle source

GET /concept/:uuid/related/:type - Fetches related concepts for the given uuid, returning only concepts of the given type @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 60
def concept_relations(concept_uuid, type, offset: nil, limit: nil)
  query = to_params(offset: offset, limit: limit)

  request.get("/concept/#{concept_uuid}/related/#{type}", query: query)
end
concept_terms(concept_uuid, offset: nil, limit: nil) click to toggle source

GET /concept/:uuid/terms - Fetches the terms for the given uuid @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 51
def concept_terms(concept_uuid, offset: nil, limit: nil)
  query = to_params(offset: offset, limit: limit)

  request.get("/concept/#{concept_uuid}/terms", query: query)
end
concepts(filter: nil, offset: nil, limit: nil, type: nil) click to toggle source

GET /concept - Fetches a list of concepts @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 23
def concepts(filter: nil, offset: nil, limit: nil, type: nil)
  query = to_params(filter: filter, offset: offset, limit: limit, type: type)

  request.get('/concept', query: query)
end
concepts_relations(uuids: [], names: [], limit: nil, type: nil) click to toggle source

GET /concept/related - Fetches related concepts for any number of concepts and/or uuids @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 38
def concepts_relations(uuids: [], names: [], limit: nil, type: nil)
  concepts = (
    names.map { |name| ['concept', name] } +
    uuids.map { |name| ['uui', name] }
  ).reject(&:empty?)

  query = to_params(limit: limit, type: type).to_a.concat(concepts)

  request.get('/concept/related', query: query)
end
terms(filter: nil, offset: nil, limit: nil, type: nil) click to toggle source

GET /terms - Fetches a list of terms @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 68
def terms(filter: nil, offset: nil, limit: nil, type: nil)
  query = to_params(filter: filter, offset: offset, limit: limit, type: type)

  request.get('/terms', query: query)
end
text_to_structure(text) click to toggle source

POST /text-to-structure - Analyzes a text and returns the concepts found @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 89
def text_to_structure(text)
  request.post('/text-to-structure', data: { text: text })
end
type_description(type, description) click to toggle source

GET /:type/:description - Redirects to the concepts UUID @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 76
def type_description(type, description)
  request.get("/#{type}/#{description}")
end
type_description_relations(type, description, relation_type) click to toggle source

GET /:type/:description/related/:totype - Redirects to the concepts UUID related concepts @return [Response]

# File lib/arbetsformedlingen/api/ontology_client.rb, line 83
def type_description_relations(type, description, relation_type)
  request.get("/#{type}/#{description}/related/#{relation_type}")
end

Private Instance Methods

to_params(filter: nil, offset: nil, limit: nil, type: nil) click to toggle source
# File lib/arbetsformedlingen/api/ontology_client.rb, line 95
def to_params(filter: nil, offset: nil, limit: nil, type: nil)
  if type && !VALID_TYPES.include?(type)
    raise(ArgumentError, "invalid type #{type}, valid types are #{VALID_TYPES.join(', ')}") # rubocop:disable Metrics/LineLength
  end

  {
    filter: filter,
    offset: offset,
    limit: limit,
    type: type,
  }.delete_if { |_k, v| v.nil? }
end