Skip to content

Instantly share code, notes, and snippets.

View danielwestendorf's full-sized avatar
🤘
rustl'n jimmies

Daniel Westendorf danielwestendorf

🤘
rustl'n jimmies
View GitHub Profile
Retriable.retriable(tries: 10, on: [RateLimitReached], intervals: Rails.application.config.retry_intervals) do
res = make_request
if res.status == 429
raise RateLimitReached
else
# process returned invoice
end
end
<!DOCTYPE html>
<html>
<head>
<style>
body, html {
margin: 0;
height: 100%;
}
.flex-column {
# spec/support/have_size.rb
require "rspec/expectations"
RSpec::Matchers.define :have_size do |expected|
match do |actual|
actual.size == expected
end
failure_message do |actual|
@danielwestendorf
danielwestendorf / compress_requests.rb
Created March 28, 2017 23:28 — forked from relistan/compress_requests.rb
Rack Middleware to automatically unzip gzipped/deflated POST data
class CompressedRequests
def initialize(app)
@app = app
end
def method_handled?(env)
!!(env['REQUEST_METHOD'] =~ /(POST|PUT)/)
end
def encoding_handled?(env)
$(document).on('change', 'input[type="file"]', function(delegatedEvent) {
var event = delegatedEvent.originalEvent;
var csrfToken = $('[name="authenticity_token"]').val();
var presignBaseUrl = $('form').attr('action');
[].forEach.call(event.target.files, function(file) {
var filename = encodeURIComponent(file.name);
var size = encodeURIComponent(file.size);
var uploader = new S3(csrfToken);
var presignUrl = presignBaseUrl +
S3 = function(csrfToken) {
var handlers = [];
var obj = this;
var isSuccess = function(xhr) {
return (xhr.status >= 200 && xhr.status < 300) || xhr.status === 304;
};
var formData = function(file, fields) {
var data = new FormData();
# Handle file uploads to S3
class UploadsController < ApplicationController
def new
@upload = Upload.new
end
# Step 1: POST to app to get the presignature URL
def create
@upload = Upload.create!(upload_params)
render json: signature # Step 2: Return the presigned URL after doing some validations
# config/initializers/aws.rb
require "aws-sdk"
Aws.config.update({
region: ENV["AWS_REGION"],
credentials: Aws::Credentials.new(ENV["AWS_ACCESS_KEY_ID"], ENV["AWS_SECRET_ACCESS_KEY"])
})
export AWS_ACCESS_KEY_ID=KEY
export AWS_SECRET_ACCESS_KEY=SECRET
export AWS_S3_BUCKET=BUCKET_NAME
export AWS_REGION=REGION
class Upload < ApplicationRecord
validates :size, inclusion: { in: 0..2.megabytes }
def key
"#{id}/#{filename}"
end
def url
bucket.object(key).presigned_url(:get, expires_in: 604_800)
end