Class: Aspera::Schema::Registry
- Inherits:
-
Object
- Object
- Aspera::Schema::Registry
- Includes:
- Singleton
- Defined in:
- lib/aspera/schema/registry.rb
Constant Summary collapse
- LOCATIONS =
{ spec: 'aspera/transfer/spec.schema.yaml', args: 'aspera/sync/args.schema.yaml', conf: 'aspera/sync/conf.schema.yaml', opts: 'aspera/cli/options.schema.yaml', aoc: 'aspera/schema/IBM Aspera on Cloud API-0.2.6-enhanced.yaml', faspex: 'aspera/schema/IBM Aspera Faspex API-5.0-enhanced.yaml', async_tables: 'aspera/schema/async_tables.yaml' }
- OPTIONS =
'opts'- TRANSFER_SPEC =
'spec'- SYNC_CONF =
'conf'- SYNC_ARGS =
'args'- AOC =
'aoc'- FASPEX =
'faspex'- ASYNC_TABLES =
'async_tables'- TRANSFER_INFO =
"#{OPTIONS}:components.schemas.TransferInfo"- REQ_BODY =
'.requestBody.content.application/json.schema'
Class Method Summary collapse
-
.instance ⇒ Registry
Returns the singleton instance of Registry.
- .known?(sym) ⇒ Boolean
-
.req_body(component, endpoint) ⇒ String
Get path to request body, no check if it exists.
Instance Method Summary collapse
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#reader(name_path) ⇒ Reader
Read schema from file or from cache.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
49 50 51 52 |
# File 'lib/aspera/schema/registry.rb', line 49 def initialize @cache = {} @main_folder = File.('../..', __dir__) end |
Class Method Details
.instance ⇒ Registry
Returns the singleton instance of Registry
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/aspera/schema/registry.rb', line 13 class Registry include Singleton class << self def known?(sym) LOCATIONS.key?(sym) end # Get path to request body, no check if it exists # @return [String] path to request body def req_body(component, endpoint) "#{component}:paths./#{endpoint}.requestBody.content.application/json.schema" end end LOCATIONS = { spec: 'aspera/transfer/spec.schema.yaml', args: 'aspera/sync/args.schema.yaml', conf: 'aspera/sync/conf.schema.yaml', opts: 'aspera/cli/options.schema.yaml', aoc: 'aspera/schema/IBM Aspera on Cloud API-0.2.6-enhanced.yaml', faspex: 'aspera/schema/IBM Aspera Faspex API-5.0-enhanced.yaml', async_tables: 'aspera/schema/async_tables.yaml' } OPTIONS = 'opts' TRANSFER_SPEC = 'spec' SYNC_CONF = 'conf' SYNC_ARGS = 'args' AOC = 'aoc' FASPEX = 'faspex' ASYNC_TABLES = 'async_tables' TRANSFER_INFO = "#{OPTIONS}:components.schemas.TransferInfo" REQ_BODY = '.requestBody.content.application/json.schema' def initialize @cache = {} @main_folder = File.('../..', __dir__) end # Read schema from file or from cache # @param name_path [String] one of the keys in LOCATIONS, with optional :<path> suffix # @return [Reader] schema def reader(name_path) name, path = name_path.split(':', 2) sym = name.to_sym Aspera.assert(Registry.known?(sym)){"schema: #{sym}"} spec_file = File.join(@main_folder, LOCATIONS[sym]) @cache[sym] = Yaml.safe_load(File.read(spec_file)) if spec_file.end_with?('.yaml') && !@cache.key?(sym) @cache[sym] = JSON.parse(File.read(spec_file)) if spec_file.end_with?('.json') && !@cache.key?(sym) reader = Reader.new(@cache[sym]) return reader unless path reader.dig(*path.split('.')) end end |
.known?(sym) ⇒ Boolean
17 18 19 |
# File 'lib/aspera/schema/registry.rb', line 17 def known?(sym) LOCATIONS.key?(sym) end |
.req_body(component, endpoint) ⇒ String
Get path to request body, no check if it exists
23 24 25 |
# File 'lib/aspera/schema/registry.rb', line 23 def req_body(component, endpoint) "#{component}:paths./#{endpoint}.requestBody.content.application/json.schema" end |
Instance Method Details
#reader(name_path) ⇒ Reader
Read schema from file or from cache
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/aspera/schema/registry.rb', line 57 def reader(name_path) name, path = name_path.split(':', 2) sym = name.to_sym Aspera.assert(Registry.known?(sym)){"schema: #{sym}"} spec_file = File.join(@main_folder, LOCATIONS[sym]) @cache[sym] = Yaml.safe_load(File.read(spec_file)) if spec_file.end_with?('.yaml') && !@cache.key?(sym) @cache[sym] = JSON.parse(File.read(spec_file)) if spec_file.end_with?('.json') && !@cache.key?(sym) reader = Reader.new(@cache[sym]) return reader unless path reader.dig(*path.split('.')) end |