The code is good already: short, clear methods. I made a few small changes, mostly around naming:
- Rather than put local and remote into a single array and refer to them by index, just give them both names. It makes client code more readable.
- Raising an error rather than an exeception, as someone else noted.
Finally, the method names seem slightly opaque. Since I don't unserstand exactly what your doing, I didn't attempt to fix this, but it appears "parse" would be the public method used by clients, and that name is vague. What is the action really accomplishing? I'd pick the name based on that. It seems you are syncing with s3, and then returning the results of that sync. Maybe "sync_results" would be appropriate?
Depending on how you intend clients to use it, you may want to expose sync
(with no output, but storing it's raw results in the object) and sync_results
separately? Parsing is really an implementation detail that has little to do with the intent of the method (I think) and so should not be the name of the method.
module CompanyLib
class S3
attr_reader :output, :local, :remote, :bucket
def set_path local, remote
@local = local
@remote = remote
@bucket = get_bucket(remote)
end
def get_bucket(str)
/(^[^\/]*)/.match(str)[0]
end
def sync
@output = `aws s3 sync #{local} s3://#{remote} --acl public-read`
raise "S3 sync command failed: #{output}" unless $?.success?
end
def parse
sync
normalize = output.gsub("\n", "\\n")
normalize.scan(/s3:\/\/#{bucket}(.*?)\\n/).flatten
end
end
end
self.
prefixes are not necessary, but that's a preference. Not sure whysync_out
is there as basically an alias forparse
. – Mark Thomas Sep 17 '14 at 0:47