module RethinkDB::Shim

Public Class Methods

dump_json(x) click to toggle source
# File lib/shim.rb, line 54
def self.dump_json(x)
  JSON.generate(x, max_nesting: false)
end
load_json(target, opts=nil) click to toggle source
# File lib/shim.rb, line 47
def self.load_json(target, opts=nil)
  recursive_munge(JSON.parse(target, max_nesting: false),
                  opts && opts[:time_format] != 'raw',
                  opts && opts[:group_format] != 'raw',
                  opts && opts[:binary_format] != 'raw')
end
recursive_munge(x, parse_time, parse_group, parse_binary) click to toggle source
# File lib/shim.rb, line 21
def self.recursive_munge(x, parse_time, parse_group, parse_binary)
  case x
  when Hash
    if parse_time && x['$reql_type$'] == 'TIME'
      t = Time.at((x['epoch_time'] * 1000).to_i / 1000.0)
      tz = x['timezone']
      return (tz && tz != "" && tz != "Z") ? t.getlocal(tz) : t.utc
    elsif parse_group && x['$reql_type$'] == 'GROUPED_DATA'
      return Hash[recursive_munge(x['data'], parse_time, parse_group, parse_binary)]
    elsif parse_binary && x['$reql_type$'] == 'BINARY'
      return Binary.new(Base64.decode64(x['data'])).force_encoding('BINARY')
    else
      x.each {|k, v|
        v2 = recursive_munge(v, parse_time, parse_group, parse_binary)
        x[k] = v2 if v.object_id != v2.object_id
      }
    end
  when Array
    x.each_with_index {|v, i|
      v2 = recursive_munge(v, parse_time, parse_group, parse_binary)
      x[i] = v2 if v.object_id != v2.object_id
    }
  end
  return x
end
response_to_native(r, orig_term, opts) click to toggle source
# File lib/shim.rb, line 58
def self.response_to_native(r, orig_term, opts)
  rt = Response::ResponseType
  re = Response::ErrorType
  begin
    case r['t']
    when rt::SUCCESS_ATOM         then r['r'][0]
    when rt::SUCCESS_PARTIAL      then r['r']
    when rt::SUCCESS_SEQUENCE     then r['r']
    when rt::RUNTIME_ERROR
      case r['e']
        when re::INTERNAL         then raise ReqlInternalError,         r['r'][0]
        when re::RESOURCE_LIMIT   then raise ReqlResourceLimitError,    r['r'][0]
        when re::QUERY_LOGIC      then raise ReqlQueryLogicError,       r['r'][0]
        when re::NON_EXISTENCE    then raise ReqlNonExistenceError,     r['r'][0]
        when re::OP_FAILED        then raise ReqlOpFailedError,         r['r'][0]
        when re::OP_INDETERMINATE then raise ReqlOpIndeterminateError,  r['r'][0]
        when re::USER             then raise ReqlUserError,             r['r'][0]
        when re::PERMISSION_ERROR then raise ReqlPermissionError,       r['r'][0]
        else                           raise ReqlRuntimeError,          r['r'][0]
      end
    when rt::COMPILE_ERROR        then raise ReqlServerCompileError,    r['r'][0]
    when rt::CLIENT_ERROR         then raise ReqlDriverError,           r['r'][0]
    else raise ReqlRuntimeError, "Unexpected response: #{r.inspect}"
    end
  rescue ReqlError => e
    raise e.class, "#{e.message}\nBacktrace:\n#{RPP.pp(orig_term, r['b'])}"
  end
end