Class: Openphar::Cli::Main

Inherits:
Thor
  • Object
show all
Defined in:
lib/openphar/cli/main.rb

Overview

Main CLI for the openphar gem

Instance Method Summary collapse

Instance Method Details

#compare(monograph_id) ⇒ Object



115
116
117
118
# File 'lib/openphar/cli/main.rb', line 115

def compare(monograph_id)
  puts "Comparing #{monograph_id} across publishers..."
  puts "Not implemented yet"
end

#editionsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/openphar/cli/main.rb', line 92

def editions
  edition_dir = File.join(Openphar.data_path, "edition")
  return puts "No editions found" unless File.directory?(edition_dir)

  puts "Available Editions:"
  puts "-" * 60

  Dir.glob("#{edition_dir}/*/*.jsonld").each do |file|
    data = JSON.parse(File.read(file))
    next unless data["@graph"]

    data["@graph"].each do |item|
      next unless item["@type"] == "Edition"

      label = item["prefLabel"]&.values&.first || item["editionNumber"]
      count = item["monographCount"] || "unknown"
      puts format("%-20s %s monographs", label, count)
    end
  end
end


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/openphar/cli/main.rb', line 55

def link(file)
  puts "Linking #{file} to data-herbapedia..."

  linker = Linkers::HerbapediaLinker.new(
    herbapedia_data_path: options[:herbapedia]
  )

  # Read and parse JSON-LD
  data = JSON.parse(File.read(file))

  # Link each monograph
  if data["@graph"]
    data["@graph"].map! do |monograph|
      linker.link_monograph(monograph)
    end
  else
    data = linker.link_monograph(data)
  end

  # Write updated file
  File.write(file, JSON.pretty_generate(data))
  puts "Links added to #{file}"
end

#publishersObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/openphar/cli/main.rb', line 80

def publishers
  puts "Supported Publishers:"
  puts "-" * 60
  Publisher.all.each do |publisher|
    puts format("%-10s %s (%s)",
                publisher.code,
                publisher.name["en"],
                publisher.country)
  end
end

#transform(input, output) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/openphar/cli/main.rb', line 26

def transform(input, output)
  puts "Transforming #{input} to #{output}..."
  puts "Publisher: #{options[:publisher]}"
  puts "Edition: #{options[:edition]}" if options[:edition]

  # Read input
  content = File.read(input)

  # Parse based on publisher
  parser = parser_for(options[:publisher], content)
  monographs = parser.parse

  puts "Found #{monographs.length} monographs"

  # Transform to JSON-LD
  transformer = Transformers::JsonldTransformer.new
  result = transformer.transform_graph(
    monographs,
    publisher: options[:publisher],
    edition: options[:edition]
  )

  # Write output
  File.write(output, JSON.pretty_generate(result))
  puts "Output written to #{output}"
end

#validate(file) ⇒ Object



16
17
18
19
20
# File 'lib/openphar/cli/main.rb', line 16

def validate(file)
  puts "Validating #{file}..."
  # TODO: Implement SHACL validation
  puts "Validation complete"
end

#versionObject



10
11
12
# File 'lib/openphar/cli/main.rb', line 10

def version
  puts "openphar #{Openphar::VERSION}"
end