Class: Sbuilder::ApiLoaderFacade

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils::MyLogger
Defined in:
lib/sbuilder/facade/api_loader_facade.rb

Overview

Facade to Sbuilder for api loaders.

Class implements following service groups

  • for controller to configure the facade (this part is not visible for api)

  • for api loader to read/cache api configuration, to create model objects, and build sbuilder model

Constant Summary collapse

PROGNAME =

progname for logger

nil

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Construtor collapse

Prepare Facade collapse

Cache API Metadata collapse

Add to Sbuilder collapse

Construct Model Objects collapse

domain collapse

Access framework context collapse

Miscellaneous Operations collapse

Methods included from Utils::MyLogger

#getLogger, #logfile

Constructor Details

#initialize(factory, options = {}) ⇒ ApiLoaderFacade




57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 57

def initialize( factory, options = {} )
  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__} initialized" )

  # collaborators init
  setFactory( factory )
  @controller = nil
  
  # configuration init
  configureInfrastructureService( false )
  configureInterfaceService( true )
  
  @namespace = nil
  
  @cacheFile = nil
  @options = options
  
end

Instance Attribute Details

#cacheFileObject (readonly)

Returns the value of attribute cacheFile.



30
31
32
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 30

def cacheFile
  @cacheFile
end

#controllerObject

get sets when load starts



36
37
38
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 36

def controller
  @controller
end

#factoryObject (readonly)

facade collaborators



35
36
37
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 35

def factory
  @factory
end

#infrastructureServicesObject (readonly)



19
20
21
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 19

def infrastructureServices
  @infrastructureServices
end

#interfaceServicesObject (readonly)



22
23
24
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 22

def interfaceServices
  @interfaceServices
end

#namespaceObject (readonly)

string to prefix to all definitions (default ‘nil’ ie. no prefix). Allow homonymous API definition names ie. same name in separata definitions.



28
29
30
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 28

def namespace
  @namespace
end

Instance Method Details

#addParameter(paramSet, parameter, domain_name = nil) ⇒ Object

Add paramter to parameterSet

Parameters:



302
303
304
305
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 302

def addParameter( paramSet, parameter, domain_name=nil )
  paramSet.addParameter( parameter )
  configParameterDomain( parameter, domain_name ) if domain_name      
end

#addResponseParameter(paramSet, parameter, domain_name = nil) ⇒ Object

Add paramter to parameterSet response

Parameters:

  • paramSet (ParamSet_If)

    of the response where to add to

  • parameter (Parameter)

    to add

  • name (String)

    of domain to assign to parameter



316
317
318
319
320
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 316

def addResponseParameter( paramSet, parameter, domain_name=nil )
  responseParamSet = paramSet.response      
  responseParamSet.addParameter( parameter )
  configParameterDomain( parameter, domain_name ) if domain_name
end

#baseMetatypesHash

Return array of base metatypes in sbuilder context

Parameters:

  • baseMetatype (Hash)

    a customizable set of options

Returns:

  • (Hash)

    baseMetatypes



393
394
395
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 393

def baseMetatypes
  controller.baseMetatypes
end

#cache_line(line, what = nil) ⇒ Object

Service for loader to save ‘line’ into cache

Parameters:

  • line (String)

    content to store

  • what (String) (defaults to: nil)

    sub-content in cache



167
168
169
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 167

def cache_line( line, what=nil )
  Sbuilder::Utils::FileIo.cache_write( get_cachefile(what), line ) if doCache? && line
end

#configParameterDomain(parameter, domain_name) ⇒ Object

Set domain_name to parameter

Parameters:

  • parameter (Parameter)

    to add

  • name (String)

    of domain to assign to parameter private



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 328

def configParameterDomain( parameter, domain_name )

  if !parameter.is_a?(Parameter_Dom) then
    msg = <<-EOS
      Only domain parameters can assgined domain

      In this case domain '#{domain_name}' was assigned to #{parameter.class}
      EOS
    @logger.error "#{__method__}, msg"
    raise PluginException, msg
  end
  # call static busines rule to set 'domain' on 'parameter' in model context
  ResolverRuleMatch.setDomain( parameter, domain_name, controller.model )
  
end

#configureInfrastructureService(infrastructureServices) ⇒ Object

Tag all interfaces loaded using this facede with property ‘infrastructureServices’. See attriute #infrastructureServices

