module Ably::Modules::Conversions

Conversions module provides common timestamp and variable naming conversions to Ably classes. All methods are private

Private Instance Methods

DeviceDetails(device_details) click to toggle source

Convert device_details argument to a {Ably::Models::DeviceDetails} object

@param device_details [Ably::Models::DeviceDetails,Hash,nil] A device details object

@return [Ably::Models::DeviceDetails]

# File lib/submodules/ably-ruby/lib/ably/models/device_details.rb, line 10
def DeviceDetails(device_details)
  case device_details
  when Ably::Models::DeviceDetails
    device_details
  else
    Ably::Models::DeviceDetails.new(device_details)
  end
end
DevicePushDetails(device_push_details) click to toggle source

Convert device_push_details argument to a {Ably::Models::DevicePushDetails} object

@param device_push_details [Ably::Models::DevicePushDetails,Hash,nil] A device push notification details object

@return [Ably::Models::DevicePushDetails]

# File lib/submodules/ably-ruby/lib/ably/models/device_push_details.rb, line 10
def DevicePushDetails(device_push_details)
  case device_push_details
  when Ably::Models::DevicePushDetails
    device_push_details
  else
    Ably::Models::DevicePushDetails.new(device_push_details)
  end
end
ErrorInfo(error_details) click to toggle source

Convert error_details argument to a {ErrorInfo} object

@param error_details [ErrorInfo,Hash] Error info attributes

@return [ErrorInfo]

# File lib/submodules/ably-ruby/lib/ably/models/error_info.rb, line 10
def ErrorInfo(error_details)
  case error_details
  when Ably::Models::ErrorInfo
    error_details
  else
    Ably::Models::ErrorInfo.new(error_details)
  end
end
IdiomaticRubyWrapper(object, options = {}) click to toggle source

Creates or returns an {IdiomaticRubyWrapper} ensuring it never wraps itself

@return [IdiomaticRubyWrapper]

# File lib/submodules/ably-ruby/lib/ably/models/idiomatic_ruby_wrapper.rb, line 10
def IdiomaticRubyWrapper(object, options = {})
  case object
  when Ably::Models::IdiomaticRubyWrapper
    object
  else
    Ably::Models::IdiomaticRubyWrapper.new(object, options)
  end
end
PushChannelSubscription(push_channel_subscription) click to toggle source

Convert push_channel_subscription argument to a {Ably::Models::PushChannelSubscription} object

@param push_channel_subscription [Ably::Models::PushChannelSubscription,Hash,nil] A device details object

@return [Ably::Models::PushChannelSubscription]

# File lib/submodules/ably-ruby/lib/ably/models/push_channel_subscription.rb, line 10
def PushChannelSubscription(push_channel_subscription)
  case push_channel_subscription
  when Ably::Models::PushChannelSubscription
    push_channel_subscription
  else
    Ably::Models::PushChannelSubscription.new(push_channel_subscription)
  end
end
as_since_epoch(time, options = {}) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 8
def as_since_epoch(time, options = {})
  return nil if options[:allow_nil] && !time

  granularity = options.fetch(:granularity, :ms)

  case time
  when Time
    time.to_f * multiplier_from_granularity(granularity)
  when Numeric
    time
  else
    raise ArgumentError, 'time argument must be a Numeric or Time object'
  end.to_i
end
as_time_from_epoch(time, options = {}) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 23
def as_time_from_epoch(time, options = {})
  return nil if options[:allow_nil] && !time

  granularity = options.fetch(:granularity, :ms)

  case time
  when Numeric
    Time.at(time / multiplier_from_granularity(granularity))
  when Time
    time
  else
    raise ArgumentError, 'time argument must be a Numeric or Time object'
  end
end
build_messages(name, data = nil, attributes = {}) click to toggle source

Converts the name, data, attributes into the array of Message objects

@return [Array<Ably::Models::Message>]

# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 123
def build_messages(name, data = nil, attributes = {})
  return [Ably::Models::Message(ensure_supported_name_and_payload(nil, data, attributes))] if name.nil?

  Array(name).map do |item|
    Ably::Models::Message(ensure_supported_name_and_payload(item, data, attributes))
  end
end
convert_to_lower_case(key) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 86
def convert_to_lower_case(key)
  key.to_s.gsub('_', '')
end
convert_to_mixed_case(key, options = {}) click to toggle source

Convert key to mixedCase from mixed_case

# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 50
def convert_to_mixed_case(key, options = {})
  force_camel = options.fetch(:force_camel, false)

  key.to_s.
    split('_').
    each_with_index.map do |str, index|
      if index > 0 || force_camel
        str.capitalize
      else
        str
      end
    end.
    join
end
convert_to_mixed_case_hash(hash, options = {}) click to toggle source

Convert a Hash into a mixed case Hash objet i.e. { client_id: 1 } becomes { ‘clientId’ => 1 }

# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 67
def convert_to_mixed_case_hash(hash, options = {})
  raise ArgumentError, 'Hash expected' unless hash.kind_of?(Hash)
  hash.each_with_object({}) do |pair, new_hash|
    key, val = pair
    new_hash[convert_to_mixed_case(key, options)] = val
  end
end
convert_to_snake_case_symbol(key) click to toggle source

Convert key to :snake_case from snakeCase

# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 76
def convert_to_snake_case_symbol(key)
  key.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    gsub(/([a-zA-Z])(\d)/,'\1_\2').
    tr("-", "_").
    downcase.
    to_sym
end
ensure_supported_name_and_payload(name, data = nil, attributes = {}) click to toggle source

Ensures if the first argument (name) is a String, Hash or Ably::Models::Message object, second argument (data) should be a String, Hash, Array or nil (see ensure_supported_payload() method).

@return [Hash] Contains :name, :data and other attributes

(RSL1a, RSL1b)

# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 138
def ensure_supported_name_and_payload(name, data = nil, attributes = {})
  return name.attributes.dup if name.kind_of?(Ably::Models::Message)

  payload = data
  if (hash = name).kind_of?(Hash)
    name, payload = hash[:name], (hash[:data] || payload)
    attributes.merge!(hash)
  end

  name = ensure_utf_8(:name, name, allow_nil: true)
  ensure_supported_payload payload

  attributes.merge({ name: name, data: payload })
end
ensure_supported_payload(payload) click to toggle source

Ensures that the payload is a type supported by all Ably client libraries. Ably guarantees interoperability between client libraries and subsequently client libraries must reject unsupported types

@return <void>

# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 110
def ensure_supported_payload(payload)
  return if payload.kind_of?(String) ||
    payload.kind_of?(Hash) ||
    payload.kind_of?(Array) ||
    payload.nil?

  raise Ably::Exceptions::UnsupportedDataType.new('Invalid data payload', 400, Ably::Exceptions::Codes::INVALID_MESSAGE_DATA_OR_ENCODING)
end
ensure_utf_8(field_name, string_value, options = {}) click to toggle source

Ensures that the string value is converted to UTF-8 encoding Unless option allow_nil: true, an {ArgumentError} is raised if the string_value is not a string

@return <void>

# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 95
def ensure_utf_8(field_name, string_value, options = {})
  unless options[:allow_nil] && string_value.nil?
    raise ArgumentError, "#{field_name} must be a String" unless string_value.kind_of?(String)
  end
  string_value.encode(Encoding::UTF_8) if string_value
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e
  raise ArgumentError, "#{field_name} could not be converted to UTF-8: #{e.message}"
end
multiplier_from_granularity(granularity) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/conversions.rb, line 38
def multiplier_from_granularity(granularity)
  case granularity
  when :ms # milliseconds
    1_000.0
  when :s # seconds
    1.0
  else
    raise ArgumentError, 'invalid granularity'
  end
end