Add class Referator::Command
This commit is contained in:
parent
663ace82a2
commit
82ee397529
3 changed files with 62 additions and 34 deletions
|
@ -10,16 +10,6 @@ require 'referator'
|
||||||
$stdin.sync = true
|
$stdin.sync = true
|
||||||
$stdout.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)
|
def success(data = nil)
|
||||||
puts "OK #{data.to_json}"
|
puts "OK #{data.to_json}"
|
||||||
end
|
end
|
||||||
|
@ -38,61 +28,65 @@ stacktrace = nil
|
||||||
|
|
||||||
while (line = $stdin.gets)
|
while (line = $stdin.gets)
|
||||||
begin
|
begin
|
||||||
command, data = parse_command line
|
command = Referator::Command.parse line
|
||||||
case command
|
case command.name
|
||||||
##################
|
##################
|
||||||
# Administrative #
|
# Administrative #
|
||||||
##################
|
##################
|
||||||
when 'EXIT'
|
when :exit
|
||||||
success
|
success
|
||||||
exit
|
exit
|
||||||
when 'PING'
|
when :ping
|
||||||
success :pong
|
success :pong
|
||||||
when 'STACKTRACE'
|
when :stacktrace
|
||||||
success stacktrace
|
success stacktrace
|
||||||
stacktrace = nil
|
stacktrace = nil
|
||||||
##########
|
##########
|
||||||
# Config #
|
# Config #
|
||||||
##########
|
##########
|
||||||
when 'REGISTER_SCRIPT'
|
when :register_script
|
||||||
config.scripts.register(**data.transform_keys(&:to_sym))
|
config.scripts.register(**command.data.transform_keys(&:to_sym))
|
||||||
success
|
success
|
||||||
when 'REGISTER_CATEGORY'
|
when :register_category
|
||||||
config.categories.register data
|
config.categories.register command.data
|
||||||
success
|
success
|
||||||
when 'REGISTER_FORMAT'
|
when :register_format
|
||||||
config.formats.register(**data.transform_keys(&:to_sym))
|
config.formats.register(**command.data.transform_keys(&:to_sym))
|
||||||
success
|
success
|
||||||
when 'REGISTER_REF'
|
when :register_ref
|
||||||
config.repo.register_ref(**data.transform_keys(&:to_sym))
|
config.repo.register_ref(**command.data.transform_keys(&:to_sym))
|
||||||
success
|
success
|
||||||
########
|
########
|
||||||
# Info #
|
# Info #
|
||||||
########
|
########
|
||||||
when 'REF'
|
when :ref
|
||||||
config.freeze
|
config.freeze
|
||||||
success config.repo[data['category'], data['id']].to_h
|
success config.repo[command.data['category'],
|
||||||
|
command.data['id']].to_h
|
||||||
############
|
############
|
||||||
# Creation #
|
# Creation #
|
||||||
############
|
############
|
||||||
when 'MAKE_REF'
|
when :make_ref
|
||||||
config.freeze
|
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 #
|
# Rendering #
|
||||||
#############
|
#############
|
||||||
when 'FETCH_FOOTNOTES'
|
when :fetch_footnotes
|
||||||
config.freeze
|
config.freeze
|
||||||
success footnotes.fetch_footnotes
|
success footnotes.fetch_footnotes
|
||||||
when 'FETCH_NOTE'
|
when :fetch_note
|
||||||
config.freeze
|
config.freeze
|
||||||
success footnotes.fetch_note(data['category'], data['id']).to_h
|
success footnotes.fetch_note(command.data['category'],
|
||||||
when 'RENDER_FOOTNOTES'
|
command.data['id']).to_h
|
||||||
|
when :render_footnotes
|
||||||
config.freeze
|
config.freeze
|
||||||
success String footnotes.render_footnotes data
|
success String footnotes.render_footnotes command.data
|
||||||
when 'RENDER_CITE_SUP'
|
when :render_cite_sup
|
||||||
config.freeze
|
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
|
else
|
||||||
raise 'Invalid command'
|
raise 'Invalid command'
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,7 @@ require 'json'
|
||||||
require 'open3'
|
require 'open3'
|
||||||
require 'pathname'
|
require 'pathname'
|
||||||
|
|
||||||
|
require_relative 'referator/command'
|
||||||
require_relative 'referator/config'
|
require_relative 'referator/config'
|
||||||
require_relative 'referator/config/categories'
|
require_relative 'referator/config/categories'
|
||||||
require_relative 'referator/config/formats'
|
require_relative 'referator/config/formats'
|
||||||
|
|
33
lib/referator/command.rb
Normal file
33
lib/referator/command.rb
Normal 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
|
Loading…
Reference in a new issue