Module: LWS

Includes:
Errors
Defined in:
lib/lws.rb,
lib/lws/config.rb,
lib/lws/errors.rb,
lib/lws/version.rb,
lib/lws/stubbing.rb,
lib/lws/middleware.rb,
lib/lws/middleware/http_logger.rb,
lib/lws/middleware/json_logger.rb,
lib/lws/middleware/json_parser.rb,
lib/lws/middleware/request_headers.rb

Overview

The main LeftClick web services module

This module is the main namespace for the web service/application libraries that are represented by submodules. The LWS module is used to load, set up and configure the supported application/web service libraries.

Defined Under Namespace

Modules: Auth, CorporateWebsite, DigitalSignage, Errors, Generic, Maps, Presence, Resource, Ticket Classes: Config, Stubbing

Constant Summary collapse

SUPPORTED_APPS =

The list of supported apps (web service libraries) loaded by setup.

[:generic, :auth, :corporate_website, :digital_signage,
:maps, :presence, :resource, :ticket].freeze
VERSION =
Note:

The major and minor version parts match the LWS API version!

The LWS library version.

"7.1.1".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.app_module(app_name) ⇒ Module

Returns the app module for the given app name.

Parameters:

  • app_name (String, Symbol)

    the app name

Returns:

  • (Module)

    the app module



143
144
145
# File 'lib/lws.rb', line 143

def self.app_module(app_name)
  @app_modules[app_name.to_sym]
end

.load_app_modulesArray<Symbol>

(Re)loads the app modules (usually done by setup).

Returns:

  • (Array<Symbol>)

    the apps that were loaded (see also SUPPORTED_APPS)



151
152
153
154
155
156
157
158
# File 'lib/lws.rb', line 151

def self.load_app_modules
  @app_modules = {}
  app_module_path = File.dirname(__FILE__)
  SUPPORTED_APPS.each do |app_name|
    load "#{app_module_path}/lws/apps/#{app_name}.rb"
    @app_modules[app_name] = LWS.const_get(app_name.to_s.camelize)
  end
end

.load_stubbingStubbing?

(Re)loads the stubbing if enabled (usually done by setup).

Returns:

  • (Stubbing, nil)

    the stubbing object or nil if disabled



163
164
165
166
167
168
169
170
# File 'lib/lws.rb', line 163

def self.load_stubbing
  if config.stubbing.present?
    @@stubbing = Stubbing.new(config.stubbing)
  else
    @@stubbing.disable! if @@stubbing
    @@stubbing = nil
  end
end

.setup {|config| ... } ⇒ LWS

Sets up the application API libraries using the provided configuration (see Config).

The API token can be overridden using the LC_LWS_API_TOKEN environment variable. The LWS environment can be overriden using the LC_LWS_ENV environment variable.

Examples:

Set up LWS with a token and the development environment

LWS.setup do |config|
  config.api_token = "MY_TOKEN"
  config.environment = :development
end

Set up LWS with API token middleware, caching, the development environment, an map endpoint override and logging to the Rails logger

LWS.setup do |config|
  config.api_token_middleware = TokenAuthenticator
  config.caching_object = MyRedisCache.new
  config.environment = :development
  config.endpoints = { maps: "https://maps.leftclick.cloud" }
  config.http_debug = true
  config.json_debug = true
  config.logger = Rails.logger
end

Yield Parameters:

  • config (Config)

    an API configuration object that can be configured

Returns:

  • (LWS)

    the module itself

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lws.rb', line 78

def self.setup(&block)
  @@config = Config.new
  yield @@config

  # Override the API token if needed (and no custom API token middleware
  # is used)
  if ENV["LC_LWS_API_TOKEN"].present?
    @@config.api_token = ENV["LC_LWS_API_TOKEN"]
  end

  # Override the environment if needed
  if ENV["LC_LWS_ENV"].present?
    @@config.environment = ENV["LC_LWS_ENV"].to_sym
  end

  if config.api_token.blank? and config.api_token_middleware.blank?
    raise LWS::Errors::ConfigError,
      "API token or API token middleware is required"
  end

  load_app_modules
  load_stubbing

  return self
end

Instance Method Details

#configConfig

Returns the API configuration for the web services.

Returns:

  • (Config)

    the API configuration for the web services



44
# File 'lib/lws.rb', line 44

mattr_reader :config

#stubbingStubbing

Returns the stubbing setup for the web service.

Returns:

  • (Stubbing)

    the stubbing setup for the web service



47
# File 'lib/lws.rb', line 47

mattr_reader :stubbing