Skip to content

Instantly share code, notes, and snippets.

@webdevlasse
Last active July 8, 2016 14:59
Show Gist options
  • Save webdevlasse/d4b3dbedf27686e91fc2928d11269300 to your computer and use it in GitHub Desktop.
Save webdevlasse/d4b3dbedf27686e91fc2928d11269300 to your computer and use it in GitHub Desktop.
solution for retrieving completed reports from aws
#per my process: I loaded all of this data into a normal ActiveRecord Table via a migration and with proper indexing.
# I looked at the request and response data needed for AWS per the docs, and limited them to only what I knew was required though
# the sdk gives a lot of metadata that could be useful in the future. I did not test this with an actual aws bucket, but per my understanding
# it only needs the bucket name and the name of the object in the bucket which was the report_uuid. This could obviously use an
#integration test, stubbed responses and an end-to-end test on the business necessary features.
# I like to write these kinds of data to temp folders if the data is likely to go stale quickly and need to be updated
# The use of private methods creates an easy to understand footprint for the report object so that a developer can quickly get
#either a collection of completed reports, or to find out if an instance of the class is completed. That's about it. Thanks.
module Reports
class FetchPersisted
def initialize(report)
@report = report
@bucket_name = SimpleStorage::GetBucketName.new(report).get
end
def fetch
if @report.completed?
begin
#s3 being the variable for the configured s3 instance and copy_object coming from the aws-sdk
completed_report_response = s3.copy_object({
bucket: @bucket_name,
copy_source: @report.report_uuid#assuming the naming convention is same for db and aws,
}
)
#write to a tmp folder so the reports can be easily cleaned up when stale,
# depending on business needs, copy_object_result should be the only thing we care about saving to file for now
File.write('tmp/completed_reports/#{@report.report_uuid}', completed_report_response.copy_object_result)
rescue Aws::S3::Errors::ServiceError => e
#keep the exception around to use in debugging later
File.write('log/development.log', e.backtrace.inspect )
end
else
return false
end
end
end
end
class Report < ActiveRecord::Base
validates :report_uuid, presence: true, uniqueness: true
enum workflow_status: { open: 0,
completed: 1,
failed: 2 }
private
#why enumerator is handy, very easy lookup for getting all completed reports, allows for human language syntax
def self.open?
where workflow_status: 0
end
def self.completed?
where workflow_status: 1
end
def self.failed?
where workflow_status: 2
end
end
r = Report.last
f = Reports::FetchPersisted.new(r)
f.fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment