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:



273
274
275
276
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 273

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



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

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



364
365
366
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 364

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



165
166
167
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 165

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



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 299

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 created using this facadate with property ‘infrastructureServices’

Parameters:

  • infrastructureServices (Boolean)

    true/false set on all interfaces



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

def configureInfrastructureService( infrastructureServices )
  @infrastructureServices = infrastructureServices
end

#configureInterfaceService(interfaceServices) ⇒ Object

Tag all interfaces created using this facadate with property ‘interfaceService’



104
105
106
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 104

def configureInterfaceService( interfaceServices )
  @interfaceServices = interfaceServices
end

#configureNamespace(namespace) ⇒ Object

Prefix all definititions with “##namespace.” string

Parameters:

  • namespace (String)

    to prefix to all definitions



113
114
115
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 113

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



384
385
386
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 384

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



346
347
348
349
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 346

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



170
171
172
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 170

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



158
159
160
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 158

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



177
178
179
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 177

def getCacheDir
  controller.getCacheDir
end

#modelDefinition(newDefinition) ⇒ Object

Add ‘newDefinition’ to model

Parameters:



189
190
191
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 189

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

#modelInterface(newInterface) ⇒ Object

Add ‘newInterface’ to model

Parameters:



195
196
197
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 195

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:



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

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:



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

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) ⇒ Sbuilder::ParamSet_If

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

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

for request. Use #response to access response object.

Parameters:

  • path (String)

    of restfull interface operation

  • op (String)

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

Returns:



245
246
247
248
249
250
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 245

def newInterface( path, op  )
  interfaceParamSet = newParamset( path, op )
  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:



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

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:



323
324
325
326
327
328
329
330
331
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 323

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)



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

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



121
122
123
# File 'lib/sbuilder/facade/api_loader_facade.rb', line 121

def setInterfaceCacheFilePath( cacheFile )
  @cacheFile = cacheFile
end

#versionObject

# @return [String] semver number



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

def version
  Sbuilder::version
end