Skip to content

Change API for input (read only IO) #5

Open

Description

@kou
No description provided.

Activity

  1. tikkss commented on Nov 3, 2025

    @tikkss
    Contributor

    How about the following new API?

    Red Remote Input downloads the data automatically if it's not cached,
    and closes the input upon exiting the block:

    RemoteInput.open(url) do |input|
      input.read
    end

    By default, url (host + path) is converted to a directory
    safe path in the system cache path.
    For example, https://example.com/sample/data.csv is converted to
    /system cache path/example.com-sample/data.csv.

    You can also specify a custom cache path:

    RemoteInput.open(url, cache_id: "my_data_id") do |input|
      input.local_path # => "/system cache path/my_data_id/data.csv"
    end

    You can also use RemoteInput without a block:

    remote_input = RemoteInput.new(url)
    remote_input.read
    remote_input.close

    You can also clear the cache:

    remote_input = RemoteInput.new(url)
    remote_input.clear_cache

    Work in progress... for streaming data such as big data:

    remote_input = RemoteInput.new(url)
    remote_input.download do |chunk|
    end
  2. kou commented on Nov 3, 2025

    @kou
    MemberAuthor

    Thanks. Let's try the API.

    But how about disabling cache when a user doesn't specify cache ID explicitly? Or how about specifying cache_path instead of cache_id:

    RemoteInput.open("https://example.com/sample/data.csv", cache_path: "example/sub-directory/data.csv") do |input|
      input.local_path # => "/system cache path/example/sub-directory/data.csv" # "example" is cache ID and "sub-directory/data.csv" is path in the cache
    end

    We can use the following API for streaming:

    remote_input.open(...) do |input|
      loop do
        chunk = input.read(4096)
        break if chunk.nil?
        # Use chunk
      end
    end

    We may provide #each_chunk:

    remote_input.open(...) do |input|
      input.each_chunk do |chunk|
        # Use chunk
      end
    end
  3. tikkss commented on Nov 10, 2025

    @tikkss
    Contributor

    Thanks for your reply.

    But how about disabling cache when a user doesn't specify cache ID explicitly? Or how about specifying cache_path instead of cache_id:

    A user may not want to cache data, so we can disable cache by doesn't
    specify cache_path explicitly:

    RemoteInput.open("https://example.com/sample/data.csv") do |input|
      input.local_path # => nil or /Tmpdir path/data.csv
    end

    A user can also specify a cache path:

    RemoteInput.open("https://example.com/sample/data.csv", cache_path: "example/sub-directory/data.csv") do |input|
      input.local_path # => "/system cache path/example/sub-directory/data.csv" # "example" is cache ID and "sub-directory/data.csv" is path in the cache
    end

    #each_chunk is sounds nice. Thanks for your suggestion.

    I'll try to implement the new API.

  4. tikkss commented on Aug 29, 2026

    @tikkss
    Contributor

    I will open the following pull requests one by one in this order for easier to review:

    1. Add RemoteInput.open without cache
    2. Add RemoteInput.open with cache (the cache_path: option)
    3. Add RemoteInput::Input#clear_cache
    4. Add RemoteInput::Input#each_chunk
    5. Update README for the new API
    6. Prepare for the 0.1.0 release

    Note that RemoteInput::Input.new, not RemoteInput.new, because RemoteInput is a module not a class.
    For now, the class name is RemoteInput::Input. I know Input is repeated, but I couldn't think of a better name.

    After the 0.1.0 release, I would like to migrate Red Datasets to this API step by step.

  5. added a commit that references this issue on Sep 26, 2026
  6. tikkss commented on Sep 29, 2026

    @tikkss
    Contributor

    I've updated the plan as is (1 is done):

    1. Add RemoteInput.open with cache (not without)
      • Add RemoteInput::Input#clear_cache
    2. Add RemoteInput.open with cache (the cache_path: option)
    3. Add RemoteInput::Input#each_chunk
    4. Make #path a public API (that downloads the data lazily)
    5. Update README for the new API
    6. Prepare for the 0.1.0 release

    I'll work on 2.

  7. added a commit that references this issue on Oct 18, 2026
  8. tikkss commented on Nov 2, 2026

    @tikkss
    Contributor

    I want to pass a RemoteInput object to CSV.open directly.

    Steps to reproduce (with GH-36):

    require "csv"
    require "remote_input"
    
    RemoteInput.open("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") do |input|
      CSV.open(input) do |csv|
        p csv.first
      end
    end

    Expected:

    ["5.1", "3.5", "1.4", "0.2", "Iris-setosa"]

    Actual:

    .../csv-3.3.6/lib/csv.rb:1668:in 'File#initialize': no implicit conversion of RemoteInput into String (TypeError)

    Cause:

    CSV.open passes the given object to File.open: https://github.com/ruby/csv/blob/v3.3.6/lib/csv.rb#L1668

    File.open accepts a String or an object that has #to_path. RemoteInput has neither.

    Workaround:

    We can use RemoteInput#path for this:

    RemoteInput.open("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") do |input|
      CSV.open(input.path) do |csv|
        p csv.first # => ["5.1", "3.5", "1.4", "0.2", "Iris-setosa"]
      end
    end

    Proposal:

    How about adding RemoteInput#to_path?
    Then we can pass a RemoteInput object to CSV.open, File.open and so on directly:

    RemoteInput.open("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") do |input|
      CSV.open(input) do |csv|
        p csv.first # => ["5.1", "3.5", "1.4", "0.2", "Iris-setosa"]
      end
    end
  9. kou commented on Nov 4, 2026

    @kou
    MemberAuthor

    We can add to_path but CSV.open isn't suitable API for RemoteInput. CSV.open opens an input but RemoteInput is already an input. CSV.new is for an opened input. So CSV.new(remote_input) not CSV.open(remote_input) is preferred API for RemoteInput.

  10. added a commit that references this issue on Nov 5, 2026
  11. tikkss commented on Nov 6, 2026

    @tikkss
    Contributor

    I see. CSV.new is better for RemoteInput.

    How about adding RemoteInput#gets instead of RemoteInput#to_path? It's the same as IO#gets like RemoteInput#read.

    Because CSV#each reads an input by IO#gets:
    https://github.com/ruby/csv/blob/afbee5e3548d38914706ae0de8a6cf421ba43187/lib/csv/parser.rb#L323 .

    So we can't read a RemoteInput object by CSV for now:

    # test.rb
    require "csv"
    require "remote_input"
    
    RemoteInput.open("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") do |input|
      csv = CSV.new(input)
      p csv.first
    end
    $ ruby -Ilib test.rb
    /Users/tikkss/.rbenv/versions/ruby-dev/lib/ruby/gems/4.1.0+4/gems/csv-3.3.6/lib/csv/parser.rb:323:in 'CSV::Parser::InputsScanner#read_chunk': private method 'gets' called for an instance of RemoteInput (NoMethodError)
    
              chunk = input.gets(@row_separator, @chunk_size)
                           ^^^^^
            from /Users/tikkss/.rbenv/versions/ruby-dev/lib/ruby/gems/4.1.0+4/gems/csv-3.3.6/lib/csv/parser.rb:107:in 'CSV::Parser::InputsScanner#initialize'
            from /Users/tikkss/.rbenv/versions/ruby-dev/lib/ruby/gems/4.1.0+4/gems/csv-3.3.6/lib/csv/parser.rb:908:in 'CSV::Parser#build_scanner'
            from /Users/tikkss/.rbenv/versions/ruby-dev/lib/ruby/gems/4.1.0+4/gems/csv-3.3.6/lib/csv/parser.rb:424:in 'CSV::Parser#parse'
            from /Users/tikkss/.rbenv/versions/ruby-dev/lib/ruby/gems/4.1.0+4/gems/csv-3.3.6/lib/csv.rb:2677:in 'CSV#each'
            from test.rb:6:in 'Enumerable#first'
            from test.rb:6:in 'block in <main>'
            from /Users/tikkss/src/github.com/red-data-tools/red-remote-input/lib/remote_input.rb:12:in 'RemoteInput.open'
            from test.rb:4:in '<main>'

    It works when I add RemoteInput#gets.

  12. kou commented on Nov 8, 2026

    @kou
    MemberAuthor

    We can add both of #gets and #to_path.

  13. tikkss commented on Nov 8, 2026

    @tikkss
    Contributor

    Thanks! I'll open pull requests for them.

    I found that we may also need the following methods to migrate red-datasets to red-remote-input:

    • #each_line
    • #eof?
    • #readline
    • #seek

    I'll add them too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions