Sha256: 3c8fb3cd625d6ee87464e2ed75021154014e975a4353486b4a8ecda58ee54452

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

require 'json'

module Deepstream
  module Helpers
    SCHEME = 'ws://'
    DEFAULT_PORT = 6020
    DEFAULT_PATH = 'deepstream-v3'

    def self.to_deepstream_type(value)
      case value
      when Array then "O#{value.to_json}"
      when Hash then "O#{value.to_json}"
      when String then "S#{value}"
      when Numeric then "N#{value}"
      when TrueClass then 'T'
      when FalseClass then 'F'
      when NilClass then 'L'
      end
    end

    def self.to_type(payload)
      case payload[0]
      when 'O' then JSON.parse(payload[1..-1])
      when '{' then JSON.parse(payload)
      when 'S' then payload[1..-1]
      when 'N' then payload[1..-1].to_f
      when 'T' then true
      when 'F' then false
      when 'L' then nil
      else JSON.parse(payload)
      end
    end

    def self.default_options
      {
        ack_timeout: nil,
        credentials: {},
        heartbeat_interval: nil,
        in_thread: true,
        verbose: false,
        debug: false
      }
    end

    def self.url(url)
      url.tap do |url|
        url.prepend(SCHEME) unless url.start_with?(/ws(s|)\:\/\//)
        url.concat(":#{DEFAULT_PORT}") unless url[/\:\d+/]
        url.concat("/#{DEFAULT_PATH}") unless url[/\/\w+$/]
      end
    end

    def self.message_data(*args, **kwargs)
      kwargs = kwargs.empty? ? nil : kwargs
      if args.empty?
        kwargs
      else
        (args << kwargs).compact.instance_eval { one? ? first : self }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
deepstream-1.0.2 lib/deepstream/helpers.rb
deepstream-1.0.1 lib/deepstream/helpers.rb
deepstream-1.0.0 lib/deepstream/helpers.rb