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
Class Method Summary collapse
-
.[](key = nil) ⇒ Service[], Service
Addressable method, indexed by service title.
-
.endpoint_regex(url) ⇒ String
check url structure for correct endpoint.
-
.from_url(url) ⇒ Hash
return a service from the supplied url.
-
.initialize ⇒ Object
Load references and services from disk.
-
.load_reference_file(file: nil, type: nil) ⇒ Void
Load the specified reference file into the container.
-
.load_references ⇒ Void
Load all available reference files into memory.
-
.load_services ⇒ Void
Convert the raw reference into a service and add it to the stack of available serrvices.
-
.raw_reference ⇒ Hash
Raw copy of the reference files.
-
.reference(key = nil) ⇒ Hash
Single reference from the raw collection.
-
.types ⇒ Symbol[]
list of possible types of services.
Class Method Details
.[](key = nil) ⇒ Service[], Service
Addressable method, indexed by service title
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
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
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 |
.initialize ⇒ Object
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
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_references ⇒ Void
Load all available reference files into memory
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_services ⇒ Void
Convert the raw reference into a service and add it to the stack of available serrvices
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_reference ⇒ Hash
Raw copy of the reference files
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.
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 |
.types ⇒ Symbol[]
list of possible types of services
122 123 124 |
# File 'lib/cts/mpx/services.rb', line 122 def types %i[web data ingest] end |