class Grn2Drn::CommandConverter

Constants

MICRO_SECONDS_DECIMAL_PLACE

Public Class Methods

new(options={}) click to toggle source
# File lib/grn2drn/command-converter.rb, line 25
def initialize(options={})
  @options = options
  @count = 0

  @command_parser = Groonga::Command::Parser.new
end

Public Instance Methods

convert(input) { |create_message(name, command_to_body(command))| ... } click to toggle source
# File lib/grn2drn/command-converter.rb, line 32
def convert(input, &block)
  @command_parser.on_command do |command|
    unless command.name == "load"
      yield create_message(command.name, command_to_body(command))
    end
  end

  parsed_columns = nil
  @command_parser.on_load_columns do |command, columns|
    parsed_columns = columns
  end
  @command_parser.on_load_value do |command, value|
    yield create_add_command(command, parsed_columns, value)
    command.original_source.clear
  end

  input.each_line do |line|
    @command_parser << line
  end
  @command_parser.finish
end

Private Instance Methods

command_to_body(command) click to toggle source
# File lib/grn2drn/command-converter.rb, line 96
def command_to_body(command)
  stringify_keys(command.arguments)
end
create_add_command(command, columns, record) click to toggle source
# File lib/grn2drn/command-converter.rb, line 100
def create_add_command(command, columns, record)
  table = command[:table]
  body = {
    "table" => table,
  }

  if record.is_a?(Hash)
    values = record.dup
    body["key"] = values.delete("_key")
    body["values"] = values
  else
    values = {}
    record.each_with_index do |value, column_index|
      column = columns[column_index]
      if column == "_key"
        body["key"] = value
      else
        values[column] = value
      end
    end
    body["values"] = values
  end

  create_message("add", body)
end
create_message(type, body) click to toggle source
# File lib/grn2drn/command-converter.rb, line 55
def create_message(type, body)
  id_prefix = @options[:id_prefix]
  if id_prefix.nil?
    id = new_unique_id
  else
    id = "#{id_prefix}:#{@count}"
    @count += 1
  end

  message = {
    "id" => id,
    "date" => format_date(@options[:date] || Time.now),
    "dataset" => @options[:dataset],
    "type" => type,
    "body" => body,
  }
  message["replyTo"] = @options[:reply_to] if @options[:reply_to]
  message
end
format_date(time) click to toggle source
# File lib/grn2drn/command-converter.rb, line 84
def format_date(time)
  time.utc.iso8601(MICRO_SECONDS_DECIMAL_PLACE)
end
new_unique_id() click to toggle source
# File lib/grn2drn/command-converter.rb, line 75
def new_unique_id
  now = Time.now
  now_msec = now.to_i * 1000 + now.usec
  random_string = rand(36 ** 16).to_s(36) # Base36
  Digest::SHA1.hexdigest("#{now_msec}:#{random_string}")
end
stringify_keys(hash) click to toggle source
# File lib/grn2drn/command-converter.rb, line 88
def stringify_keys(hash)
  stringified_hash = {}
  hash.each do |key, value|
    stringified_hash[key.to_s] = value
  end
  stringified_hash
end