Sha256: 6f594877eeff699dcd4293bd359e36feb20e12e09cb548a17f793b262f0f6096

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

require "json"
require "open-uri"
require "yaml"

class Lono::Importer
  module Download
    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

    def read_source(source)
      open(source).read
    rescue OpenURI::HTTPError, SocketError, Errno::ENOENT
      logger.info "ERROR: Unable to read source template provided: #{source}".color(:red)
      e = $!
      logger.info "#{e.class}: #{e.message}"
      logger.info "Please double check the source provided."
      exit 1
    rescue Exception => e
      logger.info "ERROR: Unable to read source template provided: #{source}".color(:red)
      logger.info "General Exception Error:"
      logger.info "#{e.class}: #{e.message}"
      logger.info "Please double check the source provided."
      exit 1
    end

    def json?(text)
      JSON.load(text)
      true # if reach here than it's just
    rescue JSON::ParserError
      false # not json
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lono-8.0.0.pre.rc3 lib/lono/importer/download.rb
lono-8.0.0.pre.rc2 lib/lono/importer/download.rb
lono-8.0.0.pre.rc1 lib/lono/importer/download.rb