uploadcare / uploadcare-ruby

Ruby API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.
https://uploadcare.com
MIT License
39 stars 28 forks source link

Reimplement the `ResourceList#[]` method to prevent LSP violation #37

Closed vizvamitra closed 7 years ago

vizvamitra commented 7 years ago

Currently Uploadcare::Api::ResourceList objects (Uploadcare::Api::FileList, Uploadcare::Api::GroupList) are working like so:

list = api.file_list(limit: 10)

list[0] # => #<Uploadcare::Api::File ...>
# but
list[10] # => nil (since only first 10 files were loaded so far)
# but
list.first(11).last # => #<Uploadcare::Api::File ...>

This happens because Uploadcare::Api::ResourceList delegates #[] method to #objects (array of currently loaded objects), so the requested object will only be returned if it was loaded so far.

It seems to me that this behaviour violates the least surprise principle: the expected behaviour for list[10] is to return the requested element of the list (triggering next page requests if needed) or nil

So how about reimplementing the #[] method to conform with this expectation, for exampe like so:

def [](index)
  first(index+1).last
end