class STAC::FileWriter

Class to write a hash as JSON on a file.

Public Class Methods

new(hash_to_json: ->(hash) { JSON.generate(hash) } click to toggle source
# File lib/stac/file_writer.rb, line 11
def initialize(hash_to_json: ->(hash) { JSON.generate(hash) })
  @hash_to_json = hash_to_json
end

Public Instance Methods

write(hash, dest:) click to toggle source

Creates a file on ‘dest` with the given hash as JSON.

# File lib/stac/file_writer.rb, line 16
def write(hash, dest:)
  dest_uri = URI.parse(dest)
  path = if dest_uri.relative?
           dest
         elsif dest_uri.is_a?(URI::File)
           dest_uri.path.to_s
         else
           raise NotSupportedURISchemeError, "not supported URI scheme: #{dest}"
         end

  pathname = Pathname(path)
  pathname.dirname.mkpath
  pathname.write(@hash_to_json.call(hash))
end