Module: Cts::Mpx::Services

Defined in:
lib/cts/mpx/services.rb,
lib/cts/mpx/services/web.rb,
lib/cts/mpx/services/data.rb,
lib/cts/mpx/services/ingest.rb

Overview

Container style module for the collection of services available.

Defined Under Namespace

Modules: Data, Ingest, Web

Class Method Summary collapse

Class Method Details

.[](key = nil) ⇒ Service[], Service

Addressable method, indexed by service title

Parameters:

  • key (String) (defaults to: nil)

    service title to look up the service object

Returns:

  • (Service[])

    if no key, return the entire array of services

  • (Service)

    a service

Raises:

  • (ArgumentError)

    if the key is not a service name

  • (ArgumentError)

    if the key is not a string



13
14
15
16
17
18
19
20
21
# File 'lib/cts/mpx/services.rb', line 13

def [](key = nil)
  return @services unless key
  raise 'key must be a string' unless key.is_a? String

  service = @services.find { |e| e.name == key }
  raise "#{key} must be a service name." unless service

  service
end

.endpoint_regex(url) ⇒ String

check url structure for correct endpoint

Parameters:

  • url (String)

    to parse for a match

Returns:

  • (String)

    results of regex matching



42
43
44
# File 'lib/cts/mpx/services.rb', line 42

def endpoint_regex(url)
  /data\/([a-zA-Z]*\/Field)|data\/([a-zA-Z]*)\//.match(url)&.[](1, 2)&.select { |e| e unless e.nil? }[0]
end

.from_url(url) ⇒ Hash

return a service from the supplied url

Parameters:

  • url (String)

    url to parse

Returns:

  • (Hash)

    including service and endpoint as string.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cts/mpx/services.rb', line 26

def from_url(url)
  type = 'data' if url.include? 'data'
  uri = URI.parse url

  service = Services[].find { |s| uri.host.include?(s.uri_hint) if s.uri_hint && s.type == type }
  return nil unless service

  {
    service:  service.name,
    endpoint: endpoint_regex(url)
  }
end

.initializeObject

Load references and services from disk.



47
48
49
50
# File 'lib/cts/mpx/services.rb', line 47

def initialize
  load_references
  load_services
end

.load_reference_file(file: nil, type: nil) ⇒ Void

Load the specified reference file into the container

Parameters:

  • file (<Type>) (defaults to: nil)

    file to load

  • type (<Type>) (defaults to: nil)

    type of service the file contains

Returns:

  • (Void)

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
# File 'lib/cts/mpx/services.rb', line 56

def load_reference_file(file: nil, type: nil)
  raise ArgumentError, 'type must be supplied' unless type
  raise ArgumentError, 'file must be supplied' unless file

  @raw_reference.store(type, Driver.parse_json(File.read(file)))
  true
end

.load_referencesVoid

Load all available reference files into memory

Returns:

  • (Void)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cts/mpx/services.rb', line 66

def load_references
  @raw_reference = {}
  Services.types.each do |type|
    gemdir = if Gem::Specification.find_all.map(&:name).include? 'cts-mpx'
               Gem::Specification.find_by_name('cts-mpx').gem_dir
             else
               # :nocov:
               Dir.pwd
               # :nocov:
             end

    Services.load_reference_file(file: "#{gemdir}/config/#{type}_services.json", type: type.to_s)
  end
end

.load_servicesVoid

Convert the raw reference into a service and add it to the stack of available serrvices

Returns:

  • (Void)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cts/mpx/services.rb', line 83

def load_services
  @services = []
  raw_reference.each do |type, services|
    services.each do |name, data|
      s = Driver::Service.new
      s.name = name
      s.uri_hint = data['uri_hint']
      s.path = data['path']
      s.form = data['form']
      s.schema = data['schema']
      s.search_schema = data['search_schema']
      s.read_only = data['read_only'] ? true : false
      s.endpoints = data['endpoints']
      s.type = type
      s.instance_variable_set :@url, data['url'] if data['url']
      @services.push s
    end
  end
end

.raw_referenceHash

Raw copy of the reference files

Returns:

  • (Hash)

    key is name, value is the data



105
106
107
# File 'lib/cts/mpx/services.rb', line 105

def raw_reference
  @raw_reference
end

.reference(key = nil) ⇒ Hash

Single reference from the raw collection.

Parameters:

  • key (String) (defaults to: nil)

    service title

Returns:

  • (Hash)

    values of the reference



112
113
114
115
116
117
118
# File 'lib/cts/mpx/services.rb', line 112

def reference(key = nil)
  return @raw_reference unless key
  raise 'key must be a string' unless key.is_a? String
  raise "#{key} is not in the reference library." unless @raw_reference.include? key

  @raw_reference[key]
end

.typesSymbol[]

list of possible types of services

Returns:

  • (Symbol[])

    List of all types



122
123
124
# File 'lib/cts/mpx/services.rb', line 122

def types
  %i[web data ingest]
end