Parameters:

  • infrastructureServices (Boolean)

    true/false set on all interfaces



99
100
101
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 99

def configureInfrastructureService( infrastructureServices )
  @infrastructureServices = infrastructureServices
end

#configureInterfaceService(interfaceServices) ⇒ Object

Tag all interfaces loaded using this facadate with property ‘interfaceService’. See attribute #interfaceServices



106
107
108
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 106

def configureInterfaceService( interfaceServices )
  @interfaceServices = interfaceServices
end

#configureNamespace(namespace) ⇒ Object

Prefix all definititions with “##namespace.” string

Parameters:

  • namespace (String)

    to prefix to all definitions



115
116
117
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 115

def configureNamespace( namespace )
  @namespace = namespace
end

#createLogger(logname) ⇒ Logger

Returns new logger object.

Parameters:

  • logname (String)

    progname to set on logger

Returns:

  • (Logger)

    new logger object



413
414
415
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 413

def createLogger( logname )
  getLogger( logname, @options )
end

#defineDomain(domainName, domainValues = nil) ⇒ Object

Add domainName to sbuilder. Pass domainValues to controller using setApplicationDomainValues api if defined.

Parameters:

  • domainName (String)

    to define

  • domainValues (String:Array) (defaults to: nil)

    e.g. literals in application



375
376
377
378
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 375

def defineDomain( domainName, domainValues = nil )
  controller.domainEncountered( domainName )
  controller.setApplicationDomainValues( domainName, domainValues ) if domainValues && domainValues.is_a?( Array ) && domainValues.any?
end

#doCache?Boolean

Returns true if cache i.e. when cacheFile defined.

Returns:

  • (Boolean)

    true if cache i.e. when cacheFile defined



172
173
174
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 172

def doCache?
  !cacheFile.nil?
end

#get_cached(what = nil) ⇒ String

Read previously cached content (@see cache_line)

Parameters:

  • what (String) (defaults to: nil)

    sub-content in cache

