Module: Audd::Source

Defined in:
lib/audd/source.rb

Overview

Normalizes whatever the caller passed as file: into the [filename, io] pair HttpClient wants.

Constant Summary collapse

FALLBACK_FILENAME =
"upload.bin"

Class Method Summary collapse

Class Method Details

.with_file(file) ⇒ Object

Yields [filename, io], closing only handles we opened ourselves — a caller-supplied IO stays the caller's to manage.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/audd/source.rb', line 15

def self.with_file(file)
  return yield(nil) if file.nil?

  if file.respond_to?(:read)
    name = file.respond_to?(:path) ? File.basename(file.path) : FALLBACK_FILENAME
    yield [name, file]
  else
    path = Pathname.new(file.to_s)
    unless path.file?
      raise InvalidParameterError,
        "file: #{file.inspect} is not a readable file. Pass a path, a " \
        "Pathname, or an open IO — or use url: to have AudD fetch it."
    end
    path.open("rb") { |io| yield [path.basename.to_s, io] }
  end
end