class Object

Public Instance Methods

encode_times(doc, base='') click to toggle source
# File lib/bull/encode_times.rb, line 25
def encode_times doc, base=''
  ret = []
  if doc.is_a? Array
    doc.each_with_index { |v, i| ret << encode_times(v, base + '.' + i.to_s)}
  elsif doc.is_a? Hash
    doc.each_pair {|k, v| ret << encode_times(v, base + '.' + k.to_s)}
  elsif doc.is_a? Time
    return base[1..-1]
  end
  ret.flatten
end
format_float(value) click to toggle source
# File lib/bull/ui_core.rb, line 272
def format_float value
  e, d = value.to_s.split('.')
  d = '' if value.to_s.end_with?('.')
  return '' if e.nil?
  v = format_integer e
  if d.nil?
    v
  else
    v + '.' + d
  end
end
format_integer(value) click to toggle source
# File lib/bull/ui_core.rb, line 262
def format_integer value
  path = value.to_s.reverse.split(/(\d\d\d)/).select{|v| v != ''}
  if path[-1] == '-'
    path.pop
    '-' + path.join(',').reverse
  else
    path.join(',').reverse
  end
end
get_nested!(ret, attr) { |root| ... } click to toggle source
# File lib/bull/utils.rb, line 22
def get_nested! ret, attr
  ret_ = ret
  path = attr.split '.'
  root = path.shift
  doc = {}
  doc[root] = yield root

  if path.empty?
    ret[root] = doc[root] if root != 'id'
  else
    path.unshift root
    while !path.empty?
      aux = path.shift
      doc = doc[aux]
      if path.empty?
        ret[aux] = doc
      else
        ret = ret[aux] || ret[aux] = {}
      end
    end
  end
  ret_
end
reactive(*args, &block) click to toggle source
# File lib/bull/reactive_var.rb, line 85
def reactive(*args, &block)
    ret = {}
    args.each do |v|
        id = v.add(block)
        ret[id] = v
    end
    block.call
    ret
end
resolve_times(doc, keys) click to toggle source
# File lib/bull/encode_times.rb, line 3
def resolve_times doc, keys
  keys.each do |k|
    d = doc
    ks = k.split('.')
    attr = ks.shift.to_sym
    while ks != []
      if d.is_a? Array
        d = d[attr.to_i]
      else
        d = d[attr]
      end
      attr = ks.shift.to_sym
    end
    begin
        i = Integer(attr.to_s)
        d[i] = Time.parse d[i]
    rescue
        d[attr] = Time.parse d[attr]
    end
  end
end
set_nested(attr, value, doc) { |root, value| ... } click to toggle source
# File lib/bull/utils.rb, line 1
def set_nested attr, value, doc
  path = attr.split '.'
  root = path.shift
  if path.empty?
    yield root, value
  else
    doc = doc[root] || {}
    doc_ = doc
    while !path.empty?
      aux = path.shift
      if path.empty?
        doc[aux] = value
      else
        doc[aux] = {} if doc[aux].nil?
        doc = doc[aux]
      end
    end
    yield root, doc_
  end
end
start(app_controller, key_path) click to toggle source
# File lib/bull/start.rb, line 8
def start app_controller, key_path
  puts Time.now
  #MReport.load_reports

  $r = RethinkDB::RQL.new
  conn = $r.connect()

  EM.run do
    EM::WebSocket.run(:host => "0.0.0.0",
                      :port => 3000,
                      :secure => true,
                      :tls_options => {
                        :private_key_file => File.join(key_path, 'privateKey.key'), # "../../privateKey.key",
                        :cert_chain_file => File.join(key_path, 'certificate.crt') #"../../certificate.crt"
                      }
                      ) do |ws|
      controller = nil

      ws.onopen { |handshake| controller = app_controller.new ws, conn}

      ws.onmessage do |msg|
        begin
          controller.notify msg
        rescue Exception => e
          puts 'message: ', e.message
          puts 'trace: ', e.backtrace.inspect
        end
      end

      ws.onclose { controller.close; controller = nil }

      ws.onerror { |e| puts "Error: #{e.message}"}
    end
  end
end
symbolize_keys(obj) click to toggle source
# File lib/bull/symbolize.rb, line 1
def symbolize_keys obj
  if obj.is_a? Hash
    return obj.inject({}) {|memo, (k, v)| memo[k.to_sym]=symbolize_keys(v); memo}
  end
  if obj.is_a? Array
    return obj.map {|x| symbolize_keys x}
  end
  obj
end