Returns:

  • (String)

    cached content (nil if no cached content



160
161
162
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 160

def get_cached( what=nil )
  Sbuilder::Utils::FileIo.cache_read( get_cachefile(what)  ) if doCache?
end

#getCacheDirString

Return cache directory location from opitions

Returns:

  • (String)

    path to cache -directory



179
180
181
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 179

def getCacheDir
  controller.getCacheDir
end

#modelDefinition(newDefinition) ⇒ Object

Add ‘newDefinition’ to model

Parameters:



191
192
193
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 191

def modelDefinition( newDefinition )
  controller.modelDefinition( newDefinition ) 
end

#modelInterface(newInterface) ⇒ Object

Add ‘newInterface’ to model

Parameters:



197
198
199
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 197

def modelInterface( newInterface )
  controller.modelInterface( newInterface ) 
end

#newDefinition(definitionName, isArray = false) ⇒ Sbuilder::ParamSetDef

Returns New definition object created.

Parameters:

  • definitionName (String)

    name of defintition to create

  • isArray (Boolean) (defaults to: false)

    true if array defined

Returns:



211
212
213
214
215
216
217
218
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 211

def newDefinition( definitionName, isArray=false )
  paramSet =  createParamSet( Sbuilder::Constants::DEFINITION )
  # optionally prefix within namespace
  definitionName = namespacedReferenceName( definitionName )
  paramSet.setDefinition( definitionName )
  paramSet.setIsArray(  isArray )
  return paramSet
end

#newFunctionDefinition(definitionName, isArray = false) ⇒ Sbuilder::ParamSetDefFunc

Create a function definition (parameter set)

Parameters:

  • definitionName (String)

    name of defintition to create

  • isArray (Boolean) (defaults to: false)

    true if array defined

Returns:



227
228
229
230
231
232
233
234
235
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 227

def newFunctionDefinition( definitionName, isArray=false )

  paramSet =  createParamSet( Sbuilder::Constants::DEFINITION_FUNCTION )
  # optionally prefix within namespace
  definitionName = namespacedReferenceName( definitionName )
  paramSet.setDefinition( definitionName )
  paramSet.setIsArray(  isArray )
  return paramSet
end

#newInterface(path, op, optionReturn = nil, optionInterface = nil) ⇒ Sbuilder::ParamSet_If

Create new interface paramtereter set (and a response parameter set)

response, nil uses facade #infrastructureServices

process for the service, nil uses facade default #interfaceServices

to ‘response’ (ie. definiting return from a operation)

for request. Use #response to access response object.

Parameters:

  • optionReturn (Boolean, nil) (defaults to: nil)

    true allow inteface to return

  • optionInterface (Boolean, nil) (defaults to: nil)

    true generates interface

  • path (String)

    of restfull interface operation

  • op (String)

    rest operation (get, put, post,…), defaults

Returns:



254
255
256
257
258
259
260
261
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 254

def newInterface( path, op, optionReturn=nil, optionInterface=nil  )
  @logger.info "#{__method__}: path=#{path}, op=#{op}, optionReturn=#{optionReturn},  optionInterface=#{optionInterface}"

  interfaceParamSet = newParamset( path, op, optionReturn,  optionInterface )
  paramSetResponse = newParamset( path )
  interfaceParamSet.setResponse( paramSetResponse )
  return interfaceParamSet
end

#newParameter(name, isArray = false) ⇒ Sbuilder::Parameter_Dom

Cretate new pojo for a simple paramter

Parameters:

  • name (String)

    of the simple paramter to create

  • isArray (Boolean) (defaults to: false)

    true if array

Returns:



285
286
287
288
289
290
291
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 285

def newParameter( name, isArray=false )
  swagger_parameter = createParameter( Sbuilder::Constants::PARAMETER  )
  swagger_parameter.setName( name )
  # default for parameter 'not array' - set exlicitely array
  swagger_parameter.setIsArray( isArray )
  return swagger_parameter
end

#newParameterReference(name, referenceName, isArray) ⇒ Sbuilder::Parameter_Ref

Create new pojo for a reference to a definition

Parameters:

  • name (String)

    of the simple paramter to create

  • referenceName (String)

    definition name referenced

  • isArray (Boolean)

    true if array

Returns:



352
353
354
355
356
357
358
359
360
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 352

def newParameterReference( name, referenceName, isArray )
  refParameter = createParameter( Sbuilder::Constants::PARAMETER_REF  )
  refParameter.setName( name )
  # namespace.name
  referenceName = namespacedReferenceName( referenceName )      
  refParameter.setReference( referenceName )
  refParameter.setIsArray( isArray )
  return refParameter
end

#read_cached(location, &blk) ⇒ String

Reader service: read String of interface definitions from ‘location’. The string is cached in ‘@cacheFile’, which controller has configured.

read the string

‘Sbuilder::Utils::NetIo.read_lines’. Block should return a string.

Parameters:

  • location (String)

    uri/file/block parameter from which to

  • blk (yield)

    yield block to read if no block given use

Returns:

  • (String)

    read from ‘location’ (or from @cacheFile)



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 144

def read_cached( location, &blk )
  # return cached
  line = get_cached 
  return line if line
  if block_given?
    line = yield( location )
  else
    line = Sbuilder::Utils::NetIo.read_lines( location )
  end
  cache_line( line )
  return line
end

#setController(controller) ⇒ Object

Allow facade to access controller services

Parameters:



83
84
85
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 83

def setController( controller )
  @controller = controller
end

#setFactory(factory) ⇒ Object

Facade delagates the task to create new objects to ‘factory

Parameters:



89
90
91
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 89

def setFactory( factory )
  @factory = factory
end

#setInterfaceCacheFilePath(cacheFile) ⇒ Object

Cache string read into ‘cacheFile’

Parameters:

  • cacheFile (String)

    name of file to cache content in this facade



123
124
125
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 123

def setInterfaceCacheFilePath( cacheFile )
  @cacheFile = cacheFile
end

Implementation delegates ‘setSourceLink’ to ‘paramSet’

parameter set

Parameters:

  • paramSet (Sbuilder::ParamSet)

    parameset where to set the link to

  • sourceModule (String)

    pointer to module defining

  • sourceLine (int)

    sourceLine line number in #sourceModule

  • sourceColumn (int) (defaults to: 0)

    source column number #sourceModule



275
276
277
278
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 275

def setSourceLink(paramSet, sourceModule, sourceLine, sourceColumn=0)
  @logger.info "#{__method__}: paramSet=#{paramSet}, sourceModule=#{sourceModule}, sourceLine=#{sourceLine}"
  paramSet.setSourceLink(sourceModule, sourceLine, sourceColumn)
end

#versionObject

# @return [String] semver number



406
407
408
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 406

def version
  Sbuilder::version
end