class SelfSDK::App

Abstract base class for CLI utilities. Provides some helper methods for the option parser

@attr_reader [Types] app_id the identifier of the current app. @attr_reader [Types] app_key the api key for the current app.

Constants

BASE_URL
MESSAGING_URL

Attributes

client[R]
messaging_client[RW]

Public Class Methods

new(app_id, app_key, storage_key, storage_dir, opts = {}) click to toggle source

Initializes a SelfSDK App

@param app_id [string] the app id. @param app_key [string] the app api key provided by developer portal. @param storage_key [string] the key to be used to encrypt persisted data. @param storage_dir [String] The folder where encryption sessions and settings will be stored @param [Hash] opts the options to authenticate. @option opts [String] :base_url The self provider url. @option opts [String] :messaging_url The messaging self provider url. @option opts [Bool] :auto_reconnect Automatically reconnects to websocket if connection is lost (defaults to true). @option opts [Symbol] :env The environment to be used, defaults to “:production”.

# File lib/selfsdk.rb, line 49
def initialize(app_id, app_key, storage_key, storage_dir, opts = {})
  SelfSDK.logger.debug "syncing ntp times #{SelfSDK::Time.now}"
  env = opts.fetch(:env, "")

  @client = RestClient.new(base_url(opts), app_id, app_key, env)
  messaging_url = messaging_url(opts)
  unless messaging_url.nil?
    @messaging_client = MessagingClient.new(messaging_url,
                                            @client,
                                            storage_key,
                                            storage_dir: storage_dir,
                                            auto_reconnect: opts.fetch(:auto_reconnect, MessagingClient::DEFAULT_AUTO_RECONNECT),
                                            device_id: opts.fetch(:device_id, MessagingClient::DEFAULT_DEVICE))
  end
end

Public Instance Methods

app_id() click to toggle source
# File lib/selfsdk.rb, line 85
def app_id
  client.jwt.id
end
app_key() click to toggle source
# File lib/selfsdk.rb, line 89
def app_key
  client.jwt.key
end
authentication() click to toggle source

Provides access to SelfSDK::Services::Authentication service

# File lib/selfsdk.rb, line 71
def authentication
  @authentication ||= SelfSDK::Services::Authentication.new(messaging, @client)
end
close() click to toggle source

Closes the websocket connection

# File lib/selfsdk.rb, line 94
def close
  @messaging_client.close
end
facts() click to toggle source

Provides access to SelfSDK::Services::Facts service

# File lib/selfsdk.rb, line 66
def facts
  @facts ||= SelfSDK::Services::Facts.new(messaging, @client)
end
identity() click to toggle source

Provides access to SelfSDK::Services::Identity service

# File lib/selfsdk.rb, line 76
def identity
  @identity ||= SelfSDK::Services::Identity.new(@client)
end
messaging() click to toggle source

Provides access to SelfSDK::Services::Messaging service

# File lib/selfsdk.rb, line 81
def messaging
  @messaging ||= SelfSDK::Services::Messaging.new(@messaging_client)
end

Protected Instance Methods

base_url(opts) click to toggle source
# File lib/selfsdk.rb, line 100
def base_url(opts)
  return opts[:base_url] if opts.key? :base_url
  return "https://api.#{opts[:env].to_s}.joinself.com" if opts.key? :env
  BASE_URL
end
messaging_url(opts) click to toggle source
# File lib/selfsdk.rb, line 106
def messaging_url(opts)
  return opts[:messaging_url] if opts.key? :messaging_url
  return "wss://messaging.#{opts[:env].to_s}.joinself.com/v2/messaging" if opts.key? :env
  MESSAGING_URL
end