Class: MdtranslatorCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/adiwg/mdtranslator_cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

exit_on_failure added to exit with code 1 if thor cannot complete task such as if required parameters are missing



24
25
26
# File 'lib/adiwg/mdtranslator_cli.rb', line 24

def self.exit_on_failure?
   true
end

Instance Method Details

#translate(file) ⇒ Object

accept command and options



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/adiwg/mdtranslator_cli.rb', line 57

def translate(file)

   # test to see if file parameter is a local file name
   # if not ... assumed it is a JSON string
   # note: this will need to be modified if/when a reader is added that is not in JSON format
   if File.exist?(file)
      # read file
      my_file = File.open(file, 'r')
      fileObj = my_file.read
      my_file.close
   else
      fileObj = file
   end

   # for testing parameters
   # puts 'reader: ' + options[:reader]
   # puts 'writer: ' + options[:writer]
   # puts 'validation level: ' + options[:validate]
   # puts 'showAllTags: ' + options[:showAllTags].to_s
   # puts 'forceValid: ' + options[:forceValid].to_s
   # puts 'message format: ' + options[:messages]
   # puts 'return object: ' + options[:returnObject].to_s
   # puts 'css link: ' + options[:cssLink]
   # puts 'forceValid: ' + options[:forceValid].to_s

   # call mdtranslator
   mdReturn = ADIWG::Mdtranslator.translate(
      file: fileObj,
      reader: options[:reader],
      writer: options[:writer] == 'iso19115_1' ? 'iso19115_3' : options[:writer], # iso19115_1 is a subset of iso19115_3
      validate: options[:validate],
      forceValid: options[:forceValid],
      showAllTags: options[:showAllTags],
      cssLink: options[:cssLink])

   # determine return content and type of return ...
   if mdReturn[:readerStructurePass] && mdReturn[:readerValidationPass] && mdReturn[:readerExecutionPass]

      # no problem was found with the input file
      if options[:writer].nil?
         # if no writer was specified the input was being validated only,
         # ...no writer output will have been generated,
         # ...and the return will be a string unless json was requested
         if options[:messages] == 'json'
            $stdout.write mdReturn.to_json
            return
         else
            $stdout.write 'Success'
            return
         end
      else
         # a writer was specified,
         # output is expected from the translator's writer
         if mdReturn[:writerPass]
            # writer output was generated
            # ...return the writer output in its native format unless json was requested
            if options[:returnObject]
               $stdout.write mdReturn.to_json
               return
            else
               $stdout.write mdReturn[:writerOutput].to_s
               return
            end
         else
            # the writer failed or generated warnings to be reported
            # ...return the messages as a string unless json was requested
            if options[:messages] == 'json'
               $stdout.write mdReturn.to_json
               return
            else
               # build a string with messages issues from parser and validator
               s = ''
               s += "Failed\n"
               s += "Writer failed to generate output or issued ERROR OR WARNING messages \n"
               s += "See following messages for further information\n"

               # post structure messages
               i = 0
               mdReturn[:writerMessages].each do |message|
                  i += 1
                  s += "\nMessage: #{i}\n"
                  s += message + "\n"
               end

               $stdout.write s
               return

            end
         end
      end

   else

      # problems were found with the input file

      # if no writer was specified the input was being validated only,
      # ...no writer output will have been generated,
      # ...and return is always expected to be a string
      if options[:messages] == 'json'
         $stdout.write mdReturn.to_json
         return
      else
         # build a string with messages issued from parser, validator, or reader
         s = ''
         s += "Failed\n"
         s += "Input failed to pass either file structure, validation, or content requirements\n"
         s += "See following messages for further information\n"

         # post structure messages
         if mdReturn[:readerStructurePass]
            s += "Success - Input structure is valid\n"
         else
            s += "Fail - Structure of input file is invalid - see following message(s):\n"
            i = 0
            mdReturn[:readerStructureMessages].each do |message|
               i += 1
               s += "\nMessage: #{i}\n"
               s += message.to_s + "\n"
            end
         end

         # post validator messages
         unless mdReturn[:readerValidationPass].nil?
            if mdReturn[:readerValidationPass]
               s += "Success - Input content passes schema definition\n"
            else
               s += "Fail - Input content did not pass schema validation - see following message(s):\n"
               i = 0
               mdReturn[:readerValidationMessages].each do |message|
                  i += 1
                  s += "\nMessage: #{i}\n"
                  s += message.to_s + "\n"
               end
            end
         end

         # post reader execution messages
         unless mdReturn[:readerExecutionPass].nil?
            if mdReturn[:readerExecutionPass]
               s += "Success - Reader execution successful\n"
            else
               s += "Fail - Reader execution failed - see following message(s):\n"
               i = 0
               mdReturn[:readerExecutionMessages].each do |message|
                  i += 1
                  s += "\nMessage: #{i}\n"
                  s += message.to_s + "\n"
               end
            end
         end

         $stdout.write s
         return

      end
   end

end

#versionObject



221
222
223
# File 'lib/adiwg/mdtranslator_cli.rb', line 221

def version
   $stdout.write ADIWG::Mdtranslator::VERSION
end