Skip to content

Instantly share code, notes, and snippets.

@danjohnson3141
Last active April 15, 2016 20:46
Show Gist options
  • Save danjohnson3141/a5939b33ac742b07c41e579e73e39aef to your computer and use it in GitHub Desktop.
Save danjohnson3141/a5939b33ac742b07c41e579e73e39aef to your computer and use it in GitHub Desktop.
Revcaster Code Challenge

Hello Daniel,

Thanks for taking the time to review my code challenge.

I have to admit that all of my interaction with S3 has concerned uploading assets not downloading them.

I've taken a quick stab at your request but, obviously, if this were a real project I'd have some questions.

Since I'm fairly weak at getting things from S3 I've also included a snippet of code from a user profile photo upload I recently created.

code_challenge.rb

module Reports
  # Fetch the content of a persisted report file
  class FetchPersisted
    # @param [Report] report
    def initialize(report)
      @report = report
      @bucket_name = SimpleStorage::GetBucketName.new(report).get # Returns a string
    end

    # Pull in contents of report file
    def fetch
      return false unless @report.present? && @report.workflow_status = Report.workflow_statuses[:completed]
      s3 = Aws::S3::Client.new
      s3.get_object(bucket: @bucket_name, key: @report.report_uuid)
    end
  end
end

users_controller.rb

This is from one of my APIs. It takes a multi-part file upload from the web, iOS, or Android and sends it onto S3. The bucket structure supports multi-tenant (clients) and environments (develop/stage/production). After the file is successfully uploaded the resulting url to the image is saved on the row's photo field. The rand(1000...9999).to_s is there to trick the browser cache into reloading.

module V1
  class UsersController < ApplicationController
    include SanitizerHelpers
    include ObjectSetters

    before_action only: [:show, :update, :destroy, :upload, :rotate, :reset_pw, :edit] { set_object(User) }

    # POST /users/upload
    def upload
      filekey = "#{$tenant.token}/#{Rails.env}/profile/#{@user.id}"
      Aws::S3::Resource.new.bucket("access-resources").object(filekey).upload_file(params[:file].tempfile, acl: "public-read")
      url = "https://s3.amazonaws.com/access-resources/#{filekey}?#{rand(1000...9999).to_s}"
      @user.update(photo: url)
      render json: { url: url }, status: :created
    end
  end
end

Thanks again for looking this over. Feel free to poke around the rest of my <a href=""https://github.com/danjohnson3141>Git repos. They are mostly hobby projects though. All of my best work is in a private repos unfortunately.

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