Module: Lono::Pro::Importer::Download
Instance Method Summary collapse
Instance Method Details
#download_template(source, dest_path) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/lono/pro/importer/download.rb', line 7 def download_template(source, dest_path) template = read_source(source) result = if json?(template) # abusing YAML.dump(YAML.load()) to convert json to yaml YAML.dump(YAML.load(template)) else template # template is already in YAML format end folder = File.dirname(dest_path) FileUtils.mkdir_p(folder) unless File.exist?(folder) IO.write(dest_path, result) dest_path end |
#json?(text) ⇒ Boolean
39 40 41 42 43 44 |
# File 'lib/lono/pro/importer/download.rb', line 39 def json?(text) JSON.load(text) true # if reach here than it's just rescue JSON::ParserError false # not json end |
#read_source(source) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/lono/pro/importer/download.rb', line 23 def read_source(source) open(source).read rescue OpenURI::HTTPError, SocketError, Errno::ENOENT puts "ERROR: Unable to read source template provided: #{source}".color(:red) e = $! puts "#{e.class}: #{e.}" puts "Please double check the source provided." exit 1 rescue Exception => e puts "ERROR: Unable to read source template provided: #{source}".color(:red) puts "General Exception Error:" puts "#{e.class}: #{e.}" puts "Please double check the source provided." exit 1 end |