LeftClick Web Services
LWS is a library for Ruby provides access to the LeftClick web services/applications using a model-based structure that abstracts from API calls using the available REST interfaces.
Installation
In your Gemfile, add:
gem "lws"
or install the ruby-lws
package.
Usage
First, you have to initialize the library. For example, with Rails, you
would create a new config/initializers/lws.rb
file with these lines:
# config/initializers/lws.rb
LWS.setup do |config|
config.api_token = "…" # Get it from someplace
end
After that, due to the fact that LWS is based on Spyke, you can access the objects in the LeftClick Web Services similary to many ActiveRecord-like ORM's:
map = Maps::Map.all.first
markers = map.markers
company = Company.find(6)
account = Auth::Account.create(name: "Foo Bar",
company: company)
loc = Presence::Location.where(name: "Test")
loc.destroy
Configuration
The following example uses a much more elaborate setup:
LWS.setup do |config|
config.api_token_middleware = TokenAuthenticator
config.caching_object = MyRedisCache.new
config.environment = :development
config.endponts = { maps: "https://maps.leftclick.cloud" }
config.http_debug = true
config.json_debug = true
config.logger = Rails.logger
end
In this setup, a caching object is used that follows the
FaradayMiddleWare::Caching
API. It uses all development API endpoints,
except for maps, which is overriden to use the production endpoint. Also
HTTP request and JSON data debug logging is enabled and LWS will use
the Rails logger to log the messages.
Also a custom API token authenticator class is used that should implement
the Faraday::Middleware
interface and set the X-Token
header with the
runtime determined API token as value, for example:
class TokenAuthenticator < Faraday::Middleware
def call(env)
env[:request_headers]["X-Token"] = … # Some calculated token
@app.call(env)
end
end
The LC_LWS_ENV
environment variable is supported to override the LWS
environment. Allowed values are "production" (default) and "development".
The LC_LWS_API_TOKEN
is supported to set the LWS API token default.
This only works if a custom API token middleware is not used.