Add class Referator::Command

This commit is contained in:
Alex Kotov 2023-10-02 14:04:44 +04:00
parent 663ace82a2
commit 82ee397529
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 62 additions and 34 deletions

View File

@ -10,16 +10,6 @@ require 'referator'
$stdin.sync = true
$stdout.sync = true
def parse_command(line)
line = String(line).chomp
index = line.index(' ') || line.length
command = line[0...index].upcase.freeze
rest = line[index..].strip
rest = 'null' if rest.empty?
data = JSON.parse(rest).freeze
[command, data].freeze
end
def success(data = nil)
puts "OK #{data.to_json}"
end
@ -38,61 +28,65 @@ stacktrace = nil
while (line = $stdin.gets)
begin
command, data = parse_command line
case command
command = Referator::Command.parse line
case command.name
##################
# Administrative #
##################
when 'EXIT'
when :exit
success
exit
when 'PING'
when :ping
success :pong
when 'STACKTRACE'
when :stacktrace
success stacktrace
stacktrace = nil
##########
# Config #
##########
when 'REGISTER_SCRIPT'
config.scripts.register(**data.transform_keys(&:to_sym))
when :register_script
config.scripts.register(**command.data.transform_keys(&:to_sym))
success
when 'REGISTER_CATEGORY'
config.categories.register data
when :register_category
config.categories.register command.data
success
when 'REGISTER_FORMAT'
config.formats.register(**data.transform_keys(&:to_sym))
when :register_format
config.formats.register(**command.data.transform_keys(&:to_sym))
success
when 'REGISTER_REF'
config.repo.register_ref(**data.transform_keys(&:to_sym))
when :register_ref
config.repo.register_ref(**command.data.transform_keys(&:to_sym))
success
########
# Info #
########
when 'REF'
when :ref
config.freeze
success config.repo[data['category'], data['id']].to_h
success config.repo[command.data['category'],
command.data['id']].to_h
############
# Creation #
############
when 'MAKE_REF'
when :make_ref
config.freeze
success footnotes.make_ref(data['category'], data['id']).to_h
success footnotes.make_ref(command.data['category'],
command.data['id']).to_h
#############
# Rendering #
#############
when 'FETCH_FOOTNOTES'
when :fetch_footnotes
config.freeze
success footnotes.fetch_footnotes
when 'FETCH_NOTE'
when :fetch_note
config.freeze
success footnotes.fetch_note(data['category'], data['id']).to_h
when 'RENDER_FOOTNOTES'
success footnotes.fetch_note(command.data['category'],
command.data['id']).to_h
when :render_footnotes
config.freeze
success String footnotes.render_footnotes data
when 'RENDER_CITE_SUP'
success String footnotes.render_footnotes command.data
when :render_cite_sup
config.freeze
success String footnotes.render_cite_sup(**data.transform_keys(&:to_sym))
success String \
footnotes.render_cite_sup(**command.data.transform_keys(&:to_sym))
else
raise 'Invalid command'
end

View File

@ -5,6 +5,7 @@ require 'json'
require 'open3'
require 'pathname'
require_relative 'referator/command'
require_relative 'referator/config'
require_relative 'referator/config/categories'
require_relative 'referator/config/formats'

33
lib/referator/command.rb Normal file
View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
module Referator
class Command
def self.parse(str)
str = String(str).chomp
index = str.index(' ') || str.length
name = str[0...index].downcase.to_sym
rest = str[index..].strip
rest = 'null' if rest.empty?
new name, rest
end
attr_reader :name, :data
def initialize(name, json)
@name = Referator.validate_name! name
@data = freeze_data JSON.parse json
end
private
def freeze_data(object)
if object.instance_of? Array
object.map { |item| freeze_data item }.freeze
elsif object.instance_of? Hash
object.to_h { |key, value| [key.freeze, freeze_data(value)] }.freeze
else
object.freeze
end
end
end
end