Class: Sbuilder::ApiLoader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils::MyLogger
Defined in:
lib/sbuilder/facade/api_loader.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

PROGNAME =

progname for logger

"ApiLoader"

Instance Attribute Summary (collapse)

Construtor (collapse)

Prepare Facade (collapse)

Cache API Metadata (collapse)

Add to Sbuilder (collapse)

Construct Model Objects (collapse)

Miscellaneous Operations (collapse)

Constructor Details

- (ApiLoader) initialize(factory, options = {})




55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sbuilder/facade/api_loader.rb', line 55

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

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

Instance Attribute Details

- (Object) cacheFile (readonly)

Returns the value of attribute cacheFile



26
27
28
# File 'lib/sbuilder/facade/api_loader.rb', line 26

def cacheFile
  @cacheFile
end

- (Object) controller

get sets when load starts



32
33
34
# File 'lib/sbuilder/facade/api_loader.rb', line 32

def controller
  @controller
end

- (Object) factory (readonly)

facade collaborators



31
32
33
# File 'lib/sbuilder/facade/api_loader.rb', line 31

def factory
  @factory
end

- (Object) infrastructureServices (readonly)

facade configuration



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

def infrastructureServices
  @infrastructureServices
end

- (Object) namespace (readonly)

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



24
25
26
# File 'lib/sbuilder/facade/api_loader.rb', line 24

def namespace
  @namespace
end

Instance Method Details

- (Object) cache_line(line, what = nil)

Service for loader to save 'line' into cache

Parameters:

  • line (String)

    content to store

  • what (String) (defaults to: nil)

    sub-content in cache



153
154
155
# File 'lib/sbuilder/facade/api_loader.rb', line 153

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

- (Object) configureInfrastructureService(infrastructureServices)

Tag all interfaces created using this facadate with property 'infrastructureServices'

Parameters:

  • infrastructureServices (Boolean)

    true/false set on all interfaces



93
94
95
# File 'lib/sbuilder/facade/api_loader.rb', line 93

def configureInfrastructureService( infrastructureServices )
  @infrastructureServices = infrastructureServices
end

- (Object) configureNamespace(namespace)

Prefix all definititions with “#namespace.” string

Parameters:

  • namespace (String)

    string to prefix to all definitions



101
102
103
# File 'lib/sbuilder/facade/api_loader.rb', line 101

def configureNamespace( namespace )
  @namespace = namespace
end

- (Logger) createLogger(logname)

Returns new logger object

Parameters:

  • logname (String)

    progname to set on logger

Returns:

  • (Logger)

    new logger object



255
256
257
# File 'lib/sbuilder/facade/api_loader.rb', line 255

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

- (Boolean) doCache?

Returns true if cache i.e. when cacheFile defined

Returns:

  • (Boolean)

    true if cache i.e. when cacheFile defined



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

def doCache?
  !cacheFile.nil?
end

- (String) get_cached(what = nil)

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



146
147
148
# File 'lib/sbuilder/facade/api_loader.rb', line 146

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

- (Object) modelDefinition(newDefinition)

Add 'newDefinition' to model

Parameters:

  • newDefinition (Sbuilder::ParamSetDef)

    definition to add



169
170
171
# File 'lib/sbuilder/facade/api_loader.rb', line 169

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

- (Object) modelInterface(newInterface)

Add 'newInterface' to model

Parameters:

  • newInterface (Sbuilder::ParamSet_If)

    interface to add



175
176
177
# File 'lib/sbuilder/facade/api_loader.rb', line 175

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

- (Sbuilder::ParamSetDef) newDefinition(definitionName, isArray = false)

Returns New definition object created

Parameters:

  • definitionName (String)

    name of defintition to create

  • isArray (Boolean) (defaults to: false)

    if array defined

Returns:

  • (Sbuilder::ParamSetDef)

    New definition object created



189
190
191
192
193
194
195
196
# File 'lib/sbuilder/facade/api_loader.rb', line 189

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

- (Sbuilder::ParamSet_If) newInterface(path, op)

Create new interface paramtereter sert (and response parameter set)

to 'response' (ie. definiting return from a operation)

for request. Use #response to access response object.

Parameters:

  • path (String)

    path of restfull interface operation

  • op (String)

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

Returns:

  • (Sbuilder::ParamSet_If)

    New interface param set createds



207
208
209
210
211
212
# File 'lib/sbuilder/facade/api_loader.rb', line 207

def newInterface( path, op  )
  interfaceParamSet = newParamset( path, op )
  paramSetResponse = newParamset( path )
  interfaceParamSet.setResponse( paramSetResponse )
  return interfaceParamSet
end

- (Sbuilder::Parameter_Dom) newParameter(name, isArray = false)

Cretate new pojo for a simple paramter

Parameters:

  • name (String)

    name of the simple paramter to create

  • isArray (Boolean) (defaults to: false)

    true if array

Returns:

  • (Sbuilder::Parameter_Dom)

    new parmater created



218
219
220
221
222
223
224
# File 'lib/sbuilder/facade/api_loader.rb', line 218

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

- (Sbuilder::Parameter_Ref) newParameterReference(name, referenceName, isArray)

Create new pojo for a reference to a definition

Parameters:

  • name (String)

    name of the simple paramter to create

  • referenceName (String)

    definition name referenced

  • isArray (Boolean)

    true if array

Returns:

  • (Sbuilder::Parameter_Ref)

    new parmater created



233
234
235
236
237
238
239
240
241
# File 'lib/sbuilder/facade/api_loader.rb', line 233

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

- (String) read_cached(location, &blk)

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)



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sbuilder/facade/api_loader.rb', line 130

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

- (Object) setController(controller)

Allow facade to access controller services

Parameters:

  • controller (Sbuilder::Controller)

    controller sets self



79
80
81
# File 'lib/sbuilder/facade/api_loader.rb', line 79

def setController( controller )
  @controller = controller
end

- (Object) setFactory(factory)

Facade delagates the task to create new objects to `factory

Parameters:

  • factory (Sbuilder::Factory)

    to create new obejcts



85
86
87
# File 'lib/sbuilder/facade/api_loader.rb', line 85

def setFactory( factory )
  @factory = factory
end

- (Object) setInterfaceCacheFilePath(cacheFile)

Cache string read into 'cacheFile'

Parameters:

  • cacheFile (String)

    name of file to cache content in this facade



109
110
111
# File 'lib/sbuilder/facade/api_loader.rb', line 109

def setInterfaceCacheFilePath( cacheFile )
  @cacheFile = cacheFile
end

- (String) version

Returns semver number

Returns:

  • (String)

    semver number



249
250
251
# File 'lib/sbuilder/facade/api_loader.rb', line 249

def version
  "1.0.2"
end