module App::Helpers::JSON

Public Instance Methods

json_dump(object) click to toggle source
# File app/helpers/json.rb, line 6
def json_dump(object)
  ::JSON.dump(object_hash(object))
end

Private Instance Methods

already_exists_error_hash(error) click to toggle source
# File app/helpers/json.rb, line 192
def already_exists_error_hash(error)
  hash = execution_error_hash(error)
  hash[:keyspace] = error.keyspace
  hash[:table]    = error.table
  hash
end
column_type_hash(column_type) click to toggle source
# File app/helpers/json.rb, line 85
def column_type_hash(column_type)
  case column_type
  when Array
    column_type.first.to_s + '<' + column_type.slice(1..-1).join(', ') + '>'
  else
    column_type
  end
end
columns_hash(rows) click to toggle source
# File app/helpers/json.rb, line 102
def columns_hash(rows)
  return [] if rows.empty?
  rows.first.keys
end
error_hash(error) click to toggle source
# File app/helpers/json.rb, line 157
def error_hash(error)
  case error
  when ::Cassandra::Errors::NoHostsAvailable
    no_hosts_available_error_hash(error)
  when ::Cassandra::Errors::ReadTimeoutError
    read_timeout_error_hash(error)
  when ::Cassandra::Errors::WriteTimeoutError
    write_timeout_error_hash(error)
  when ::Cassandra::Errors::UnavailableError
    unavailable_error_hash(error)
  when ::Cassandra::Errors::UnpreparedError
    unprepared_error_hash(error)
  when ::Cassandra::Errors::AlreadyExistsError
    already_exists_error_hash(error)
  when ::Cassandra::Errors::ExecutionError, ::Cassandra::Errors::ValidationError
    execution_error_hash(error)
  else
    exception_hash(error)
  end
end
exception_hash(error) click to toggle source
# File app/helpers/json.rb, line 178
def exception_hash(error)
  {
    :class   => error.class.name,
    :message => error.message,
    :trace   => error.backtrace
  }
end
execution_error_hash(error) click to toggle source
# File app/helpers/json.rb, line 186
def execution_error_hash(error)
  hash = exception_hash(error)
  hash[:statement] = statement_hash(error.statement)
  hash
end
execution_info_hash(execution_info) click to toggle source
# File app/helpers/json.rb, line 107
def execution_info_hash(execution_info)
  {
    :keyspace    => execution_info.keyspace,
    :statement   => statement_hash(execution_info.statement),
    :options     => execution_options_hash(execution_info.options),
    :hosts       => execution_info.hosts.map(&method(:host_hash)),
    :consistency => execution_info.consistency,
    :retries     => execution_info.retries,
    :trace       => execution_info.trace && execution_trace_hash(execution_info.trace)
  }
end
execution_options_hash(execution_options) click to toggle source
# File app/helpers/json.rb, line 125
def execution_options_hash(execution_options)
  {
    :consistency        => execution_options.consistency,
    :serial_consistency => execution_options.serial_consistency,
    :page_size          => execution_options.page_size,
    :timeout            => execution_options.timeout,
    :trace              => execution_options.trace?
  }
end
execution_trace_event_hash(execution_trace_event) click to toggle source
# File app/helpers/json.rb, line 147
def execution_trace_event_hash(execution_trace_event)
  {
    :id             => execution_trace_event.id,
    :activity       => execution_trace_event.activity,
    :source         => execution_trace_event.source,
    :source_elapsed => execution_trace_event.source_elapsed,
    :thread         => execution_trace_event.thread
  }
end
execution_trace_hash(execution_trace) click to toggle source
# File app/helpers/json.rb, line 135
def execution_trace_hash(execution_trace)
  {
    :id          => execution_trace.id,
    :coordinator => execution_trace.coordinator,
    :duration    => execution_trace.duration,
    :parameters  => execution_trace.parameters,
    :request     => execution_trace.request,
    :started_at  => execution_trace.started_at,
    :events      => execution_trace.events.map(&method(:execution_trace_event_hash))
  }
