class Spacebunny::LiveStream::Base

Attributes

api_endpoint[RW]
auto_configs[R]
auto_configure[R]
auto_connection_configs[R]
client[RW]
connection_configs[R]
custom_connection_configs[R]
host[RW]
live_streams[RW]
log_level[R]
log_to[R]
logger[R]
organization_id[RW]
raise_on_error[RW]
secret[RW]
tls[R]
tls_ca_certificates[R]
tls_cert[R]
tls_key[R]
verify_peer[R]
vhost[RW]

Public Class Methods

new(protocol, *args) click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 17
def initialize(protocol, *args)
  @protocol = protocol
  @custom_connection_configs = {}
  @auto_connection_configs = {}
  options = args.extract_options.deep_symbolize_keys

  @client = options[:client] || raise(ClientRequired)
  @secret = options[:secret] || raise(SecretRequired)
  @api_endpoint = options[:api_endpoint] || {}

  @raise_on_error = options[:raise_on_error]
  @log_to = options[:log_to] || STDOUT
  @log_level = options[:log_level] || ::Logger::ERROR
  @logger = options[:logger] || build_logger

  extract_and_normalize_custom_connection_configs_from options
  set_live_streams options[:live_streams]
end

Public Instance Methods

api_endpoint=(options) click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 36
def api_endpoint=(options)
  unless options.is_a? Hash
    raise ArgumentError, 'api_endpoint must be an Hash. See doc for further info'
  end
  @api_endpoint = options.deep_symbolize_keys
end
auto_configure?() click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 88
def auto_configure?
  @client && @secret
end
connect() click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 68
def connect
  logger.warn "connect method must be implemented on class responsibile to handle protocol '#{@protocol}'"
end
connection_options=(options) click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 72
def connection_options=(options)
  unless options.is_a? Hash
    raise ArgumentError, 'connection_options must be an Hash. See doc for further info'
  end
  extract_and_normalize_custom_connection_configs_from options.with_indifferent_access
end
disconnect() click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 79
def disconnect
  @connection_configs = nil
end
host=(host) click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 96
def host=(host)
  @connection_configs[:host] = host
end
on_receive(options = {}) click to toggle source

Stub method: must be implemented on the class responsible to handle the protocol

# File lib/spacebunny/live_stream/base.rb, line 84
def on_receive(options = {})
  logger.warn "on_receive method must be implemented on class responsibile to handle protocol '#{@protocol}'"
end
secret=(secret) click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 104
def secret=(secret)
  @connection_configs[:secret] = secret
end
vhost=(vhost) click to toggle source
# File lib/spacebunny/live_stream/base.rb, line 113
def vhost=(vhost)
  @connection_configs[:secret] = secret
end

Private Instance Methods

build_logger() click to toggle source

@private

# File lib/spacebunny/live_stream/base.rb, line 147
def build_logger
  logger          = ::Logger.new(@log_to)
  logger.level    = normalize_log_level
  logger.progname = 'Spacebunny'
  Spacebunny.logger = logger
end
check_and_add_live_streams(chs) click to toggle source

@private

# File lib/spacebunny/live_stream/base.rb, line 173
def check_and_add_live_streams(chs)
  @live_streams = [] unless @live_streams
  return unless chs
  chs.each do |ch|
    case ch
      when Hash
        ch.symbolize_keys!
        # Check for required attributes
        [:id, :name].each do |attr|
          unless ch[attr]
            raise LiveStreamParamError(ch[:name], attr)
          end
        end
        @live_streams << ch
      else
        raise LiveStreamFormatError
    end
  end
end
check_connection_configs() click to toggle source

@private Check for required params presence

# File lib/spacebunny/live_stream/base.rb, line 130
def check_connection_configs
  # Do nothing ATM
end
extract_and_normalize_custom_connection_configs_from(options) click to toggle source

@private

# File lib/spacebunny/live_stream/base.rb, line 155
def extract_and_normalize_custom_connection_configs_from(options)
  @custom_connection_configs = options

  @custom_connection_configs[:logger] = @custom_connection_configs.delete(:logger) || @logger

  if conn_options = @custom_connection_configs[:connection]
    @custom_connection_configs[:host] = conn_options.delete :host
    if conn_options[:protocols] && conn_options[:protocols][@protocol]
      @custom_connection_configs[:port] = conn_options[:protocols][@protocol].delete :port
      @custom_connection_configs[:tls_port] = conn_options[:protocols][@protocol].delete :tls_port
    end
    @custom_connection_configs[:vhost] = conn_options.delete :vhost
    @custom_connection_configs[:client] = conn_options.delete :client
    @custom_connection_configs[:secret] = conn_options.delete :secret
  end
end
merge_connection_configs() click to toggle source

@private Merge auto_connection_configs and custom_connection_configs

# File lib/spacebunny/live_stream/base.rb, line 136
def merge_connection_configs
  auto_connection_configs.merge(custom_connection_configs) do |key, old_val, new_val|
    if new_val.nil?
      old_val
    else
      new_val
    end
  end
end
normalize_auto_connection_configs() click to toggle source

@private Translate from auto configs given by APIs endpoint to a common format

# File lib/spacebunny/live_stream/base.rb, line 195
def normalize_auto_connection_configs
  {
      host: auto_configs[:connection][:host],
      port: auto_configs[:connection][:protocols][@protocol][:port],
      tls_port: auto_configs[:connection][:protocols][@protocol][:tls_port],
      vhost: auto_configs[:connection][:vhost],
      client: auto_configs[:connection][:client],
      secret: auto_configs[:connection][:secret]
  }
end
normalize_log_level() click to toggle source

@private

# File lib/spacebunny/live_stream/base.rb, line 207
def normalize_log_level
  case @log_level
    when :debug, ::Logger::DEBUG, 'debug' then ::Logger::DEBUG
    when :info,  ::Logger::INFO,  'info'  then ::Logger::INFO
    when :warn,  ::Logger::WARN,  'warn'  then ::Logger::WARN
    when :error, ::Logger::ERROR, 'error' then ::Logger::ERROR
    when :fatal, ::Logger::FATAL, 'fatal' then ::Logger::FATAL
    else
      Logger::ERROR
  end
end
set_live_streams(live_streams) click to toggle source

@private Check if live_streams are an array

# File lib/spacebunny/live_stream/base.rb, line 121
def set_live_streams(live_streams)
  if live_streams && !live_streams.is_a?(Array)
    raise StreamsMustBeAnArray
  end
  check_and_add_live_streams(live_streams)
end