module MaxCube::Messages

Encapsulates methods related to Cube messages, i.e. parsing and serializing of TCP/UDP messages. It does not provide any network features (this is responsibility of {Network}.

Constants

DAYS_OF_WEEK

Names of days of week in order Cube protocol uses.

DEVICE_MODE

Device modes that determines geating scheduling.

DEVICE_TYPE

Device types identified in Cube protocol.

Private Instance Methods

ary_elem(ary, id, info) click to toggle source

Helper method that checks presence of index in array (if not, exception is raised). @param ary [#[]] input container (usually constant). @param id index of element in container. @param info [#to_s] context information to pass to raised error. @return element of container if found. @raise [InvalidMessageBody] if element not found.

# File lib/maxcube/messages.rb, line 176
def ary_elem(ary, id, info)
  elem = ary[id]
  return elem if elem
  raise InvalidMessageBody
    .new(@msg_type, "unrecognized #{info} id: #{id}")
end
ary_elem_id(ary, elem, info) click to toggle source

Reverse method to {#ary_elem}.

# File lib/maxcube/messages.rb, line 184
def ary_elem_id(ary, elem, info)
  id = ary.index(elem)
  return id if id
  raise InvalidMessageBody
    .new(@msg_type, "unrecognized #{info}: #{elem}")
end
conv_args(type, info, *args, &block) click to toggle source

Applies a block to given arguments in order to perform conversion to certain type. If conversion fails, {InvalidMessageBody} is raised. Thus, this method can be used also for type checking purposes only. @param type [#to_s] name of the type to convert to. @param info [#to_s] context information to pass to raised error. @param args [Array] arguments to be converted into the same type. @yield a rule to provide

certain type check and conversion of arguments.

@return [Array] converted elements. @raise [InvalidMessageBody] if conversion fails.

# File lib/maxcube/messages.rb, line 74
def conv_args(type, info, *args, &block)
  info = info.to_s.tr('_', ' ')
  args.map(&block)
rescue ArgumentError, TypeError
  raise InvalidMessageBody
    .new(@msg_type,
         "invalid #{type} format of arguments #{args} (#{info})")
end
day_of_week(day_id) click to toggle source

Uses {#ary_elem} with {DAYS_OF_WEEK}

# File lib/maxcube/messages.rb, line 212
def day_of_week(day_id)
  ary_elem(DAYS_OF_WEEK, day_id, 'day of week')
end
day_of_week_id(day) click to toggle source

Uses {#ary_elem_id} with {DAYS_OF_WEEK}

# File lib/maxcube/messages.rb, line 217
def day_of_week_id(day)
  if day.respond_to?('to_i') && day.to_i.between?(1, 7)
    return (day.to_i + 1) % 7
  end
  ary_elem_id(DAYS_OF_WEEK, day.capitalize, 'day of week')
end
device_mode(device_mode_id) click to toggle source

Uses {#ary_elem} with {DEVICE_MODE}

# File lib/maxcube/messages.rb, line 202
def device_mode(device_mode_id)
  ary_elem(DEVICE_MODE, device_mode_id, 'device mode')
end
device_mode_id(device_mode) click to toggle source

Uses {#ary_elem_id} with {DEVICE_MODE}

# File lib/maxcube/messages.rb, line 207
def device_mode_id(device_mode)
  ary_elem_id(DEVICE_MODE, device_mode.to_sym, 'device mode')
end
device_type(device_type_id) click to toggle source

Uses {#ary_elem} with {DEVICE_TYPE}

# File lib/maxcube/messages.rb, line 192
def device_type(device_type_id)
  ary_elem(DEVICE_TYPE, device_type_id, 'device type')
end
device_type_id(device_type) click to toggle source

Uses {#ary_elem_id} with {DEVICE_TYPE}

# File lib/maxcube/messages.rb, line 197
def device_type_id(device_type)
  ary_elem_id(DEVICE_TYPE, device_type.to_sym, 'device type')
end
to_bool(info, arg) click to toggle source

Uses {#to_bools}, but operates with single argument. @param arg argument to convert to bool. @return [Boolean] converted element

to +TrueClass+ or +FalseClass+.
# File lib/maxcube/messages.rb, line 139
def to_bool(info, arg)
  to_bools(info, arg).first
end
to_bools(info, *args) click to toggle source

Uses {#conv_args} to convert objects to bools. @param args [Array] arguments to convert to bools. @return [Array<Boolean>] converted elements

to +TrueClass+ or +FalseClass+.
# File lib/maxcube/messages.rb, line 121
def to_bools(info, *args)
  conv_args('boolean', info, *args) do |arg|
    if arg == !!arg
      arg
    elsif arg.nil?
      false
    elsif %w[true false].include?(arg)
      arg == 'true'
    else
      !Integer(arg).zero?
    end
  end
end
to_datetime(info, arg) click to toggle source

Uses {#to_datetime}, but operates with single argument. @param arg argument to convert to Time. @return [Time] converted element.

# File lib/maxcube/messages.rb, line 165
def to_datetime(info, arg)
  to_datetimes(info, arg).first
end
to_datetimes(info, *args) click to toggle source

Uses {#conv_args} to convert objects to Time. @param args [Array] arguments to convert to Time. @return [Array<Time>] converted elements.

# File lib/maxcube/messages.rb, line 146
def to_datetimes(info, *args)
  conv_args('datetime', info, *args) do |arg|
    if arg.is_a?(Time)
      arg
    elsif arg.is_a?(String)
      Time.parse(arg)
    elsif arg.respond_to?('to_time')
      arg.to_time
    elsif arg.respond_to?('to_date')
      arg.to_date.to_time
    else
      raise ArgumentError
    end
  end
end
to_float(info, arg) click to toggle source

Uses {#to_floats}, but operates with single argument. @param arg [#Float] argument to convert to float. @return [Float] converted element.

# File lib/maxcube/messages.rb, line 113
def to_float(info, arg)
  to_floats(info, arg).first
end
to_floats(info, *args) click to toggle source

Uses {#conv_args} to convert numbers or string of characters (not binary data!) to floats. @param args [Array<#Float>] arguments to convert to floats. @return [Array<Float>] converted elements.

# File lib/maxcube/messages.rb, line 106
def to_floats(info, *args)
  conv_args('float', info, *args) { |x| Float(x) }
end
to_int(base, info, arg) click to toggle source

Uses {#to_ints}, but operates with single argument. @param arg [#Integer] argument to convert to integer. @return [Integer] converted element.

# File lib/maxcube/messages.rb, line 98
def to_int(base, info, arg)
  to_ints(base, info, arg).first
end
to_ints(base, info, *args) click to toggle source

Uses {#conv_args} to convert numbers or string of characters (not binary data!) to integers in given base (radix). For binary data use {Parser#read}. @param base [Integer] integers base (radix), 0 means auto-recognition. @param args [Array<#Integer>] arguments to convert to integers. @return [Array<Integer>] converted elements.

# File lib/maxcube/messages.rb, line 90
def to_ints(base, info, *args)
  base_str = base.zero? ? '' : "(#{base})"
  conv_args("integer#{base_str}", info, *args) { |x| Integer(x, base) }
end