end
host_hash(host) click to toggle source
# File app/helpers/json.rb, line 54
def host_hash(host)
  {
    :ip              => host.ip,
    :id              => host.id,
    :datacenter      => host.datacenter,
    :rack            => host.rack,
    :release_version => host.release_version,
    :status          => host.status
  }
end
keyspace_hash(keyspace) click to toggle source
# File app/helpers/json.rb, line 65
def keyspace_hash(keyspace)
  {
    :name   => keyspace.name,
    :cql    => keyspace.to_cql,
    :tables => keyspace.tables.map do |table|
      {
        :name    => table.name,
        :cql     => table.to_cql,
        :columns => table.columns.map do |column|
          {
            :name  => column.name,
            :type  => column_type_hash(column.type),
            :order => column.order
          }
        end
      }
    end
  }
end
no_hosts_available_error_hash(error) click to toggle source
# File app/helpers/json.rb, line 231
def no_hosts_available_error_hash(error)
  hash = exception_hash(error)
  errors = []

  error.errors.each do |host, e|
    errors << {
      :host  => host_hash(host),
      :error => error_hash(e)
    }
  end

  hash[:errors] = errors
  hash
end
object_hash(object) click to toggle source
# File app/helpers/json.rb, line 12
def object_hash(object)
  case object
  when ::Cassandra::Host
    host_hash(object)
  when ::Cassandra::Keyspace
    keyspace_hash(object)
  when ::Cassandra::Result
    result_hash(object)
  when ::Exception
    exception_hash(object)
  when ::Hash
    hash = ::Hash.new
    object.each do |key, value|
      hash[key] = object_hash(value)
    end
    hash
  when ::Enumerable
    object.map(&method(:object_hash))
  when ::String
    begin
      object.encode('utf-8')
    rescue Encoding::UndefinedConversionError
      '0x' + object.unpack('H*').first
    end
  when nil
    'null'
  else
    object.to_s
  end
end
object_key(object) click to toggle source
# File app/helpers/json.rb, line 43
def object_key(object)
  case object
  when ::Cassandra::Host
    object.ip
  when ::Cassandra::Keyspace
    object.name
  else
    raise "unsupported object #{object.inspect}"
  end
end
read_timeout_error_hash(error) click to toggle source
# File app/helpers/json.rb, line 205
def read_timeout_error_hash(error)
  hash = execution_error_hash(error)
  hash[:retrieved]   = error.retrieved?
  hash[:consistency] = error.consistency
  hash[:required]    = error.required
  hash[:received]    = error.received
  hash
end
result_hash(result) click to toggle source
# File app/helpers/json.rb, line 94
def result_hash(result)
  {
    :rows    => result.map(&method(:object_hash)),
    :columns => columns_hash(result),
    :info    => execution_info_hash(result.execution_info),
  }
end
statement_hash(statement) click to toggle source
# File app/helpers/json.rb, line 119
def statement_hash(statement)
  {
    :cql => statement.cql
  }
end
unavailable_error_hash(error) click to toggle source
# File app/helpers/json.rb, line 223
def unavailable_error_hash(error)
  hash = execution_error_hash(error)
  hash[:consistency] = error.consistency
  hash[:required]    = error.required
  hash[:alive]       = error.alive
  hash
end
unprepared_error_hash(error) click to toggle source
# File app/helpers/json.rb, line 199
def unprepared_error_hash(error)
  hash = execution_error_hash(error)
  hash[:id] = error.id
  hash
end
write_timeout_error_hash(error) click to toggle source
# File app/helpers/json.rb, line 214
def write_timeout_error_hash(error)
  hash = execution_error_hash(error)
  hash[:type]        = error.type
  hash[:consistency] = error.consistency
  hash[:required]    = error.required
  hash[:received]    = error.received
  hash
end