class Spacebunny::Device::Base
Attributes
api_endpoint[RW]
auto_configs[R]
auto_configure![R]
auto_connection_configs[R]
channels[RW]
connection_configs[R]
custom_connection_configs[R]
host[RW]
id[RW]
key[RW]
log_level[R]
log_to[R]
logger[R]
name[RW]
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/device/base.rb, line 17 def initialize(protocol, *args) @protocol = protocol @custom_connection_configs = {} @auto_connection_configs = {} options = args.extract_options.deep_symbolize_keys key = args.first @key = key || options[:key] @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_channels options[:channels] end
Public Instance Methods
api_endpoint=(options)
click to toggle source
# File lib/spacebunny/device/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
connect()
click to toggle source
# File lib/spacebunny/device/base.rb, line 66 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/device/base.rb, line 70 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/device/base.rb, line 77 def disconnect @connection_configs = nil end
host=(host)
click to toggle source
# File lib/spacebunny/device/base.rb, line 111 def host=(host) @connection_configs[:host] = host end
id=(id)
click to toggle source
# File lib/spacebunny/device/base.rb, line 95 def id=(id) @connection_configs[:device_id] = id end
name=(name)
click to toggle source
# File lib/spacebunny/device/base.rb, line 103 def name=(name) @connection_configs[:name] = name end
on_receive(options = {}, &block)
click to toggle source
Stub method: must be implemented on the class responsible to handle the protocol
# File lib/spacebunny/device/base.rb, line 87 def on_receive(options = {}, &block) logger.warn "on_receive method must be implemented on class responsibile to handle protocol '#{@protocol}'" end
publish(channel, message, options = {})
click to toggle source
Stub method: must be implemented on the class responsible to handle the protocol
# File lib/spacebunny/device/base.rb, line 82 def publish(channel, message, options = {}) logger.warn "publish method must be implemented on class responsibile to handle protocol '#{@protocol}'" end
secret=(secret)
click to toggle source
# File lib/spacebunny/device/base.rb, line 119 def secret=(secret) @connection_configs[:secret] = secret end
vhost=(vhost)
click to toggle source
# File lib/spacebunny/device/base.rb, line 128 def vhost=(vhost) @connection_configs[:secret] = secret end
Protected Instance Methods
auto_configure?()
click to toggle source
@protected
# File lib/spacebunny/device/base.rb, line 135 def auto_configure? !@key.nil? end
with_channel_check(name) { || ... }
click to toggle source
# File lib/spacebunny/device/base.rb, line 139 def with_channel_check(name) unless res = channels.include?(name.to_sym) logger.warn <<-MSG You're going to publish on channel '#{name}', but it does not appear a configured channel. If using auto-configuration (device-key) associate the channel to device '#{connection_configs[:device_name]}' from web interface. If providing manual configuration, please specify channels list through the :channels option or through given setter, e.g. client.channels = [:first_channel, :second_channel, ... ]) MSG end if block_given? yield else res end end
Private Instance Methods
build_logger()
click to toggle source
@private
# File lib/spacebunny/device/base.rb, line 188 def build_logger logger = ::Logger.new(@log_to) logger.level = normalize_log_level logger.progname = 'Spacebunny' Spacebunny.logger = logger end
check_connection_configs()
click to toggle source
@private Check for required params presence
# File lib/spacebunny/device/base.rb, line 171 def check_connection_configs raise DeviceIdMissing unless @connection_configs[:device_id] end
extract_and_normalize_custom_connection_configs_from(options)
click to toggle source
@private Copy options to custom_connection_configs
and normalize some of the attributes overwriting it
# File lib/spacebunny/device/base.rb, line 197 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[:device_id] = conn_options.delete :device_id @custom_connection_configs[:device_name] = conn_options.delete :device_name @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/device/base.rb, line 177 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_and_add_channels(chs)
click to toggle source
@private
# File lib/spacebunny/device/base.rb, line 216 def normalize_and_add_channels(chs) @channels = [] unless @channels return unless chs chs.each do |ch| case ch when Hash @channels << ch[:name].to_sym else ch.to_sym 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/device/base.rb, line 231 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], device_id: auto_configs[:connection][:device_id], device_name: auto_configs[:connection][:device_name], secret: auto_configs[:connection][:secret] } end
normalize_log_level()
click to toggle source
@private
# File lib/spacebunny/device/base.rb, line 244 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_channels(channels)
click to toggle source
@private Check if channels are an array
# File lib/spacebunny/device/base.rb, line 162 def set_channels(channels) if channels && !channels.is_a?(Array) raise ChannelsMustBeAnArray end normalize_and_add_channels(channels) end