Skip to content

Conversation

@starbeast
Copy link

Added rubocop and specs (with simplecov), faraday is used for web requests, Ruby 2.7 is set as a lower bound required version.

Copy link

@rafasoares rafasoares left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments on the Data::Base class

Comment on lines 28 to 33
ret = data.each_with_object([]) do |element, acc|
processed_element = vacuum(element) unless element.nil?
next if processed_element.nil?

acc.push(processed_element)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it could be replaced by data.compact.

Or filter_map, since you're targeting Ruby 2.7+

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not exactly, compact doesn't get rid of empty arrays or hashes, while this recursive vacuum does something like:

=> cleaner.vacuum([1,2,[],{a: {}}])
=> [1,2]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it was an oversight, but filter_map would cover that, since you could just recursively call vacuum in it.


acc.push(processed_element)
end
ret.length.zero? ? nil : ret

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret.length.zero? ? nil : ret
ret.empty? ? nil : ret

Or ret.presence if you're willing to add a dependency to active_support/core_ext.

Comment on lines 38 to 44
ret = data.each_with_object({}) do |(key, element), acc|
processed_element = vacuum(element) unless element.nil?
next if processed_element.nil?

acc[key] = processed_element
end
ret.length.zero? ? nil : ret

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the array method above

Comment on lines 19 to 25
def vacuum(data)
case data
when Array then vacuum_array_data(data)
when Hash then vacuum_hash_data(data)
else data
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this entire "vacuum" logic belongs in a separate class/module like DataCleaner or something like that. It can still be a child of the trustly/data module if it doesn't apply anywhere else, but I'd rather have it outside the Base class.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could very well be a module that gets included in the Base class, but it would still be separate in the code.

Comment on lines 47 to 51
def stringify_hash(hash)
hash.each_with_object({}) do |(k, v), acc|
acc[k.to_s] = v.is_a?(Hash) ? stringify_hash(v) : v
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably rename it to deep_stringify_keys, but right, arrays are not handled here :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes I wish active_support/core_ext was part of the Ruby stdlib 🙃

@starbeast starbeast requested a review from rafasoares August 25, 2022 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants