upserve / docker-api

A lightweight Ruby client for the Docker Remote API
MIT License
1.09k stars 287 forks source link

Parameters that accept (string) Paths should also accept Pathname objects #573

Open ianfixes opened 3 years ago

ianfixes commented 3 years ago

Ruby supports Pathname objects which are more appropriate than strings for encoding paths (or more bluntly: paths aren't strings).

Builtin functions like File.open honor Pathname objects, and it would be great if this library did as well. This can be as simple as attaching a .to_s to any incoming function argument used as a path, or even rewriting sections of the API to leverage Pathname directly.

e.g. this function https://github.com/swipely/docker-api/blob/d0d12c41eb736240551250ed2cfd261ad43f144c/lib/docker/util.rb#L136-L143

Could be rewritten

  def create_dir_tar(directory_or_pathname)
    directory = directory_or_pathname.to_s            ###### <-- add .to_s here
    tempfile = create_temp_file
    directory += '/' unless directory.end_with?('/')   # Or, consider using Pathname to avoid this string hack

    create_relative_dir_tar(directory, tempfile)

    File.new(tempfile.path, 'r')
  end